Building Applets271
Note that in large projects, there may be many source code files, class files and other
object code files to generate the output executables. In such cases, you usually don’t use the Java
compiler on the command line to compile individual files. The best way to carry out such tasks
is to use the
make
utility that you have already learned previously in this book.
9.5.3Running Java Applications
Java applications run on the top of Java virtual machine. The “Hello World” application
that you created in the previous section can be executed using the Java virtual machine.
[root@desktop /root]# java HelloWorld
Hello World!
[root@desktop /root]#
Note that when you run this application, you don’t use
HelloWorld.class
on the
command line. If you do, the Java virtual machine will not be able to execute the command. I
have seen many messages in mailing lists and newsgroups from people who make this mistake
and then wonder why they get error messages.
9.5.4Using gcj to Build Java Applications
The
gcj
compiler is part of the GCC compiler suite. It is able to compile the Java lan-
guage programs. One benefit of compiling Java programs with
gcj
is that you can create binary
executable applications instead of Java byte code. These applications can be executed on Linux
without the need of Java virtual machine. The downside is that you can execute applications
built this way only on the platform for which you compiled. This is in contrast to the basic Java
notion of compile once, run everywhere. Detailed information about using
gcj
is present at its
web site http://gcc.gnu.org/java/.
You can use GNU debugger for programs compiled with
gcj
, which is a big advantage
for many programmers who are familiar with
gdb
. To be able to compile a program with
gcj
,
you must have
libgcj
available on your system. The
libgcj
provides class libraries, byte
code interpreter and garbage collector at the runtime. Latest version of
libgcj
is available
from ftp://sourceware.cygnus.com/pub/java/ and method of its compilation and installation is
discussed in Chapter 3. Refer to Chapter 3 for a description of how to compile programs with
gcj
compiler.
9.6Building Applets
Like any Java applications, applets are also compiled using the Java compiler. The only differ-
ence is that a Java applet is not like a standard application and can be run only inside a browser.
Following is a source code for the applet
HelloWorld.java
that is used as an example here.
The applet, when compiled with the Java compiler results in
HelloWorld.class
file, which
is Java byte code.