modsyn – programs and such

My creative outlet

Archive for the ‘SFX’ Category

How to: Write an executable Java driver for Windows

Posted by modsyn on August 10, 2007

If you write Java code, eventually someone will approach you with the question of how to install/run a .JAR file. When you distribute your code to a wide range of anonymous users, the number of these approaches are more than you would have ever imagined. At least, that was the case for me. I wanted as many people as possible to use my application, so I started replying to each request, eagerly trying to get their system properly configured for Java. At first it was fun helping people, but it became so overwhelming that I wanted to pull out my hair. I thought to myself that there had to be a better way. Sure there was a better way, I could write code that was native to the OS. The problem was, I’ve never programmed UI in C/C++/anything besides Java. So, I decided to do the second best thing I could think of: native code that launched my Java code. Simple, right? Yes and no. Since most of the users of the application were Windows users, that’s the OS I targeted.

Here are the steps I took (and sample code) to create a self-extracting executable file that launches a .jar file.

Step One: Download and install MinGW from here. MinGW stands for Minimalist GNU for Windows and it provides the tools you need to compile your executable code (gcc). Where you install it doesn’t matter, just remember you will need to know where it is later.

Step Two: Compile your Java code into a JAR file. Doing so is really out of the scope of this article, but if you need help, see Sun’s Tutorial on the matter.

Step Three: Write the driver native code. This is the most complicated aspect of the process. It is written in C and does 3 things.

  1. Locates the Java Runtime Environment on the host machine. This is accomplished using Registry values. You also provide a target version to look for, I like at least 1.5.
  2. Invokes the specified Java method using the JRE found. For this case it will be the main entry point of the Java application.
  3. Extracts the JAR file from itself into the system’s temporary folder. I mention this part last for a reason, even though it is the first event that occurs.

Looking at the provided code you’ll be able to see where changes should be made. For instance, in the sample code I am launching a Java method named ‘main’ from the HelloWorldUI class that is located in the ‘hello_java.jar’ file. To change it to launch the ‘myEntryPoint’ static method of ‘SuperApplication’ from the ‘RealApp.jar’ change the file to look like:

strcat(tmp,"\\RealApp.jar");
FILE *outf;
outf = fopen( tmp, "wb" );
if( outf==NULL ) {
printf( "couldn't open output file\n" );
exit (2);
}
...
//this is name of the class to invoke in the JAR file
cls = (*env)->FindClass(env, "SuperApplication");
if (cls == 0) {
printf("Can't find class\n");
exit(1);
}
mid = (*env)->GetStaticMethodID(env, cls, "myEntryPoint", "([Ljava/lang/String;)V");
if (mid == 0) {
printf("Can't find main\n");
exit(1);
}

Step Four: Once the driver has been compiled there is one more step before the self-extractor is ready: packing the jar file and the exe file together. For this I have written a simple Java program that copies the exe file to a new file, then copies the jar file to the same new file. At the end of the new file (the last 4 bytes excatly) the program writes the size of the exe driver. These bytes are used by the driver to know where the executable java code starts. All you do is run the program (aptly named ‘SFX’) in the following manner: java SFX driver.exe application.jar sfx_application.exe

All relevant code my be found here. You will need to edit make.bat and change the value of MINGW to point to wherever you installed MinGW from step one. I am assuming that the Java SDK is already in the path.

That’s it for now. I’ll be posting another how to for creating executables with nice icons using MinGW and the GIMP next. Look forward to it, neh?

Don’t forget the files: JSFX.zip

Advertisements

Posted in example, Java, MinGW, programs, SFX, tutorial, Windows | 1 Comment »