<<

DEPLOYING A JAVA

To deploy a Java program you need to:

Type it out and save it into a Compile it Execute it

Type and Save You can use any word processing to create, edit and save your Java programs. Seasoned , however, prefer to use an integrated development environment ( IDE ), which is a software tool that combines the functionality of word processing, compiling and executing into one package. An IDE also provides other services such as automatic code indention, text coloring and a that allows you to execute a program step-by-step while inspecting its internal . jGRASP is such an IDE. Since it is implemented in Java, it works on any that runs Java. It is easy to use, easy to learn and includes visualization tools that allow you to better understand the structure of your code and its . For a full introduction to jGRASP, see the website www.jgrasp.org .

Deploying a Java Program Page 1

The image below shows the jGRASP window with the simple HelloWorld application typed onto the workspace. Notice the Browse window (marked by the red rounded rectangle) indicates the folder F:\Java Examples is empty.

Deploying a Java Program Page 2

You must save your application to a file having the same name as the application followed by .java as its extension. Thus, the HelloWorld application must be saved as HelloWorld.java .

The image below shows the jGRASP window after saving the application. Notice HelloWorld.java is listed in the Browse window.

Deploying a Java Program Page 3

Compile A is a software tool that translates a program into (or machine language ) which can be thought of as the “native” language of the computer. The Java compiler (which we call , for short) translates Java into a machine language named . Each Java class is compiled into a separate file whose name is the same as the class being compiled followed by .class as its extension.

Java compiler Java code (javac) Bytecode

HelloWorld.java HelloWorld.class

The image below shows the jGRASP window after compiling the HelloWorld application. Notice the Browse window lists both HelloWorld.java and HelloWorld.class .

Deploying a Java Program Page 4

Execute Once a has been compiled into machine code, the computer is able to execute it. Each computer is built to execute its own special machine code. A physical computer is built of hardware circuitry that directly processes its machine code. A virtual computer is built of software. Its main component is the , which is a software tool that simulates the operation of a computer when it processes its machine code. The physical computer on which the virtual computer executes is called the host machine . The machine code for a virtual computer is called an interpreted language .

Bytecode is an interpreted language that is processed by a virtual computer called the Java ( JVM ).

Example Consider a running a Java app. The Smartphone is the host machine since its circuitry is executing the JVM. The JVM is processing the bytecode, producing the behavior of Java program from which it was derived.

Here’s a diagram of the :

Java Virtual Byte code Machine (JVM) class file

JVM is loaded Host computer Time JVM executes into memory produces Java on the host by operating program computer system output

Deploying a Java Program Page 5

Interpreters are used to increase the portability of a computer language. Ideally, once you write a Java program and compile it, the class file can be executed on any host machine that can run the JVM.

JVM executing under Windows

Byte code in JVM executing Java Program Java compiler class file under UNIX

JVM executing under MacOS

Exercises

1. Enter, compile and run the HelloWorld application using jGRASP.

2. Using jGRASP, edit the HelloWorld application above so that it prints a different message. Save it using a different class and file name, compile and run it.

What file names should you use to save the following Java applications?

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

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

Deploying a Java Program Page 6

What file names should you use to save the following Java applications?

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

6. Enter, compile and run this Java application using jGRASP. import static javax.swing.JOptionPane.*; import static java.util..*;

public class MyApplication { public static void main( String [] args ) { String date = getInstance( ).getTime( ).toString( ); showMessageDialog( null, date ); } }

Deploying a Java Program Page 7