Structured Programming   «Prev  Next»
Lesson 6 Java interpreter
ObjectiveUse the Java interpreter.

Java Interpreter to Translate bytecode

You have compiled your first Java program, so now you are ready to run it using the Java interpreter. This is a program that translates the bytecode produced by the Java compiler into machine instructions that your computer can understand.
Java interpreter: Runs a Java program. Translates bytecode into machine code for a particular machine.
To run a Java program using the Java interpreter that is included in the Java 2 SDK, you will issue the java command along with the name of the bytecode file of your Java program. For example, to run a Java application contained in the file HelloWorld.class you would issue the following command at the command prompt.

java HelloWorld

Note that a Java bytecode file is also commonly referred to as a class file. Also note that when specifying a class file with the java command, the .class extension of the dclass file is not specified (i.e., it is implied).

Using the Java Development Tools

The Java SDK contains a number of tools in addition to the commonly used java interpreter and javac compiler. This topic gives you an overview of all of the tools in the SDK, and details the most commonly used of those.

Using Common SDK Tools

appletviewer

The Java Applet Viewer command allows you to execute applets outside of the context of a Web browser. To view an applet, pass the URL of the HTML document with the embedded applet to the appletviewer program as an argument. If the document does not contain any applets, the program does nothing. The program will run each applet it finds in a separate window.
appletviewer recognizes applets embedded using the
  1. <object>,
  2. <embed>, and
  3. <applet>
tags.
The following options are available:
  1. -debug Starts the appletviewer using the Java debug program jdb, allowing you to debug applets.
  2. -encoding encodingName Specifies the input HTML file encoding that should be used when reading the URL.
  3. -Jjavaoption Passes the specified javaoption as an argument to the Java interpreter.

Usage: appletviewer [options] url file

jar command

The JAR tool combines multiple files into a single compressed file called a Java ARchive. It uses the same compression algorithm as Zip files, which means that you can open JAR files using any standard Zip utility. Its primary purpose is to facilitate ease of distribution when managing complete applications. For example, you can bundle your entire application into a single JAR for distribution, and others can use it by simply dropping it into their classpath. Alternatively, a JAR file can be set to be self-executing, so that the freestanding application it contains can be run by double-clicking. Applets have made good use of JAR files, because they facilitate all of the classes in an application being downloaded at one time, which saves considerable HTTP traffic, and speeds execution. In addition, JAR files can be signed so that users can determine the application's author.
If you use UNIX, you will find it easy to begin working with the jar command, as its syntax is nearly identical to TAR.

The following options are available for the jar command:
  1. c - Creates a new archive in a file named f, if f is also specified.
  2. u - Updates an existing JAR file when f is specified.

For example:
jar uf myApp.jar myClass.class 
updates the JAR file named myApp.jar by adding to it the class named myClass.class.
  1. x - Extracts the files and directories from JAR file f if f is specified, or the standard input if f is omitted. If input files are specified, only those files will be extracted; otherwise, all of the files in the directory will be extracted.
  2. t - Lists the table of contents in JAR file f if f is specified, or the standard input if f is omitted. If input files are specified, only those files and directories will be listed; otherwise, all of the files and directories will be listed.
  3. i - Generates index information for the specified JAR file and its dependents.
In the next lesson you will be introduced to some basic terminology from object-oriented programming.

Java helloWorld Windows - Exercise

Run the HelloWorld program (Windows)
Java helloWorld Windows - Exercise

Java HelloWorld - Unix - Exercise

Run the HelloWorld pogram (Unix)
Java HelloWorld - Unix - Exercise