To turn a Java application into an executable program,
you need to link it with the needed libraries, just as for C or C++.
The linker by default looks for a global function named main
.
Since Java does not have global functions, and a
collection of Java classes may have more than one class with a
main
method, you need to let the linker know which of those
main
methods it should invoke when starting the application.
You can do that in any of these ways:
main
method
when you link the application, using the --main
flag,
described below.
gij
program,
making sure that gij
can find the libraries it needs.
-lgij
, which links
in the main
routine from the gij
command.
This allows you to select the class whose main
method you
want to run when you run the application. You can also use
other gij
flags, such as -D
flags to set properties.
Using the -lgij
library (rather than the gij
program
of the previous mechanism) has some advantages: it is compatible with
static linking, and does not require configuring or installing libraries.
These gij
options relate to linking an executable:
--main=
CLASSNAMEmain
method should be invoked when the resulting executable is
run.
-D
name[=
value]
--main
. It defines a system
property named name with value value. If value is not
specified then it defaults to the empty string. These system properties
are initialized at the program's startup and can be retrieved at runtime
using the java.lang.System.getProperty
method.
-lgij
gij
command.
This option is an alternative to using --main
; you cannot use both.
-static-libgcj
Caution: Static linking of libgcj may cause essential parts
of libgcj to be omitted. Some parts of libgcj use reflection to load
classes at runtime. Since the linker does not see these references at
link time, it can omit the referred to classes. The result is usually
(but not always) a ClassNotFoundException
being thrown at
runtime. Caution must be used when using this option. For more
details see:
http://gcc.gnu.org/wiki/Statically%20linking%20libgcj