Compilation
Navigate Getting Started topic: ) |
In Java, programs are not compiled into executable files; they are compiled into Bytecode (as discussed earlier), which the JVM then executes at runtime. Java source code is compiled into bytecode when we use the javac
compiler. The bytecode gets saved on the disk with the file extension .class
. When the program is to be run, the bytecode is converted, using the Just-In-Time (JIT) compiler. The result is machine code which is then fed to the memory and is executed.
So Java has two step compilation:
- To create byte-code
- To create machine level code
The Java classes/Byte Codes are compiled to machine code and loaded into memory by the JVM when needed the first time. This is different than other languages like C/C++ where the whole program had to be compiled to machine code and linked to create an executable file, before the program could start.
Quick compilation procedure[edit]
To execute your first Java program, follow the instructions below:
1. | Proceed only if you have successfully installed and configured your system for Java as discussed here. | ||
2. | Open your preferred text editor — this is the editor you set while installing the Java platform. | ||
For example, Notepad or Notepad++ on Windows; Gedit, Kate or SciTE on Linux; or, XCode on Mac OS, etc. | |||
3. | Write the following lines of code in a new text document:
|
||
4. | Save the file as HelloWorld.java — the name of your file should be the same as the name of your class definition and followed by the .java extension. This name is case-sensitive, which means you need to capitalize the precise letters that were capitalized in the name for the class definition. |
||
5. | Next, open your preferred command-line application. | ||
For example, Command Prompt on Windows; and, Terminal on Linux and Mac OS. | |||
6. | In your command-line application, navigate to the directory where you just created your file. If you do not know how to do this, consider reading through our crash courses for command-line applications for Windows or Linux. | ||
7. | Compile the Java source file using the following command which you can copy and paste in if you want:
|
||
8. | Once the compiler returns back to the prompt, run the application using the following command:
|
||
9. | The above command should result in your command-line application displaying the following result:
|
- Ask for help if the program did not execute properly in the Discussion page for this chapter.
Automatic Compilation of Dependent Classes[edit]
In Java, if you have used any reference to any other java object, then the class for that object will be automatically compiled, if that was not compiled already. These automatic compilations are nested, and this continues until all classes are compiled that are needed to run the program. So it is usually enough to compile only the high level class, since all the dependent classes will be automatically compiled.
![]() |
Main class compilation
javac ... MainClass.java |
However, you can't rely on this feature if your program is using reflection to create objects, or you are compiling for servlets or for a "jar", package. In these cases you should list these classes for explicit compilation.
![]() |
Main class compilation
javac ... MainClass.java, ServletOne.java, ... |
Packages, Subdirectories, and Resources[edit]
Each Java top level class belongs to a package (covered in the chapter about Packages). This may be declared in a package
statement at the beginning of the file; if that is missing, the class belongs to the unnamed package.
For compilation, the file must be in the right directory structure. A file containing a class in the unnamed package must be in the current/root directory; if the class belongs to a package, it must be in a directory with the same name as the package.
The convention is that package names and directory names corresponding to the package consist of only lower case letters.
Top level package[edit]
A class with this package declaration
![]() |
Code section 2.1: Package declaration
package example; |
has to be in a directory named example
Subpackages[edit]
A class with this package declaration
![]() |
Code section 2.2: Package declaration with sub-packages
package org.wikibooks.en; |
has to be in a directory named en
which has to be a sub-directory of wikibooks
which in turn has to be a sub-directory of org
resulting in org/wikibooks/en
.
Java programs often contain non-code files such as images and properties files. These are referred to generally as resources and stored in directories local to the classes in which they're used. For example, if the class com.example.ExampleApp
uses the icon.png
file, this file could be stored as /com/example/resources/icon.png
. These resources present a problem when a program is complied, because javac
does not copy them to wherever the .class
files are being complied to (see above); it is up to the programmer to move the resource files and directories.
Filename Case[edit]
The Java source file name must be the same as the public class name, the file contains. There can be only one public class defined per file. The Java class name is case sensitive, as is the source file name.
The naming convention for the class name is for it to start with a capital letter.
Compiler Options[edit]
Debugging and Symbolic Information[edit]
Ant[edit]
- For comprehensive information about all aspects of Ant, please see the Ant Wikibook.
The best way to build your application is to use a build tool. The build tool would check all the needed dependencies and compile only the needed class for the build. The Ant tool is one of the best and the most popular build tools currently available. Ant is a build management tool designed to replace MAKE as the tool for automated builds of large Java applications. Like Java, and unlike MAKE, Ant is designed to be platform independent.
Using Ant you would build your application from the command line by typing:
![]() |
Ant building
ant build.xml |
The build.xml
file contains all the information needed to build the application.
Building a Java application requires certain tasks to be performed defined in a build.xml
file. Those tasks may include not only compiling the code, but also copying code, packaging the program to Jar, creating EJBs, to running automated tests, doing ftp for the code to remote site, and so on. For some tasks a condition can be assigned, for example a compile only changed code, or do the task if that was not already done so. Tasks dependency can also be specified, that will make sure that the order of executions of the tasks are in the right order. For example, compile the code before package them to jar, the package-to-jar task depend on the compilation task.
The build.xml
file is generally kept in the root directory of the java project. Ant parses this file and executes the tasks therein. Below we give an example build.xml
file.
Ant tool is written in Java and is open source, so it can be extended if there is a task you'd like to be done during the build that is not in the pre-defined tasks list. It is very easy to hook your ant task code to the other tasks: your code only needs to be in the classpath, and the Ant tool will load it at runtime. For more information about writing your own Ant tasks, please see the project website at http://ant.apache.org/.
build.xml
file.<project name="ExampleApp" basedir="." default="main">
<property name="source.dir" value="source" />
<property name="libraries.dir" value="libraries" />
<property name="build.dir" value="build" />
<property name="classes.dir" value="${build.dir}/classes" />
<property name="dist.dir" value="${build.dir}/dist" />
<property name="main-class" value="com.example.ExampleApp"/>
<path id="classpath">
<fileset dir="${libraries.dir}" includes="**/*.jar"/>
</path>
<target name="clean">
<delete dir="${build.dir}"/>
</target>
<target name="compile">
<mkdir dir="${classes.dir}"/>
<javac srcdir="${source.dir}" destdir="${classes.dir}" classpathref="classpath" />
<copy todir="${classes.dir}">
<fileset dir="${src.dir}" excludes="**/*.java" />
</copy>
</target>
<target name="build" depends="compile">
<mkdir dir="${dist.dir}"/>
<copy todir="${dist.dir}/lib" flatten="true">
<path refid="classpath" />
</copy>
<path id="dist.classpath">
<fileset dir="${dist.dir}/lib" includes="*.jar" />
</path>
<manifestclasspath property="dist.manifest.classpath" jarfile="${dist.dir}/${ant.project.name}.jar">
<classpath refid="dist.classpath" />
</manifestclasspath>
<jar destfile="${dist.dir}/${ant.project.name}.jar" >
<zipfileset dir="${classes.dir}" />
<manifest>
<attribute name="Class-Path" value="${dist.manifest.classpath}"/>
<attribute name="Main-Class" value="${main-class}" />
</manifest>
</jar>
</target>
<target name="run-build" depends="build">
<java jar="${dist.dir}/${ant.project.name}.jar" fork="true">
<classpath>
<path refid="classpath"/>
<path location="${dist.dir}/${ant.project.name}.jar"/>
</classpath>
</java>
</target>
<target name="run" depends="compile">
<java classname="${main-class}" >
<classpath>
<path refid="classpath"/>
<pathelement location="${classes.dir}" />
</classpath>
</java>
</target>
<target name="clean-build" depends="clean, build"/>
<target name="main" depends="clean, run"/>
</project>
The next most popular way to build applications is using an Integrated Development Environment (IDE).
The JIT compiler[edit]
The Just-In-Time (JIT) compiler is the compiler that converts the byte-code to machine code. It compiles byte-code once and the compiled machine code are re-used again and again, to speed up execution. Early Java compilers compiled the byte-code to machine code each time it was used, but more modern compilers cache this machine code for reuse on the machine. Even then, java's JIT compiling was still faster than an "interpreter-language", where code is compiled from high level language, instead of from byte-code each time it was used.
The standard JIT compiler runs on demand. When a method is called repeatedly, the JIT compiler analyzes the bytecode and produces highly efficient machine code, which runs very fast. The JIT compiler is smart enough to recognize when the code has already been compiled, so as the application runs, compilation happens only as needed. As Java applications run, they tend to become faster and faster, because the JIT can perform runtime profiling and optimization to the code to meet the execution environment. Methods or code blocks which do not run often receive less optimization; those which run often (so called hotspots) receive more profiling and optimization.