Java Reference
In-Depth Information
Don't Panic During the Exam!
The purpose of the ColorChanger and TestColors example is to demonstrate running
a Java application from a command line, so what the code does is not relevant in this
situation. If you are not familiar with the Container and ActionListener classes, a
ColorChanger can listen to action events of a GUI component in Java because it
implements ActionListener . When an action event occurs, the actionPerformed
method is invoked, which changes the background color of the given GUI component.
You might encounter a situation on the exam where you are not familiar with some of the
classes in the given code. Don't panic! Focus on what the exam question is asking before
trying to fi gure out what the code is doing. You might discover that the behavior of the
code is irrelevant because the question is testing you on a different facet of the language.
You can also set the classpath for the JVM on the command line using the -classpath
fl ag, which is discussed in the next section, followed by a discussion on running Java code
stored in JAR fi les.
The - classpath Flag
The java command that starts the JVM has a -classpath fl ag that allows the classpath to
be specifi ed from the command line. This is a common technique for ensuring the classpath
is pointing to the right directories and JAR fi les. Using the -classpath fl ag overrides the
CLASSPATH environment variable.
For example, we could run the TestColors program using the following command
prompt executed from any directory:
java -classpath c:\myproject\build com.sybex.demos.TestColors
If you have multiple directories or JAR fi les, use a semicolon on a Windows machine to
separate them on the -classpath fl ag. For example, the following command line adds the
current directory to the classpath:
java -classpath c:\myproject\build;. com.sybex.demos.TestColors
On a Unix machine, use a colon to separate multiple directories and JAR fi les:
java -classpath /myproject/build:. com.sybex.demos.TestColors
The java command can also defi ne the classpath using the -cp fl ag, which is just a
shortcut for the -classpath fl ag.
JAR Files
Bytecode can be stored in archived, compressed fi les known as JAR fi les . JAR is short
for Java archive. The compiler and the JVM can fi nd bytecode fi les in JAR fi les without
needing to uncompress the fi les onto your fi le system. JAR fi les are the most common way
Search WWH ::




Custom Search