« Greasemonkey - Take Control of Your Web Experience | Main | A 15-Minute Guide to Using CVS with WDSc »



July 12, 2005

GCJ - Compile Java into Native Machine Code

Have you ever wanted to convert a Java program into a Windows executable (EXE) or native Linux program? One need for this is that a JRE would not be required on the machine in order to run the program. GCJ, part of the GNU Compiler Collection can accomplish this.

GCJ is the GNU Compiler for the Java Programming Language and it allows you to compile Java source code or .class files into native machine code. As a quick example, I am going to create a simple HelloWorld Java class and convert it into HelloWorld.exe.

Requirements:

Instructions:

  1. Create HelloWorld.java or download it here.
  2. Start Cygwin and change to the directory of your Java file.
  3. Compile the Java into a .o file by typing: gcj -c -g -O HelloWorld.java
  4. Link the file and create a .exe by typing: gcj --main=HelloWorld -o HelloWorld HelloWorld.o
  5. You have now created a file called HelloWorld.exe. However, if you run it you will see the error:
    This application has failed to start because cygwin1.dll was not found. Re-installing the application may fix this problem.
    The reason for this error is because Windows cannot find a Cygwin library needed to run the code. To fix this, add the bin directory of your Cygwin installation, i.e. C:\cygwin\bin to your PATH environment variable. This can be done by right-clicking on My Computer, choosing Properties, going to the Advanced tab, and clicking on the Environment Variables button. Then, edit the 'Path' System variable and add ;C:\cygwin\bin to the end. You will not need the semi-colon in the beginning if there is already one there. Be careful when doing this as this could mess up your system if not done correctly.
  6. Once you add the bin directory to your Path, close all DOS and Cygwin windows that are open, open a new DOS window and run HelloWorld.exe. You will then see the correct output, all without needing a JRE.

NOTE - After adding the Cygwin 'bin' directory to your Path, certain applications may stop working. For example, my Mozilla Firefox installation does not work after doing this. The reason is that the Cygwin library may conflict with other installed libraries. Therefore, you should probably only add the bin directory to your path before running a program compiled with GCJ and then you should remove it after the program has completed. If necessary, create a batch file that adds the path, runs the GCJ-compiled program and then removes the entry from the path. Make sure to only remove the entry and not the entire path.

Posted by Chuck at July 12, 2005 12:12 PM

Trackback Pings

TrackBack URL for this entry:
http://www.chuckcaplan.com/blog/mt-tb.cgi/23

Comments

Could you port all of your java apps over to windows using this program? Is this what developers are doing?

Posted by: Justin at July 12, 2005 04:42 PM

Theoretically, I guess I could. I wonder if anyone has ported a gigantic program like JBoss to native code. That would be fun. I don't think GCJ is hugely popular, but I think it is gaining popularity as I am starting to hear more about it.

Posted by: caplan8293 [TypeKey Profile Page] at July 12, 2005 04:52 PM

I tried compiling the above but linking seemed to produce this error. Why is it calling "ld"?

$ gcj --main=TestMain -o TestMain TestMain.o
/usr/lib/gcc/i686-pc-cygwin/3.4.4/../../../../i686-pc-cygwin/bin/ld: invalid option -- B
Try `/usr/lib/gcc/i686-pc-cygwin/3.4.4/../../../../i686-pc-cygwin/bin/ld --help'
for more information.
collect2: ld returned 1 exit status

Posted by: Joe at February 24, 2006 04:48 AM

Joe,
It looks like your system is trying to use the ld.exe in /usr/lib/gcc/i686-pc-cygwin/3.4.4/../../../../i686-pc-cygwin/bin. In other words, /usr/i686-pc-cygwin/bin. My installation uses the ld.exe in /usr/bin. If you switch which ld.exe the system uses are you still getting that error?
Also, make sure you have the latest version of ld.exe in case a new version has added the option the program is looking for...
Good luck.
Chuck

Posted by: Chuck Caplan [TypeKey Profile Page] at February 25, 2006 07:58 PM

Post a comment




Remember Me?