Java Reference
In-Depth Information
this file yourself by opening YOURPROJECT.jar in an archive manager, such as WinZIP, WinRAR,
or 7‐Zip, but this is by no means required):
Manifest-Version: 1.0
Class-Path: .
Main-Class: Program
8.
Finally, if you want to run a main method from a JAR file not containing a MANIFEST.MF file (not
exported as a runnable JAR file in Eclipse) or want to run another main method, you can do so by
executing the following command:
java -classpath courseadministration.jar Program
How It Works
Now take a look at how it works.
1.
When running programs from Eclipse, the args variable will be empty by default. It is also possible
to supply arguments to programs from Eclipse, but this is a bit involved and generally not required.
2.
In the next steps, you create the runnable JAR. Eclipse will compile your classes, compress them,
and store them in the JAR file together with a MANIFEST.MF ile.
3.
Next, you run the program from the command line:
cd "C:\Users\USERNAME\Desktop\"
java -jar courseadministration.jar
You should get back the same output as the Eclipse console gave you earlier. The first command
( cd ) navigates to the desktop directory where you saved your JAR. The second command ( java )
calls Java and tells it to execute your JAR in the command line. JAR files can be distributed to
others and run on all platforms where a Java Runtime Environment (JRE) is available.
4.
The next command does the same, but passes in some arguments from the command line to the
program:
java -jar courseadministration.jar Argument1 Argument_2 Argument-3 Argument 4
Why are arguments useful? In most cases—especially for command‐line programs—they sup-
ply configuration parameters to the program at hand, for example, an argument can indicate the
filename that should be read in by a program, or an argument can specify an image‐conversion
program indicating the desired quality of the resulting image, and so on. In most cases, however,
you will not need to use program arguments in day‐to‐day programming, as it is oftentimes easier
(and cleaner) to either let your program read in a configuration file (you will see how to deal with
file input and output in a later chapter) or create a GUI to provide configuration options to users
(GUIs will be dealt with later as well).
5.
The next steps show how Java determines which main method to run from a JAR file, either by
using a MANIFEST.MF file (created by Eclipse), or by passing this manually:
java -classpath courseadministration.jar Program
This command will add your JAR file to the Java classpath (the locations in which Java will look
to find classes) and then supplies the class name ( Program ) from which to run the main method.
Search WWH ::




Custom Search