486 Assignment #1: Hello World in Java

 

Due in TWO DAYS - Thursday 8/26/04. 50 pts.

 

Directions: hand in hard copy of code and output. If you get stuck, please get help promptly. Note: the following instructions are optimized for the PCs in the lab (Coover Hall  rm. 1318). If you have trouble running it on another system, try it in rm. 1318 to determine if the problem is in the Java installation on the other system, or something you are overlooking in the HW instructions. If you have no account, go to Coover 2101 to get one.
 

Follow these steps to create and run a Java application.
 

  1. Create a Java Source File

Using a text editor, create a file named  HelloWorldApp.java which prints something. Here is a sample "Hello World" program:

/**

* The HelloWorldApp class implements an application that

* simply displays "Hello World!" to the standard output.

* Provided by D. Berleant

*/

import java.lang.*;

class HelloWorldApp {

    public static void  main(String [ ] args) {

       System.out.println("Hello World!"); //Display the string.

    }

}

Make sure you observe the uppercase/lowercase conventions shown above.

Save the file:

Hint: Notepad can work for this, as can most other editors (MS Word, etc.), and more powerful IDEs and other software development tools. If you use a text editor be sure to save the files as text, since other file formats insert control and formatting characters specific to the file format into the file that will confuse the Javac compiler. Also, take care that the editor you are using does not add an undesired suffix to the file (resulting e.g. in “HelloWorldApp.java.txt” or something strange like that).

Possible problem: “I say to save it as HelloWorldApp.java, but it appears in the directory as HelloWorldApp.java.txt. What should I do?”

Solution: Try saving it with  “save as” and type double quotes around the exact file name you want: “HelloWorldApp.java” not HelloWorldApp.java.

Possible problem: “I’m using pfe and when I save it as HelloWorldApp.java it appears in the directory as helloworldapp.java. What should I do?

Solution: Change the file name manually to have the desired case characteristics.

  1. Compile the Source File

Compile the source file using the Java compiler by entering:

javac HelloWorldApp.java

The compiler creates a file named HelloWorldApp.class in the same directory as the Java source file (HelloWorldApp.java). This class file contains Java bytecodes.

Hint: you can do this in an “MS DOS Prompt” window. You can get to the MS DOS Prompt window by running c:\WINNT\system32\cmd.exe directly (as of 1/9/04), or by clicking “Start” then “All Programs” then “Accessories” then “Command Prompt.” 

javac is at c:\Program Files\j2sdk1.4.2_04\bin\javac. (Hint: on other computers, it may be elsewhere, and sometimes multiple java installations will exist of which some do not contain the compiler. To sort through this, you can use the file finder OS service to look for a file named javac.) You can set things up so that the computer can find it there without you always having to type the entire path, by setting the PATH variable. In the MS DOS Prompt window, type: “set PATH=c:\Program Files\j2sdk1.4.2_04\bin” and the computer will now know to look there for the javac compiler program. Alternatively, you could type “c:\Program Files\j2sdk1.4.2_04\bin\javac” instead of just “javac” if you really want to.

  1. Run the Application 

    Run the program using the Java interpreter by entering:

    java HelloWorldApp

    You must be in the same directory as HelloWorldApp.class to do this, because otherwise you would have to type something like, java c:\adirectory\HelloWorldApp, and then Java would look for a class with that odd, long name, which of course you did not define in your source code. At this point, you should see "Hello World!" displayed.

    Now try running it using the debugger by entering:

    jdb HelloWorldApp

    What happens? Run it in this Java Debugger (whence "jdb") environment. Hint: type a question mark ('?') to see the debugger commands.

  2. Do another hello program, which has a simple GUI interface. This is the one to hand in:

    import java.lang.*;

    import javax.swing.JOptionPane;

    public class Hello{

       public static void main(String [ ] args){

          JOptionPane.showMessageDialog(null, "Welcome to CPRE 485");

          System.exit(0);

       }

    }

  3. Comment out the last statement. Run. What is different, and why? Write the answer as a comment at the top of the program.