Book creator (disable)

Java Programming/Java Programming Environment

From Wikibooks, the open-content textbooks collection

< Java Programming
Jump to: navigation, search
Navigation Left Arrow.svg The Java Platform Java Programming
Java Programming Environment
Getting Started Navigation Right Arrow.svg


To compile Java programs, you will need to download and install the Java Development Kit (JDK). This is available from Sun's website. Other hardware and operating system vendors also supply Java Development Kits for their platforms, although they may change the name of the kit. Sun produces JDKs for Windows, Linux, and Solaris.

There are numerous environments you can use to develop your Java programs. You can choose to write your programs in a text editor and then compile them using the command line, or you can use an integrated development environment (IDE). IDEs like NetBeans and Eclipse provide many useful functions such as syntax error checking, code completion, automatic compilation and debugging, which you may find useful, especially if this is your first foray into programming.

Contents

[edit] The Java Compiler

The JDK consists of a set of tools necessary to construct Java programs. The most notable tool in the JDK is the Java compiler, also known as javac.

javac compiles Java source files into executable Java class files. Source files are text files with a .java file name extension. You can create such files with a text editor, like Notepad, or an IDE. javac then compiles these files into loadable and executable class files, using the .class extension. For example, if you create a Java class org/wikibooks/util/PrintDate.java

package org.wikibooks.util;
import java.util.Date;
public class PrintDate 
{
    public static void main(String[] args) 
    {
        System.out.println(new Date());
    }
}

you can compile it by entering the following command in a command shell:

javac org/wikibooks/util/PrintDate.java

You would normally invoke this in a shell whose working directory is the root directory containing all of your source files resides. (The Java package statement, package org.wikibooks.util, corresponds to the directory structure org/wikibooks/util see Java packages.)

javac will create the file PrintDate.class in the same directory where the source file is located. If there are syntax errors, javac will print those to the shell. The PrintDate.class file contains the byte code, that will run under all hardware where the Java runtime is installed. Even if the PrintDate.class file was created in Windows operating system, you can copy this file to unix and it will be executed fine.

Usually there is more than one class file created and those files are packaged to a application_name.jar file to distribute to run in any hardware.

IDEs may manage this process automatically. For example, Eclipse contains its own Java compiler and thus does not use javac directly. It automatically compiles the Java source files when you save them. The use of IDE's such as Eclipse are beyond the scope of this module, however, so consult the IDE's tutorials and help to see how they provide a Java programming environment.

[edit] The bytecode

It should be clear from the above paragraph that the Java compiler compiles source code text files with the extension .java into executable code usually confined into a class file with the .class extension. Such code is called Bytecode.

In many languages prior to Java, the source code would generally compile into the machine-code for the particular machine the program was compiled upon. Therefore, if a program was compiled on an X86 machine, it would run only on an X86 machine and no other. Java, on the other hand, produces a bytecode - an intermediate binary form of code that is a portable representation of the Java class. Any Java Virtual Machine on any hardware/operating system platform can then execute this same bytecode. There are some restrictions to this portability. For example, a Java ME system cannot execute all programs compiled for the Java SE environment because Java ME is pared down to small devices. But in general, a Java SE program can run unmodified on any Java SE virtual machine.

[edit] The JIT compiler

Being compiled halfway through, it is the job of the Java Virtual Machine to compile the rest of the program to native code at the time of its execution making Java code follow the "Write Once, Run Anywhere" (WORA) policy. The compiler used to compile bytecode into machine-code at runtime is called the Just-In-Time or JIT compiler. Once a piece of code is compiled by the JVM to execution code, the code is used and re-used again and again, to speed up execution.

[edit] The Java Runtime Environment

The Java Runtime Environment, or JRE, is responsible for the execution of Java programs. The Sun JDK also includes the JRE. The JRE however can also be installed and used without installing the JDK which is useful if you wish to execute Java programs but not build them. The JRE helps load Java programs into the memory and executes them.

[edit] Main entry point

In Java programming, classes are used to define objects and entities that hold particular data. To execute a Java program, a special class is required to assist in loading the program into the computer's memory. Such a class contains a method with the following signature.

public static void main(String[] args) {...}

The class is hence said to have a main entry point defined and is usually called a Java program. The method described above as the main entry point is usually nicknamed the main method. Java classes without main methods are simply classes, although they may be part of a program.

Each Java class can have a main method, as opposed to C, and C++ where there can only be one, for the whole program. This feature is good in a sense that the main method can be implemented to do some testing on the class. The program must define one class's main method as the entry point for the program.

[edit] Executing a command-line Java program

If a class which you compile with javac has a main entry point, you can execute the class by specifying the class name as an argument to the java program.

java org.wikibooks.util.PrintDate

This will run the main method in the PrintDate class in the org.wikibooks.util package. (Packages provide a convenient way to provide namespaces and organization of Java classes. We'll use org.wikibooks.util as a parent package in this module. More on packages.)

Your program will begin execution in the shell window. Inputs and outputs will be gathered from and to your shell window. On a Windows platform, you can use a DOS command window as the command shell program for the execution of Java programs. The JRE is normally called java because the java program is the most widely used program to execute Java programs.

[edit] Executing a Graphical User Interface Java program

On the Windows platform, there is an alternate JRE executable called javaw.exe or javaw which runs Java programs as a Windows native application - that is, with no console for standard input or output.

javaw org.wikibooks.util.ViewDate

Rather than executing in the console, the Java program would be executed in a separate Windows native process. This is typically done for Java applications which create their own graphical user interface (GUI) windows. The above-mentioned org.wikibooks.util.PrintDate program, which prints output to the standard output stream, is not appropriate for use with javaw as there is no console output. Instead, org.wikibooks.util.ViewDate would be a program which creates its own windows to display the date.

As with java, IDE's also manage the execution of Java programs in slightly different ways. They may provide shortcuts for running programs and windows for capturing the output.

On UNIX/Linux this does not matter. If the program is launched graphically (by file association in a file manager) a console is not shown. GUI programs will have the titlebars following the look and feel of your desktop (KDE, GNOME, Fluxbox, XFCE) theme.

[edit] Other JDK tools

Apart from the tools specified above in detail, the JDK has matured over the years and has included in itself several other tools. Where some of these tools are no longer used, others offer a far greater deal of capability to the Java Development Kit. Below is a list of some of the tools available for the JDK.

[edit] The apt tool

In Java 1.5 (alias Java 5.0) Sun added a mechanism called annotations. Annotations allow the addition of meta-data to Java source code, and even provide mechanisms to carry that meta-data forth into a compiled class files.

Also starting with Java 1.5 Sun added the apt tool to the JDK. apt works on Java source code. It is an annotation processing tool which digs through source code, finds annotation statements in the source code and executes actions if it finds known annotations. The most common task is to generate some particular source code.

The actions apt performs when finding annotations in the source code are not hard-coded into apt. Instead, one has to code particular annotation handlers (in Java). These handlers are called annotation processors.

The most difficult thing with apt is that Sun decided to use a whole set of new terminology. apt can simply be seen as a source code preprocessor framework, and annotation processors are typically just code generators.

See also: Getting Started with the Annotation Processing Tool (apt)

[edit] The appletviewer tool

Java applets require a particular environment to execute. Typically, this environment is provided by a browser with a Java plug-in, and a web server serving the applet. However, during development and testing of an applet it might be more convenient to start an applet without the need to fiddle with a browser and a web server. In such a case, Sun's appletviewer from the JDK can be used to run an applet.

[edit] The javah tool

A Java class can call native, or non-Java, code that has been prepared to be called from Java. The details and procedures are specified in the JNI (Java Native Interface). Commonly, native code is written in C (or C++). The JDK tool javah helps to write the necessary C code, by generating C header files and C stub code.

[edit] The extcheck tool

extcheck also appeared first with Java 1.5. It can be used prior to the installation of a Java extension into the JDK or JRE environment. It checks if a particular Jar file conflicts with an already installed extension.

[edit] Security Tools

The JDK comes with a large number of tools related to the security features of Java. Usage of these tools first requires study of the particular security mechanisms.

The tools are:

keytool
To manage keys and certificates
jarsigner
To generate and verify digital signatures of JARs (Java ARchives)
policytool
To edit policy files
kinit
To obtain Kerberos v5 tickets
klist
To manage Kerberos credential cache and key table
ktab
To manage entries in a key table

[edit] The native2ascii tool

native2ascii is an important, though underappreciated, tool for writing properties files -- files containing configuration data -- or resource bundles -- files containing language translations of text.

Such files can contain only ASCII and Latin-1 characters, but international programmers need a full range of character sets. Text using these characters can appear in properties files and resource bundles only if the non-ASCII and non-Latin-^1 characters are converted into Unicode escape sequences (\uXXXX notation).

The task of writing such escape sequences is handled by native2ascii. You can write the international text in an editor using the appropriate character encoding, then use native2ascii to generate the necessary ASCII text with embedded Unicode escape sequences. Despite the name, native2ascii can also convert from ASCII to native, so it is useful for converting an existing properties file or resource bundle back to some other encoding.

native2ascii makes most sense when integrated into a build system to automate the conversion.

[edit] RMI Tools

[edit] Java IDL and RMI-IIOP Tools

[edit] Deployment & Web Start Tools

[edit] Browser Plug-In Tools

[edit] Monitoring and Management Tools / Troubleshooting Tools

With Java 1.5 a set of monitoring and management tools have been added to the JDK, in addition to a set of troubleshooting tools.

The monitoring and management tools are intended for monitoring and managing the virtual machine and the execution environment. They allow, for example, monitoring memory usage during the execution of a Java program.

The troubleshooting tools provide rather esoteric insight into aspects of the virtual machine. (Interestingly, the Java debugger is not categorized as a troubleshooting tool.)

All the monitoring and management and troubleshooting tools are currently marked as "experimental" (which does not affect jdb). So they might disappear in future JDKs.

[edit] The Jar tool

Jar is short for Java archive. It is a tool for creating Java archives or jar files - a file with .jar as the extension. A Java archive is a collection of compiled Java classes and other resources which those classes may require (such as text files, configuration files, images) at runtime. Internally, a jar file is really a .zip file.

[edit] The jdb tool

Jdb is short for Java debugger. The Java debugger is a command-line console that provides a debugging environment for Java programs. Although you can use this command line console, IDE's normally provide easier to use debugging environments.

[edit] The Javadoc tool

As programs grow large and complex, programmers need ways to track changes and to understand the code better at each step of its evolution. For decades, programmer have been employing the use of special programming constructs called comments - regions that help declare user definitions for a code snippet within the source code. But comments are prone to be verbose and incomprehensible, let alone be difficult to read in applications having hundreds of lines of code.

Java provides the user with a way to easily publish documentation about the code using a special commenting system and the javadoc tool. The javadoc tool generates documentation about the application programming interface (API) of a set of user-created Java classes. javadoc reads source file comments from the .java source files and generates HTML documents that are easier to read and understand without looking at the code itself.

[edit] The javap tool

Where Javadoc provide a detailed view into the API and documentation of a Java class, the javap tool prints information regarding members (constructors, methods and variables) in a class. In other words, it lists the class' API and/or the compiled instructions of the class. javap is a formatting disassembler for Java bytecode.

See other tools for all the tools that are bundled with Sun's JDK.