Java Programming/Compilation
| Navigate Getting Started topic: |
We have already discussed compilation basics . Here's a recap of the concepts we'd seen earlier and some additional details.
Contents |
[edit] Compiling to bytecode
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 four step compilation:
- compiler will check the syntactical error
- create byte-code
- create a blank *.class file
- merge that byte code to that blank *.class file
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.
JIT compilers compile 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.
[edit] Automatic Compilation of Dependent Classes
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. It is usually enough to compile only the high level class, since all the dependent classes will be automatically compiled.
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 a "jar" package. In these cases you should list these classes for explicit compilation.
javac ... MainClass.java, ServletOne.java, ...
The best way is to use a build tool to build your application. The build tool would check all the needed dependencies and compile only the needed class for the build. The Ant tool is the best and the most popular build tool currently available. Using Ant you would build your application from the command line by typing:
ant build.xml
The xml file contains all the information needed to build the application.
Note: In rare cases, your code may appear to compile correctly but the program behaves as if you were using an old copy of the source code (or otherwise reports errors during runtime.) When this occurrs, you may need to clean your compilation folder by either deleting the class files or using the Clean command from the IDE.
The next most popular way to build applications are using an IDE. IDE stands for Integrated Development Environment, examples of which are listed below.
[edit] Packages, Subdirectories, and Resources
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. A class with this package declaration package example; has to be in a directory named example
Subpackages. A class with this package declaration package org.wikibooks.en; has to be in the org/wikibooks/en directory.
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. See also the section on how to automate this using ant, below.
[edit] Filename Case
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.
[edit] Compiler Options
[edit] Debugging and Symbolic Information
[edit] Additional Tools
[edit] IDEs
This section contains a little about the different IDEs available and their strengths and weaknesses.
[edit] JBuilder
JBuilder is a IDE with proprietary source code, sold by Borland. One of the advantages in integration with together, a modeling tool.
[edit] JCreator
There's info at: http://www.apcomputerscience.com/ide/jcreator/index.htm
[edit] Eclipse
Eclipse is a free IDE, plus a developer tool framework that can be extended for a particular development need. IBM was behind this free software development and it replaced IBM Visual Age tool. The idea was to create a standard look and feel that can be extended. The extendibility has distinguished Eclipse from other IDE tools. Eclipse also meant to compete with Microsoft Visual Studio tools. Microsoft tools give a standard way of developing code in the Microsoft world. Eclipse gives a similar standard way of developing code in the Java world, with big success so far. With the online error checking only, coding can be speed up by at least 50%(coding does not include programming).
The goal for Eclipse are twofold:
- Give a standard IDE for developing code
- Give a starting point, and the same look and feel for all other more sophisticated tools built on Eclipse
IBM's WSAD, and later IBM Rational Software Development Platform are built on Eclipse.
Standard Eclipse features:
- Standard window management (perspectives, views, browsers, explorers, ...)
- As you type error checking (immediate error indications, ...)
- As you type help window (type ., or <ctrl> space, ...)
- Automatic build (changed source code automatically compiled, ...)
- Built in debugger (full featured GUI debugger)
- Source code generation (getters and setters, ...)
- Searches (for implementation, for references, ...)
- Code refactoring (global reference update, ...)
- Plug-in-based architecture (be able to build tools that integrate seamlessly with the environment and other tools)
- ...
For more information see;
[edit] NetBeans
The NetBeans IDE is a free, open-source Integrated Development Environment for software developers. The IDE runs on many platforms including Windows, Linux, Solaris, and the MacOS. It is easy to install and use straight out of the box. The NetBeans IDE provides developers with all the tools they need to create professional cross-platform desktop, enterprise, web and mobile applications.
More info can be found at http://www.netbeans.org/products/ide/
[edit] BlueJ
BlueJ is an IDE that includes templates and will compile and run the applications for you. BlueJ is often used by classes because it is not necessary to set classpaths. BlueJ has it's own sets of Library's and you can add your own under preferences. That sets the classpath for all compilations that come out of it to include those you have added and the BlueJ libraries.
BlueJ offers an interesting GUI for the creation of packages and programs. Classes are represented as boxes with arrows running between them to represent inheritance/implementation or if one is constructed in another. BlueJ adds all those classes (the project) into the classpath at compile time.
BlueJ Homesite
[edit] Kawa
Kawa was developed by Tek-Tools. It is basically a Java editor which does not include wizards, and GUI tools. It is best suited to experienced Java programmers in small and midsized development teams.
The latest version is 4.0, you can download it. For more info. see Kawa from tek-tools. See a javaworld article
It looks that there is no new development for Kawa.
[edit] Ant
For comprehensive information about all aspects of Ant, please see the Ant Wikibook.
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.
Building a Java application requires certain tasks to be performed. Those tasks may include not only compiling the code, but also copying files, packaging the program into a jar file, running tests and so on. Some of these tasks may depend upon others having been done previously (not creating a jar unless the program has been complied, for instance). It might also be a good idea to not execute all tasks every time the program is complied -- e.g. to only compile changed source files. Ant makes all of these things easy.
The tasks and their dependencies are defined in a build.xml file, 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>
[edit] The JIT compiler
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.
| Java Programming | Index | ||
|---|---|---|---|
| Introduction | Getting Started | Fundamentals | Classes and Objects |
| Collections | Exceptions | Concurrent Programming | Reflection |
| Applets | JavaBeans | Libraries | |
This page may need to be