Challenge Execute compiled Java files under .NET without having access to the Java source code. For the sake of this example, assume that the following Java files have been compiled using a native Java compiler:
Listing 1: GuiApplicationLauncher.java
package integrating;
public final class GuiApplicationLauncher {
public static void main(String[] args) {
new GuiApplication().init();
}
}
Listing 2: GuiApplication.java
package integrating;
import java.awt.*;
import java.awt.event.*;
public final class GuiApplication {
public void init() {
window = new Frame("Java");
//...
window.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent event) {
window.dispose();
System.exit(0);
}});
//...
}
private final static class OnMouseEnteredHandler extends MouseAdapter {
//...
}
private final static class OnMouseExitedHandler extends MouseAdapter {
//...
}
private Frame window;
//...
}
Compiling these source files using a native Java compiler will create the following .class files:
- GuiApplication$1.class
- GuiApplication$OnMouseEnteredHandler.class
- GuiApplication$OnMouseExitedHandler.class
- GuiApplication.class
- GuiApplicationLauncher.class
The first four .class files in this list form a minimal working set. A working set of files is a collection of files that together form a closed set of dependencies. In this case, the first four files all depend on each other, and none of them depend on
GuiApplicationLauncher.class. In contrast, any working set containing
GuiApplicationLauncher.class must also contain the other four .class files.
SolutionConvert the five .class files given in the Challenge section above into a .NET executable using the JbImp.exe utility. (The default J#* installation installs JbImp into the C:\Program Files\Microsoft Visual J# .NET\Framework\Bin folder. You need to ensure that this folder is on your PATH.)
JbImp reads .class files containing Java byte code and emits .NET Common Intermediate Language (CIL)* with the equivalent functionality. JbImp only supports Java class files based on Java Developer's Kit (JDK)* versions 1.1.4 and earlier (it cannot convert .class files that contain references to Swing*, for example). You can convert the five .class files in this example into a .NET executable with the following command:
jbimp /target:exe /out:FirstDotNetApplication.exe *.class
The .exe file created is a self-describing CIL program that is JIT compiled into a native executable by the .NET Virtual Execution System* when run.
A separate item, "
How to Compile .java Files into .NET Executables," covers creating .NET executables when the Java source code is available.
Another separate item, "
How to Inspect the Internal Structure of a .NET Executable," shows how to examine the structural dependencies within this .NET executable using the IL DASM utility.
SourceIntegrating Java* and Microsoft .NET*