Java Programming/Getting Started/Compilation

From Wikibooks, the open-content textbooks collection

< Java Programming | Getting Started
Jump to: navigation, search
Previous Chapter Next Chapter

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 two step compilation:

  • Step one to create byte-code
  • Step two 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, 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. So it is enough to compile only the high level class, all the dependent classes will be automatically compiled.

javac ... MainClass.java

You can't rely on this feature if your program is using reflection to create objects, you are compiling for servlets or for 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.

The next most popular way to build applications are using an IDE. IDE stands for Intergated Development Environment. Some of them are listed below.

[edit] Packages and Subdirectories

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 coresponding to the package consist of only lower case letters.

[edit] Examples

[edit] Top level package

A class with this package declaration

package example;

has to be in a directory named

example

[edit] Subpackages

A class with this package declaration

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

[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 is distinguish Eclipse from any 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 simular standard way of developing code in the Java world, with a 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 build 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 preferances. 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 intresting GUI for creation of packages and programs. Classes are represented as boxes with arrows running between them to represent inheratance/implementation or if on is constructed in another. BlueJ addes 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, It does not include wizards, and GUI tools, best suited to experienced Java programmers in small and midsized delelopment 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

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 to compile the code, but also to copy code, to package the code to Jar, to create EJBs, to run automated tests, to ftp 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 tasks and their dependencies are defined in a "build.xml" file. The Ant program will parse it and do whatever it describes. The Ant tool is much more powerful than the MAKE tool. Because of the java codes are spread out in the file system according the java package(name space), the use a MAKE tool feels very awkward.

Also the Ant tool is written in Java and can be extended, so if there is a task you'd like to be done during the build, and the task is not in the pre-defined tasks list, you can write it yourself. It is very easy to hook your ant task code to the other tasks, you code only needs to be in the classpath, and the Ant tool will load it during runtime. Your ant task code has to follow the Ant framework, that's all.

A wikibook on ant usage is available at Programming:Apache Ant. The Ant project website : [ant.apache.org]

[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 optimation.



Personal tools
Create a book
  • Add wiki page
  • Collections help