Java Reference
In-Depth Information
Compiling the Source Code
Compiling is the process of translating the source code into a special binary format called bytecode . This is accomplished
using a program (usually called a compiler) javac, which comes with JDK from Oracle Corporation. The process of
compiling Java source code is shown in Figure 2-4 .
Java source code
(Welcome.java)
javac
(Java Compiler)
Byt ecode
(Welcome.class)
Figure 2-4. The process of compiling a Java source code into bytecode
You supply the source code (in your case, Welcome.java ) as input to the Java compiler and it generates a new file
(or a set of files) with extension .class . The file with extension .class is called a class file. A class file is in a special
format called bytecode. Bytecode is a machine language for Java virtual machine (JVM). I will discuss the JVM and
bytecode later in this chapter.
Now, I will walk through the steps that are needed to compile the source code on Windows. For other platforms,
for example, UNIX and Mac OS X, you will need to use the file path syntax specific to those platforms.
It is assumed that you have installed JDK from Oracle Corporation on your machine. It is further assumed that
you have stored your source code in a directory such as
C:\javaprograms\com\jdojo\intro\Welcome.java
Open a command prompt and change the directory, so intro should be your current working directory.
The prompt should look as follows:
C:\javaprograms\com\jdojo\intro>
To compile the Welcome.java , execute the following command:
C:\javaprograms\com\jdojo\intro>javac Welcome.java
If you do not get any error message, it means your source code was compiled successfully and it generated a new
file named Welcome.class in intro directory. However, if you get any error, there could be one of the three reasons:
Welcome.java file in the directory C:\javaprograms\com\jdojo\intro .
You have not saved the
You may not have installed the JDK on your machine.
JAVA_HOME\bin directory to the
PATH environment variable, where JAVA_HOME refers to the directory where you installed
the JDK on your machine. If you installed JDK 8 in the directory C:\java8 , you need to add
C:\java8\bin to the PATH environment variable on your machine.
If the above discussion about setting PATH environment variable did not help, you can use the following
command. This command assumes that you have installed JDK in the directory C:\java8 .
If you have already installed the JDK, you have not added the
C:\javaprograms\com\jdojo\intro>C:\java8\bin\javac Welcome.java
 
Search WWH ::




Custom Search