Compiling and Running a Java Program Using the Command Line

  1. Optional: Create a new directory where you will store the files for the program. This step is more important if you are using packages. For lab 1, you don't need to worry about packages. But, if you do use packages, you must create a directory tree that matches your package name. For example, if you have a package named "networks.webserver", all files in that package MUST be in a directory named "webserver" that is in a directory named "networks".
  2. Open up your favorite text editor (I like emacs) and write your code. For your first lab, copy and paste the code from the tutorial web page. But, remember that you have a couple of things to modify. You may not be able to fully understand all of the code. We will talk more about sockets in class. But, you should be able to understand enough to modify the necessary portions for the assignment. Use this as an opportunity to learn more about Java!
  3. Save the file. Important: The name of the file must be the class name with a .java extension. If you are writing a class named Test, your file MUST be named For your first lab you should save the code as "".
  4. cd into the directory where your code has been saved. Note: I am assuming that you are not using packages..
  5. Make sure that the java compiler is in your PATH. If you type "javac" and the command is not recognized, the compiler is not in your path. Setting the path depends on a number of things. Let me know if you need help with this.
  6. Compile by typing javac filename where filename is the name of the file. For example, javac If the compiler doesn't spit out any errors, it should have generated a ".class" file. Do an "ls" (or "dir" if you are using windows) to verify the class file is there.
  7. Run by typing java classname. For example, java smtpClient. Note: If you are using multiple classes in your program you will need to compile all of the files and then run the program by using the classname of the class that contains your main method.
  8. You should see the output. For lab 1, make sure the output looks correct. To test, you may want to send yourself an email first to make sure that your mail is being sent correctly. Then, modify the code to send the email to my address. Also, take some time to try to figure out what the code is doing. You may not understand every line of it, but don't just copy and paste. I expect you to take some time to understand what's going on.

Sami Rollins