Sunday 4 November 2007

java Programming in Linux with gcj (basic)

The simple Java source code in Listing 1 can be compiled into Java bytecode with gcj -C HelloWorld.java and interpreted using gij HelloWorld. The same source code can be compiled into a native executable using gcj --main=HelloWorld -o HelloWorld HelloWorld.java and executed using ./HelloWorld. This article avoids including import and other trivial statements in Java code listings; see Resources for the full source files.

####################

## make executable
$ gcj -C HelloWorld.java
$ gij HelloWorld # to test if it works
$ gcj --main=HelloWorld -o HelloWorld HelloWorld.java
$ ./HelloWorld # to run it and test

## make executable jar (same way with in Windows)
$ gcj -c HelloWorld.class # compile Hello.class to Hello.o
$ jar cvf hi.jar HelloWorld.class # create a jar
## You got to include "manefest" file containing the class name of the "main()" method as:Main-class: your.package.Class_NAME.without_dot_class
# modify the "MANIFEST.MF", adding at the end:
Main-class: HelloWorld # Note: 1) there is a space after ':'. 2) there is NO empty line before the 'Main-class' line.


## make executable with jar
$ gcj -c HelloWorld.class # compile Hello.class to Hello.o
$ jar cvf hi.jar HelloWorld.class # create a jar
$ gcj -c hi.jar # compile hi.jar to hi.o
$ gcj --main=HelloWorld -o hi hi.o # link hi.o to hi; gcj accept all reasonable input files, such as .java files, .class files and .o files, even .jar files. Super!!!
$ ./hi # run hi
Hello, World!



No comments:

My photo
London, United Kingdom
twitter.com/zhengxin

Facebook & Twitter