Java Reference
In-Depth Information
for (int i = 0; i < args.length; i++) {
p("Argument "+i+" equals: "+args[i]);
}
p("");
}
static void p(String l) {
System.out.println(l);
}
}
2.
When running this code from Eclipse, you will notice that the
args
variable is empty (the array has
a length of zero).
3.
You're going to create a so‐called “runnable JAR” file. JAR stands for Java ARchive, which is
basically the same as a compressed folder (a ZIP file) containing compiled classes. A
runnable
JAR file is a JAR file that can be executed. To execute it, right‐click your project folder in Eclipse
and choose Export. Next, navigate to Java and select Runnable JAR File. A wizard will pop up
asking you to select a launch configuration and an export destination. In the launch configura-
tion, you can select the class Java should use to run the
main
method. In this case, select
Program
‐ YOURPROJECT
(of course,
YOURPROJECT
represents the name you've chosen). If this option does
not appear in the drop down, you might have forgotten to run the
main
method in Eclipse first
(see Step 2). As the export destination, you will create a JAR file somewhere in the desktop (
C:\
Users\USERNAME\Desktop\YOURPROJECT.jar
, replacing
USERNAME
with your actual username).
4.
Press Finish to create the runnable JAR, which should then appear on your desktop. Normally, it is
possible to just double‐click this file to run it (like with normal programs), but since you have not
created a GUI application, you need to run this JAR from the command line. Open a command
window (run
cmd.exe
in Windows) and execute the following:
cd "C:\Users\USERNAME\Desktop\"
java -jar courseadministration.jar
5.
You should get back the same output as the Eclipse console gave you earlier. Now let's take a look
at the
args
variable. Keeping the command‐line window open, execute the following:
java -jar courseadministration.jar Argument1 Argument_2 Argument-3 Argument 4
6.
Take note of the output now. The program will report that you have supplied five arguments:
Argument1
,
Argument_2
,
Argument‐3
,
Argument
, and
4
. This immediately shows you that argu-
ments are just strings (hence the
String
array) coming from what you pass in the program call
(split based on spaces, ' '). What if you want to include a space in your argument? Then you just
enclose your arguments in double quotes, like so:
java -jar courseadministration.jar "first argument" "second argument"
7.
The final question that remains is how Java knows which
main
method to run in a given JAR file.
This information is stored in a special
META‐INF
folder inside the JAR file. This folder contains a
MANIFEST.MF
file, which will in this case contain the following information (you can hunt down
Search WWH ::
Custom Search