Java Reference
In-Depth Information
Specifying the Class Name
The command line for java.exe requires the name of the class that contains main . Notice
that the name of the class is not the same as the name of the bytecode fi le, which in the
SayHello example is SayHello.class . The following command line does not work:
java SayHello.class
The JVM looks for a class named class in the SayHello package (which it will not fi nd)
and throws a NoClassDefFoundError . The JVM only needs the name of the class; it will
fi nd the corresponding bytecode fi le by scanning all the directories and JAR fi les set in
your CLASSPATH environment variable. If you do not set a CLASSPATH , the JVM looks in the
current working directory.
The exam will likely test your knowledge with a more complex example where the class
containing main is in a package. Let's look at another example, starting with a class called
ColorChanger in the com.sybex.events package:
1. package com.sybex.events;
2.
3. import java.awt.Component;
4. import java.awt.Color;
5. import java.awt.event.*;
6.
7. public class ColorChanger implements ActionListener {
8. private Component container;
9.
10. public ColorChanger(Component c) {
11. container = c;
12. }
13.
14. public void actionPerformed(ActionEvent e) {
15. String color = e.getActionCommand();
16. if(color.equals(“red”)) {
17. container.setBackground(Color.RED);
18. } else if(color.equals(“blue”)) {
19. container.setBackground(Color.BLUE);
20. } else {
21. container.setBackground(Color.WHITE);
22. }
23. }
24. }
Search WWH ::




Custom Search