SOS Company - Simple and Original Solutions
How to create a ".exe" Windows binary file from a Java source code
Contributed by Administrator Sunday, 01 July 2007 Last Updated Saturday, 14 July 2007
This tutorial explains how to create a ".exe" file for Windows, generated from a Java source code. It allows creating more efficient programs using the gcj compiler, because the generated ".exe" file is a native binary file for Windows, and doesn't need any virtual machine to execute. As mentioned previously, this has a second advantage that is your program won't need to install a virtual machine to execute, and relies totally on the Windows system API. The MINGW environment will be used to automate the compilation process with the "make" utility.
First of all, you will need to download the compiler that will generate the ".exe" file. This compiler is the GCC compiler. You can find on the internet many versions of this compiler, either for Linux or for Windows. Personnaly, I used a non official version that you can download from http://www.thisiscool.com/gcc/gcc43ecj-20061207.tar.bz2 . The author of this site maintains some Win32 builds of the GCC compiler, and I find that the most up-to-date version for compiling java programs is the one you can download here. Now that we have downloaded the compiler, let's compile our first program ! For this first example, I will use the so known "Hello world" program. For that, open a command line, and on the prompt, execute the following command : gcj --main=HelloWorld HelloWorld.java -o HelloWorld.exe You will need to have your gcc_install_directory\bin in the path, otherwise the gcj call will fail. You might execute the binary to check the results : > HelloWorld Hello World ! Now let's analyse the compiler command : - gcj is the GCC compiler for java. - --main=HelloWorld means that the starting point of our program is the main method of the HelloWorld class - HelloWorld.java is the name of the file to compile - -o HelloWorld.exe specifies the name of the .exe file to generate
For those who are acustomed with the GCC compiler, only the --main option should be new. This option is mandatory in Java, because unlike the other langages like C, we might have many main methods, one in each class. So we have to specify which one is the entry point for our program. If you look to the size of the ".exe" file, you will see that it is quite big for a so simple program. For me it was about 30M. This is a normal result because GCC links automatically your binary code to the libgcj library that provides all the functions to replace the java virtual machine and the standard java libraries. Please note that some of the standard java classes methods are still not supported.
The second example we will see now is more "real life" example. Generally, one program is formed by many source files. We will see now how we can compile all of these files easily to generate our ".exe" file. You can download this example files from this link. Let's suppose that our program is formed by two files : HelloWorld.java and GoodbyeWorld.java. HelloWorld.java is within the default package, whereas GoodbyeWorld.java is in the Goodbye Package. The commands to compile the program are as following : gcj -c HelloWorld.java -o HelloWorld.o gcj -c Goodbye\GoodbyeWorld.java -o Goodbye\GoodbyeWorld.o gcj --main=HelloWorld HelloWorld.o Goodbye\GoodbyeWorld.o -o MessagesToTheWorld.exe The first two lines will generate intermediate ".o" files, whereas the third line will "link" the intermediate files to generate the ".exe".
http://www.sos-company.com Powered by Joomla! Generated: 18 June, 2011, 06:26
SOS Company - Simple and Original Solutions
This is a quite good solution, but we can do better. For that, we need more tools. Personnaly, I use the "make" program, that eases the compiling process for programs that has too many files. There is a Windows port of many Linux tools called MinGW, that provides also the "make" program for Windows. You can download the MinGW tools by following this link. After that, we need to create a file, called "makefile" that will describe how to compile our files. This will allow us to compile our program, simply by writing "make" on the command line. Some explanations about the makefile I used are provided here : First, I define some variables : SRC, OBJ, TARGET, MAIN_CLASS. The variable SRC will hold the name of all the source files. For that I use the "find" command that will search for all the "*.java" files and put the name of these files within SRC. # Source files list src=$(shell find . -name "*.java")
# Intermediate object files list OBJ=$(SRC:.java=.o)
# .exe file name TARGET=MessagesToTheWorld.exe
# main class name MAIN_CLASS=HelloWorld
Second, I define the rules that will allow the "make" program know how to build my ".exe" file. The main rule is : $(TARGET): $(OBJ) gcj --main=$(MAIN_CLASS) $(OBJ) -o $(TARGET) that is equivalent to : MessagesToTheWorld.exe: Goodbye/GoodbyeWorld.o HelloWorld.o gcj --main=HelloWorld ./Goodbye/GoodbyeWorld.o ./HelloWorld.o -o MessagesToTheWorld.exe
This means that MessagesToTheWorld.exe is up-to-date only if Goodbye/GoodbyeWorld.o and HelloWorld.o are up-todate. In other words, if the modification date of MessagesToTheWorld.exe is older than the modification date of the files Goodbye/GoodbyeWorld.o and HelloWorld.o, we have to execute gcj --main=HelloWorld ./Goodbye/GoodbyeWorld.o ./HelloWorld.o -o MessagesToTheWorld.exe Then I can use make to update my MessagesToTheWorld.exe file by running : > make MessagesToTheWorld.exe gcj -c Goodbye/GoodbyeWorld.java -o Goodbye/GoodbyeWorld.o gcj -c HelloWorld.java -o HelloWorld.o gcj --main=HelloWorld ./Goodbye/GoodbyeWorld.o ./HelloWorld.o -o MessagesToTheWorld.exe If I retry again, no new compiling is done. > make MessagesToTheWorld.exe make: `MessagesToTheWorld.exe' is up to date. This is the most important feature that comes with the "make" program. "make" is able to see that no modification has been done to your program source files thanks to the files modification date, and then won't launch a new compile again. So each time you change any of your source files, you have just to launch the "make" program, and it will recompile only the modified files. Now, to sum-up what we have seen in this tutorial : - Where to get tools to create a ".exe" Windows file from java source code - How to use the gcc compiler to compile the source code - How to automate the compile process using "make"
http://www.sos-company.com Powered by Joomla! Generated: 18 June, 2011, 06:26
SOS Company - Simple and Original Solutions
These techniques have been used to generate the MPEG Parser Windows binary file. Hope you enjoyed this tutorial !
http://www.sos-company.com
Powered by Joomla!
Generated: 18 June, 2011, 06:26