Question:
Is there a way to make a binary executable from Java code?
Answer:
There are several stunts you could try in order to accomplish this,
but they all cripple the platform independence of your Java programs.
For example, on Unix or Wintel systems, you could create a shell
script or batch file that included the relevant class files, extracted
them into a temporary directory and then invoked the Java interpreter
on the class files with the right arguments. This means you can
invoke the program with just one command without having to set a
CLASSPATH variable first or to explicitly invoke the Java interpreter
on the commandline. In fact, this is how HotJava (itself written in
Java) is invoked. But shell scripts are not very cross-platform and
introduce bugs and insecurity for the user.
If shell scripts are not an option, you could write a C program that
set the appropriate environment variables and then invoked a secondary
binary - in this case the Java interpreter - with the right set of
parameters. Unix platforms can do this with the system() call, other
platforms also have similar system-dependent library routines that
allow applications to invoke other programs. The class files can
be statically compiled into the binary as arrays of data or can
accompany the program as set up files depending on how tightly you
wish to bundle the various files.
Both of these stunts of course, assume that you have the Java interpreter
available on the system. If that's not the case, then you can pretty
much forget having an executable Java program because your Java application
will need the Java libraries, class files, and set up info that
comprise Java's core packages.
This may become less of an issue in the future since most computer
vendors have announced plans to make a Java virtual machine implementation
available for their platforms. This would mean that every system
will have a Java interpreter or just-in-time compiler built right
into the operating system of the machine.