Page 1 of 1

Compiling Java programs natively with GCJ. Rate Topic: -----

#1 Tom9729  Icon User is offline

  • Segmentation fault
  • member icon

Reputation: 180
  • View blog
  • Posts: 2,641
  • Joined: 30-December 07

Posted 05 January 2008 - 02:36 PM

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.

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.

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/d...y/j-native.html

Is This A Good Question/Topic? 0
  • +

Page 1 of 1