68Chapter3 • Compilers and Assemblers
3.3.7.3Compiling Java Code
Information about the GCC Java compiler
gcj
is available at http://gcc.gnu.org/java/.
Before you can build Java programs, you also need to have
libgcj
installed. With old compil-
ers, you had to install it separately from source code. When you build and install new versions of
GCC,
libgcj
is installed automatically. If you are still using an old compiler and want to have
libgcj
installed, information is available at http://gcc.gnu.org/java/libgcj2.html. Briefly the
process is as follows:
Download
libgcj
from ftp://sourceware.cygnus.com/pub/java/ or another web site on
the Internet. Untar it in
/opt
directory and a new directory will be created under
/opt
which
will contain source code for
libgcj
. Create a directory
/opt/libgcj-build
and move
into this directory. After that you have to perform the following sequence of commands:
•../libgcj/configure
•make
•make install
Note that your new compiler must be in
PATH
before you build
libgcj
.
Now let us see how to compile a Java program. Consider the following simple Java pro-
gram that prints the message “Hello World”. The source code filename is
hello.java
.
class HelloWorld {
public static void main (String args[]) {
System.out.print("Hello World ");
}
}
Traditionally you have to invoke the
javac
program to build a byte code. After that you
have to run the byte code using the
java
program on Linux. However if you use
gcj
, you can
create a binary output file
hello
using the following command:
gcj –main=HelloWorld –o hello hello.java
The output file is
hello,
which is an executable binary. The –
main
switch is used for
the entry point when the program is executed.
The compiler uses some information to build Java code. This information includes reading
the
gcj
specification file and libraries. The following command displays this information.
[rr@conformix 4]$ gcj -v
Reading specs from /opt/gcc-3.0.4/lib/gcc-lib/i686-pc-linux-
gnu/3.0.4/specs
Reading specs from /opt/gcc-3.0.4/lib/gcc-lib/i686-pc-linux-
gnu/3.0.4/../../../libgcj.spec
rename spec lib to liborig
rename spec startfile to startfileorig
Next Page >>
<< Previous Page
Back to the Table of Contents