Compiling Java with GCJ
Before you start, make sure you have a working MinGW environment!
How to build a “Hello, world!†example?
HelloWorld.java:
public class HelloWorld {
public static void main(String[] args) {
System.out.println(“Hello, world!”);
}
}
Type in the command line:
gcj --main=HelloWorld -o HelloWorld
HelloWorld.java
How to build a simple SWT example?
IMPORTANT: libSWT required
HelloSWT.java:
import org.eclipse.swt.widgets.*;
public class HelloSWT {
public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}
}
Type in the command line:
gcj --main=HelloSWT --classpath=swt.jar
-o HelloSWT HelloSWT.java -lswt
How to build a simple JNI example?
HelloJNI.java:
public class HelloJNI {
public native void helloJNI();
static {
System.loadLibrary(“helloJNI”);
}
public static void main(String[] args) {
HelloJNI h = new HelloJNI();
h.helloJNI();
}
}
helloJNI.c:
#include <jni.h>
#include <stdio.h>
#include “helloJNI.h”
// This method is given by the header file
JNIEXPORT void JNICALL Java_HelloJNI_helloJNI
(JNIEnv *env, jobject obj)
{
printf(“Hello, world!”);
return;
}
Type in the command line:
gcj -C HelloJNI.java gcjh -jni -o helloJNI.h HelloJNI gcc -shared -o helloJNI.dll helloJNI.c -Wl,--out-implib,libhelloJNI.a gcj --main=HelloJNI -fjni -o HelloJNI HelloJNI.java libhelloJNI.a