Start Java

Source Files

  • When writing your programs, you will create a source file.
  • A source file is a text file that contains your Java code.
  • The name of the file must match the name of the class, and must have a .java extension.
  • We will start by vi to complete the programs described below.
  • In Linux, create directory where you will work on your Java code. Note: this directory should be under your HOME directory.
  • Download this source file so we can play with it a bit: RandList.java. (Right-click in your Linux browser and save it in directory you just created.)

Compiling

You must first compile your programs, using the javac command:
javac <file>

The file name must end in .java

Try running the file command on the RandList Java code - what does it say?

When you compile, it creates a .class file. You can use ls to see the new file.

To run the RandList program, type java RandList.class (try it now; more soon!).

Applet versus Application

An application is a stand-alone program that runs on your machine.

An Applet is a program that is executed over the Internet, typically by a browser.

The browser must have the Java plug-in, so for consistency we will use a tool called appletviewer.

In either case, since it is browser-based, you must give it an HTML file to run the Applet.

Here is a basic HTML file for running an Applet (you may copy and paste, please put in your own name):

<html>
<head>
<title>Hello World by Your Name</title>
</head>
<body>
<p>Hello World Applet

<applet code="HelloWorld.class" width=300 height=200>
</applet>

</body>
</html>

Note that the HTML file references the Applet, but it uses the file name that contains the .class file, which in this is case "HelloWorld.class".

What file did this HelloWorld.class come from? And what filename should this HTML code be in?

Running

To run your Java application, use the java command:
java <class-name>
 
To run your Java Applet, first write an HTML file, and run it with Applet Viewer:
appletviewer <file>

Note that appletviewer  doesn't require a file extension, but by convention it should be .html.

Error - Running the HelloWorld Applet with the java command

  1. Running HelloWorld with the java command will generate and error because it is not an application.

    java HelloWorld

  2. One difference between an application and an Applet is that applications must have a main(). Our Applet does not, so we see the error message:
    Exception in thread "main" java.lang.NoSuchMethodError: main
    

Hello World as an Application

Edit HiWorld.java in your Linux environment and type in this code (note, you cannot copy/paste it, so practice by typing):


Play around and try these errors:

Error - Compiling hiworld.java instead of HiWorld.java
Error - Compiling HiWorld instead of HiWorld.java
Error - Running HiWorld.java instead of HiWorld
Error - Running HiWorld.class instead of HiWorld

Hello World as an Applet

Type in the code for the HelloWorld Applet (note, you cannot copy/paste it, so practice by typing):

Compile and run it.
Play around and try these errors:

Error - Running HelloWorld with java instead of appletviewer
Error - Running HelloWorld.java  instead of HelloWorld.html
Error - Running HelloWorld.class  instead of HelloWorld.html

Java Assignments:

  1. Update the HiWorld program and turn this newer version in.
    Using two statements, have it print out "From <yourName>" after printing "Hello World!", but with your name on the same line.

  2. Update the HelloWorld Applet and turn this newer version in.
    Using two statements, have it print out "From <yourName>" after printing "Hello World!", but put your name on the line below (line up the left-hand side of the 2 strings).