In this lab you will learn to:
There are two ways to compile and run a java program, using the command line interface, and using an IDE. We are going to start by using the command line interface. (In simpler terms, using DOS). First, you need to download a file called "HelloWorld.java". You can do this as follows:
Now open the command line interface by selecting the Programs from the Start menu, and then selecting the MS-DOS Prompt
This is called the command line. Now you need to change to the directory where the file is located (i.e., C:\files. You can do this by typing:
cd c:\files
on the command line. Next, type
dir
This shows a listing of what is in the current directory (i.e., folder). You should see a file named HelloWorld.java in the directory.
Next we will set the path variable to assure that DOS can locate the java compiler. To set the path variable do the following:
set path=C:\jdk1.3.1\bin;%PATH%
Now we are ready to compile the file. To do this, type
javac HelloWorld.java
Javac is the java compiler. It translates the program into bytecode. Now type the dir command again. This time you should see another file called "HelloWorld.class" that was created by the compiler. (If it is not there, start the lab from the beginning. If you still can not get it, ask your lab instructor for help.) The class file is the file that is run by the java interpreter. To run the file, type
java HelloWorld
Your MS-DOS should now look like the one below:
Now you will use an IDE instead of the command line interface. An IDE
can simplify your life by integrating a file editor, the java
compiler, and the java interpreter into one package. IDE
stands for "Integrated Development Environment". For this course, we
use a free IDE called JCreator.
JCreator requires the latest JDK from Sun. You can download the JDK
from Sun at http://java.sun.com.
Now start JCreator as follows and make sure that your window looks like the one above:
If your window does not look like the picture above, choose the File menu and then select Close Workspace. Now it should look like the one above. (If not, ask your lab instructor for help)
Now, we need to create a new project:
Make sure that Empty Project is selected and in the Location field type C:\files\ to save the project in the files folder on the C drive.
For the Project Name, type FirstHello. (We will
almost always start with an empty project for our labs, but feel free
to experiment with the other options when you have some free time.)
Now hit OK. Your JCreator window should now look
like this:
Now choose the File menu again and once again choose
New. This time the Files tab should be selected when
the window opens. Make sure that Java File is
selected and again type FirstHello for the
Filename.
Finally, our window looks like this:
Today, we will be making a console application which will
open a MS-DOS window when it runs. Now type (or copy and paste) the
following lines of code:
public class FirstHello {
public static void main(String[] args) {
System.out.println("Howdy Folks");
}
}
Now, let's examine the code a little bit.
public class FirstHello {
...
}
This is the definition of a class named FirstHello. For now, remember that the name after public class should always be the same name as the file. (If it is not the same name, then it will not work correctly.)
public static void main(String[] args) {
...
}
This is the method where everything in a console application starts. (Applets do not have a main method, their starting point is elsewhere.) We will get into the details of the public static void and what is in the ()'s later in lecture.
Finally the line:
System.out.println("Howdy Folks");
This is where we will focus our attention today. This line of code prints to the console. We are calling the println() method of the object called System.out (That was a mouthful, wasn't it!). In the call to the println() method, we put "Howdy Folks" in the parentheses. This is what will be printed to the console.
Now let's get back to JCreator. We now want to compile our project. You can do this in one of several ways. You can select Compile Project under the Build menu, or you can just press F7 on your keyboard, or you can press the compile button as pointed out below.
The bottom of JCreator should look like the one below. If you have any errors, check your typing and see that it is typed exactly as above. If you still have trouble, ask your lab instructor for help.
We are now ready to run our program and see what happens. As with compiling, there are several ways of running the program. You can select Execute Project under the Build menu, or you can press F5 on your keyboard, or you can press the Execute button pointed out below.
If your window looks like the one below....CONGRATULATIONS!!! You have created a Java program.
Now, let's look around a bit. First, using Windows Explorer, open the project folder that you have just created and look at its contents. Notice that you see the java source code file, FirstHello.java, and the FirstHello.class file containing the compiled version of that program along with 3 other files. These 3 other files were created by JCreator. The file with the extension .jcw (i.e., FirstHello.jcw) is the "workspace file". You can open a project in JCreator by double-clicking on the workspace file for that project.
Now, let's play with the code. Change the following, one at a time, in your program. Speculate as to whether these changes will produce errors, and, if there are errors, look at the descriptions in JCreator. If you double-click on the starting line of an error message in the output window, JCreator will place a red arrow in front of the line of code where the error occurred in your program---be sure to try this.
Now you need to copy your Initials project folder to a floppy disk which you should insert into the A: drive. In the future, you can save your homework project folders on disk if you want to work on them both at school and at home. Remember to save the entire project folder on disk as opposed to just the java source code file---this will make it much easier to work in JCreator. If you don't have a floppy disk, your instructor should be able to provide one that you must return after you have completed this lab. There are many ways to copy files and folders in Windows, below is one set of instructions. Feel free to use a different procedure if you prefer.