Java

Contents

Description

Java is an operating-system-independent platform for computing. It consists of a programming language, utility programs and a run-time environment. A Java program can be developed on one computer and run on any other computer with the correct run-time environment. Usually, older Java programs can run in newer run-time environments. Java is rich enough that even very complicated applications can be written with no operating system dependencies; this is called 100% Java.

With the development of the Web, Java has gained in popularity, since the programmer does not know which system the user may be on. There are several ways Java can be used with a Web application.

applet
An applet is a Java object designed to be in embedded in a Web page. The applet is run when when the Web page is opened, and the applet displays in the area of the page set aside by the applet tag. The Java run-time environment is on the same machine as the web browser.
servlet
Web servers can call a Java program called a servlet. The servlet produces HTML that any web browser can display. The Java run-time environment is on the same machine as the Web server.
webstart
The standard Java installation from Sun has a program called javaws that will start a Java application delivered from a Web page. The delivered Web file is an XML file which has all the need information to start and install the webstart applications.

A Java program that can be run from the command line is called a Java application

Contents

Where to find Java

Where can I use Java on campus?

Java is available on Strauss, but the Java installed by the system is not the most current Java. The most common Java commands are:

java Execute a program in the Java run-time environment.
appletviewer View a Java applet in a separate window.
jar Manage a Java archive file.
javac Compile a Java program.

There are several versions of Java, both older and newer than the system Java. You may set up up the files in your account to always give you any version of Java you desire.

Is Java available at the public computing sites?

At the computing sites both Firefox and Internet Explorer have Java run-time support built in, but only applets written to the Java 1.1 specifications can run without a Java plugin. At this time, the Java plugin is not installed in any central computing sites. For a current list of available software, see the computing sites Web page .

How can I obtain Java for my personal computer?

Java comes as part most UNIX operating systems including Solaris and Mac OS X. Sun offers a free fully functional reference implementation for UNIX and MS Windows machines. To either upgrade your installed Java or get a Java for Windows, see the Sun web pages at http://java.sun.com.

Contents

Java instructions

Running a Java application on Strauss

A simple Java application, one that only uses the core Java methods, will come as class file, while a more complicated application will come as a directory of class files or as one jar file.

You can tell a class file by the .class suffix. To run the application, type the command

java application

where application is the file name without the .class suffix.

You can tell a jar file by the .jar suffix. To run the application in a Java archive file, type the command

java -jar application.jar

where application.jar is the file name of the Java archive.

Running a Java applet on Strauss

A Java applet requires requires a browser for display and for the gui elements to work properly. The program appletviewer will act as a simple, Java only, browser for viewing the applet. You use it with an HTML file with the proper applet tags:

appletviewer applet.html

All HTML outside of the applet tag will be ignored; this is really just for viewing the applet. To view the full page with the HTML and Java, copy the HTML file and all supporting class files and/or jar files to a Web server. You can copy them to your public_html directory to view them on the udel.edu server. Make sure you permit all these files to be readable by the www account (group others). Also make sure you only use Java 1.1 methods, since both Firefox and Internet Explorer only fully support Java 1.1.

Using Other versions of Java

To use another version of Java on Strauss, you must set the JAVA_HOME environment variable to the directory which has the version of Java you want to use. Then based on the variable, you set the PATH environment variable to get the correct version when you type the commands and MANPATH to make sure you get the correct manual page when you use the man command. For example:

<55>% java -version 
java version "1.2.1" 
Solaris VM (build Solaris_JDK_1.2.1_04c, native threads, sunwjit) 
<56>% newgrp 
<1>% setenv JAVA_HOME /usr/j2sdk 
<2>% setenv PATH $JAVA_HOME/bin:$PATH
<3>% setenv MANPATH $JAVA_HOME/man:$MANPATH 
<4>% java -version 
java version "1.3.1" 
Java(TM) 2 Runtime Environment, Standard Edition (build 1.3.1-b24) 
Java HotSpot(TM) Client VM (build 1.3.1-b24, mixed mode)
<5>% exit 
exit 
<57>% java -version 
java version "1.2.1" 
Solaris VM (build Solaris_JDK_1.2.1_04c, native threads, sunwjit)

Compiling a Java program

If you have a simple java program which only uses the core Java methods then you must compile it and make a class file. This class file can be run on Strauss or copied to a PC to be run (assuming the Java run-time environment is correct). Here is a very simple text based "Hello World" program:

<1>% cat HelloWorld.java 
/* 
 * A text Java hello world program. 
 */ 

public class HelloWorld{ 
    public static void main( String args[] ){ 
        try {
            System.out.println("Hello "+args[0]+"!");
        } catch (ArrayIndexOutOfBoundsException e) { 
            System.out.println("Hello World!");
        } 
    } 
} 

<2>% make HelloWorld.class
javac HelloWorld.java 
<3>% java HelloWorld 
Hello World! 
<4>% java HelloWorld Web 
Hello Web!

Contents

More help

There are many tutorials on Java programming and many resources on using Java on the Web.

For help in programming, you should start by looking at the demo Java programs that come with the Java Development Kit, jdk. These demo examples come with Java source files and instructions on how to run them. There are several versions of jdk on Strauss. To get a listing of all the demo directories, type the command:

ls /usr/jdk*/demo

The examples in the applet directory are written as an applet to be displayed in a browser. There is always an HTML file which you can use to start the applet. You can try any applet example. For example to run the DrawTest demo applet:

firefox /usr/jdk1.2.2/demo/applets/DrawTest/example1.html

You can also use appletviewer or iexplorer as your browser. We do not recommend Internet Explorer, iexplorer, on Unix.

If you want to run the Swing examples in the jfc directory, you will need to use the Applet Viewer, since Firefox's Java does not support Swing. For example, to run the Java2Demo Swing applet:

appletviewer /usr/jdk1.2.2/demo/jfc/Java2D/Java2Demo.html

If you try this with Firefox, you will get the following error in Firefox's Java Console.

# Applet exception: error: java.lang.NoClassDefFoundError: javax/swing/JApplet

Contents