Java Reference
In-Depth Information
If you do not want to change the current working directory to C:\javaprograms\com\jdojo\intro , you can
compile your source code using the full path for Welcome.java file as follows:
C:\>C:\java8\bin\javac C:\javaprograms\com\jdojo\intro\Welcome.java
It does not matter how you specify the path for the javac command and the Welcome.java file. If the command
prompt can find it, your Java source code will be compiled successfully. Below is another way to compile the
Welcome.java file. This time, I assume that the javac.exe command file is in PATH and the current directory is
C:\javaprograms .
C:\javaprograms>javac com\jdojo\intro\Welcome.java
At this point, it is assumed that you have compiled the source code in Welcome.java successfully, which has
generated a new file named Welcome.class in the C:\javaprograms\com\jdojo\intro directory.
You are now ready to run your first Java program. Before you run this program, let's discuss one important point
about the file name that is generated by the compiler. The name of the bytecode file (the .class file) is Welcome.class .
Why did the compiler choose to name the class file Welcome.class ? You have used the word “Welcome” at three places
when you wrote the source code and compiled it.
First, you declared a class named
Welcome .
Second, you saved the source code in a file named
Welcome.java .
Welcome.java file name to the compiler as an input.
Which one of your three steps prompted the compiler to name the generated bytecode file as Welcome.class ?
As a first guess, it appears to be the third step, which is passing Welcome.java as an input file name to the Java compiler.
However, the guess is wrong. It is the first step, which is declaring a class named Welcome in the source code
Welcome.java , which prompted the compiler to name the output bytecode file Welcome.class . You can declare
as many classes as you want in one Java source code. Suppose you declare two classes, Welcome and Bye , in the
Welcome.java file. What file name will the compiler choose to name the output class file? The compiler scans
the whole source code file. It creates one class file for each class declared in the source code. If the Welcome.java
file had three classes, Welcome , Thanks and Bye , the compiler would have generated three class files, Welcome.class ,
Thanks.class , and Bye.class .
And third, you passed
Running the Compiled Code
A Java program is run by a JVM. A JVM is invoked using a program called java , which is the java.exe file installed on
your machine along with the JDK. The java program accepts the fully qualified name of the Java class you want to run.
Recall that a class has two names: a simple name and a fully qualified name. The fully qualified name must be used
to run the class. You are going to run Welcome class whose fully qualified name is com.jdojo.intro.Welcome . Use the
following command at the command prompt to run the class:
C:\javaprograms>java com.jdojo.intro.Welcome
What happens when you use the above command? First, the JVM tries to locate the bytecode (here, Welcome.class
file) for the com.jdojo.intro.Welcome class on your machine. JVM replaces every dot in the fully qualified name of the
class with the file-separator character on the host system. A backslash is the file-separator character on Windows and a
forward slash on UNIX-like operating systems. This step converts the com.jdojo.intro.Welcome class name that was
passed to the JVM to com\jdojo\intro\Welcome . Note that on a UNIX-like operating system, the same class name will
be converted to com/jdojo/intro/Welcome .
 
Search WWH ::




Custom Search