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
Running
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):
Java Assignments for HW7:
- 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.
- 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).