Running Java Applications
A standalone Java application must have at least one class
containing a method called main(), which is the
first code to be executed upon startup. To run the application, start the
VM, specifying that class as an argument. You can also specify options to
the interpreter as well as arguments to be passed to the
application:
%java[interpreteroptions]class_name[programarguments]
The class should be specified as a fully qualified class name, including the package name, if any. Note, however, that you don’t include the .class file extension. Here are a couple of examples:
%javaanimals.birds.BigBird%javaMyTest
The interpreter searches for the class in the classpath, a list of directories and
archive files where classes are stored. We’ll discuss the classpath in
detail in the next section. The classpath can be specified either by an
environment variable or with the command-line option
-classpath. If both are present, the
command-line option is used.
Alternately, the java command can be used to launch an “executable” Java archive (JAR) file:
%java-jarspaceblaster.jar
In this case, the JAR file includes metadata with the name of the
startup class containing the main()
method, and the classpath becomes the JAR file itself.
After loading the first class and executing its main() method, the application can reference
other classes, start additional threads, and create its user interface or
other structures, as shown in Figure 3-1.
Figure 3-1. Starting a Java ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access