Welcome to Dream.In.Code
Getting Java Help is Easy!

Join 109,338 Java Programmers for FREE! Ask your question and get quick answers from experts. There are 946 online right now! We've got more than 500 tutorials and 2,000 snippets. Join and find out why Dream.In.Code is the #1 programming help community on the internet! Registration is fast and FREE... Join Now!



Compiling Java programs natively with GCJ.

 
Reply to this topicStart new topic

> Compiling Java programs natively with GCJ.

Tom9729
Group Icon



post 5 Jan, 2008 - 02:36 PM
Post #1


What is GCJ?

GCJ is the GNU Java compiler. It's free, open source, and available on many platforms.


Why would I want to natively compile my code with GCJ?

It will run faster, it will take up less memory while running, and it won't rely on any clunky virtual machine!


Why wouldn't I want to natively compile my code with GCJ?

Natively compiled code takes up more disk space than the ".class" files generated by javac.

Natively compiled code isn't cross platform.

GCJ only supports the Java spec up to about 1.1 (last I read). While this is great for simpler programs, it can take some real work to get more complex ones working. In particular, AWT and Swing are a real pain.

If you want to use an external library like LWJGL, you'll have to natively compile that as well.


Where can I get GCJ?

If you're using Linux, GCJ should be in your package manager.

If you're using Windows, you'll need to install MinGW from here. Last time I used it, there was an installer. Make sure you select GCJ during the install!


A very basic example.

Our example source code is very simple.

CODE

import java.lang.String;

public class test
{
    public static void main(String[] args)
    {
        String one = new String("Hello, I like coding.");

        System.out.println(one);

        String two = one.split(",")[0];

        System.out.println(two);

        String three = one.split(",")[1];

        System.out.println(three);
    }
}


Now we're going to compile and run it.

CODE

tom@midnight:/tmp$ gcj --main=test -o test test.java
tom@midnight:/tmp$ ./test
Hello, I like coding.
Hello
I like coding.
tom@midnight:/tmp$


Let's go over the arguments specified to GCJ in that command.
--main=test, specifying the class that contains our main method.
-o test, specifying the output binary. If you use Windows, you should make this -o test.exe for example.
test.java, the source code we're compiling.


Recommended readings.

http://www.ibm.com/developerworks/library/j-native.html
Go to the top of the page
+Quote Post


Register to Make This Ad Go Away!


Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 

Lo-Fi Version Time is now: 9/6/08 04:51PM

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month