When you compile and run packaged software from an IDE, it can be as easy as
clicking a run icon as IDE’s support, setting the necessary paths that will be used
by the compiler and interpreters. However, when you try to compile and interpret
the code yourself from the command line, you will need to know exactly how
to path your files. Consider our sample application that is now placed in the
com.scjaexam.tutorial package.
package com.scjaexam.tutorial;
public class GreetingsUniverse {
public static void main(String[] args) {
System.out.println("Greetings, Universe!");
}
}
This exercise will have you compiling and running the application with new
classes created in
a separate package.
1. Compile the program.
javac -d . GreetingsUniverse.java
2. Run the program to ensure it is error-free.
java -cp . com.scjaexam.tutorial.GreetingsUniverse.
3. Create three classes named Earth, Mars, and Venus and place them in
the
com.scja.exam.tutorial.planets package. Create constructors
that will print the names of the planets to standard out. Note that the details
for the Earth class are given here as an example of what you will need to do.
package com.scja.exam.tutorial.planets;
public class Earth {
public Earth {
System.out.println("Hello from Earth!");
}
}
4. Instantiate each class from the main program, by adding the necessary code
to the GreetingsUniverse class.
5. Ensure that all of the source code is in the paths
src/com/scjaexam/tutorial/ and
src/com/scjaexam/tutorial/planets/, respectively.
6. Determine the command-line arguments needed to compile the complete
program. Compile the program, and debug where necessary.
7. Determine the command-line arguments needed to interpret the program.
Run the program.
The standard output will read:
$ Greetings, Universe!
Hello from Earth!
Hello from Mars!
Hello from Venus!