Using Java at the command line

Getting started with Java

The instructions for installing and getting started with Java are embedded in The Hw4 aswsignment page.

Getting to the command line

The command line is simply a means for interacting with a computer (more specifically, its operating system) through a text-based interface.

Windows: To get the Widows command line, choose the "Command Prompt" application from the "Start" menu -- "Accessories." Alternatively, choose the "Run..." option from the "Start" menu, and then enter cmd and the prompt provided. If you take this route, you may want to create a shortcut to the command prompt on your desktop.

Mac OS X: To get the Mac command line, use the built-in Terminal program, which is located in the "Applications" folder under "Utilities."

Getting around the command line

Unlike Scheme and Python, running Java source code is a two-step process.

From within the directory containing the code, you need to first compile the code. This converts it from source (what you wrote) to byte code (which will get executed). For example, if your source file is named OpenList.java you would type

prompt> javac OpenList.java OptnListTester.java
If you are on knuth you will need to run:

prompt> javac -cp ".:/usr/share/junit-4/lib/junit.jar" OpenList.java OpenListTester.java

The compiler may catch errors - in that case, go to the topmost error, read the message, and go back to the source code to fix the problem.

Once the code compiles without error, if you list the contents of the directory, there will now be a file named OpenList.class - this file holds the compiled byte code. There will be one .class file for each class in your original source-code file. To actually run your program, you then type
prompt> java OpenList
where you may replace OpenList with any class name that has a main method. This will then start the main method of that class.

In particular, you will often want to test your programs using the JUnit testing framework.  To invoke your JUnit tests from the command line, do the following:

prompt> java org.junit.runner.JUnitCore OpenListTester

If you are working on knuth, you may need to run JUnit with the following line:

prompt> java -cp ".:/usr/share/junit-4/lib/junit.jar" org.junit.runner.JUnitCore OpenListTester


Windows users: will notice that the javac and java commands won't be "found" by the system by default. The following notes allow you to fix this.  Also, be sure to follow the instructionrs for installing JUnit on the main assignment page.

Making the Windows command-line "see" Java

Once you have the Java development kit (JDK) installed, you will be able to run the compiler javac and the run-time java at the command line. However, to do so in Windows, you'll need to type the full path name to those programs, e.g., C:\Program Files\Java\jdk1.6.0_07\bin\javac. This is a pain, so you should set up the system's "search path" so that you can simply type javac at the command line to compile. To do this, click the Start Menu and go through the following steps:


 - Control Panel
 - System
 - "Advanced" Tab
 - "Environment Variables" button at bottom
 - Under "System variables," scroll to
 - "Path" and select it
 - click "Edit"
 - a one-line textbox will appear
 - go to the right-hand end and paste the following:

;C:\Program Files\Java\jdk1.6.0_07\bin

 Be sure to paste the correct path for your java installation -
there may be different version numbers in the path!
Then enter and click on OK a couple of times to finish.
The semicolon is the windows path separator. That's why it's at the start. After the semicolon is the path to the java compiler (javac) as well as some other tools.

To check that it's working, open a cmd window and type javac -version. You should see javac 1.6.0_07 or whatever your version is and then lots of additional lines.

OK, but the command java to actually run the programs isn't working!

This may or may not be happening to you (it depends on earlier interactions of your computer with Java, some of which you might not have realized because it gets used behind the scenes in lots of web-browsing situations).

However, if javac, the compiler, is working, but java is not working (be sure you're typing java CLASSNAME without the .class extension!), then it's almost certainly the so-called "classpath". The classpath is where Java looks for the classes whose main method it will run. You need to have the current directory in your classpath, and this is done by setting the CLASSPATH environment variable. Here's what to do (almost identical to augmenting the Path environment variable, above:


 - Control Panel
 - System
 - "Advanced" Tab
 - "Environment Variables" button at bottom
 - Under "System variables," scroll to
 - "CLASSPATH" and select it IF PRESENT
 - click "Edit"
 - a one-line textbox will appear
 - go to the right-hand end and paste the following:

;.

 Then enter and click on OK a couple of times to finish.

 - if CLASSPATH was NOT PRESENT
 - click "New" (in the lower, "System variables" part of the window)
 - name the new variable CLASSPATH
 - enter the value

.

  Then click on OK a couple of times to finish.
You'll have to start a new command-line window to have the changes take effect. Within a command-line window, you can view the values of all the environment variables with the set command. (You can change them with the same command - type help set for more.)

Working remotely at the CS lab

You can login remotely to your CS lab account by using a secure shell login program. A very popular one for Windows machines that you can download for free is named Putty. If you have a Mac, you can use the built-in Terminal program, which is located in "Applications" - "Utilities."

Once you're logged into a CS machine, perhaps knuth.cs.hmc.edu, you will be at teh command-line prompt for that machine.

What about editing your code

Again, you may use your favorite text editor. A free editor that has worked well on any OS is JEdit. For some people, the development release has installed more readily than the "stable" release. A much faster (to load) editor is Crimson Editor, but it is for Windows only. Regardless of your specific editor, do be sure to use a text editor, not a word processor - word processors (like Microsoft Word) insert invisible control characters into their documents that will interfere with writing and compiling code.