Informatik 1 – Schmiedecke

Lab X0-Extension – Java Coffee without Milk and Sugar
Java Development Using the Command Shell

Objective: Learn using the most basic programming environment – the command shell.

In "real life", you will rarely have to write and run Java programs from a command shell. Yet, these basic programming tools will be present almost anywhere, so you can use them to mend or alter Java programs immediately on the machine where they run. So you should know how to use them.


  1. Using the command shell
  2. Java Develoment from the command shell
  3. Having some fun

1. Using the Command Shell.

  1. Rather than clicking on icons, you start programs in the command shell by typing commands which tell the operating system to start them. Open a command shell: you can do this graphically by clicking the icon in the start menu, or you click "run…" and type cmd. The  first command to try is "explorer" – it starts a windows explorer (in a new window, of course).

2.      The most important thing to learn is to navigate through the file system. In a shell, you always work in a current working directory – the "prompt" at the beginning of the line tells you which one. Start a windows explorer and find this directory. The most important navigation commands you need to know are:

·        dir                         list the contents of current directory

·        cd path                change working directory to path (only on the same drive, e.g. "cd C:\Programs\Java\jdk1.6\bin")

·        cd ..                      move up one directory

·        F:                          change to drive F

·        mkdir name        create a new directory

Now navigate through different drives and directories and compare what you see with the windows explorer view. After mkdir, can you see the new directory in the windows explorer?

3.      All files ending with .bat or .exe are executable DOS commands. They can be called from their directory by just typing their name, from other directories you need to type their path and add \name. A .bat file contains a sequence of shell commands. A .bat file is interpreted, i.e. its commands are executed.
Edit a file
letter.bat which contains the following lines:
echo please write a letter to me!

Now run letter.bat from its own directory and from another directory, giving its full path and name, e.g. Z:\X1\letter

4.      Instead of typing long command paths, you can store command paths in the system variable PATH. When a command is typed, all directories listed in PATH will be searched for it (in the given order).
Type PATH to see the present content of PATH. Add the
letter path, using the set command:
    set PATH=%PATH%;Z:\X1

Now try to use simply letter from different directories.

Note: %PATH% represents the old value of the variable. So by setting the Path variable to %PATH%, you make no changes at all. It is usually a good habit to preserve the old state of a system varialble and only append additional values, as we did above.

Linux hint: Setting your path in Linux.

5.      The SET command sets a system variable only for the time a command shell  is active. After closing it, PATH returns to its old value. In order to permanently set a PATH (in your profile, not for other users), you need to use the settings dialog: settings > control panel > system > advanced > environment variables.

2. Java Development in the Command Shell

If you need to install java on your computer, this is what you have to do:

Download from Sun  J2SE Java 6.0 sdk without NetBeans , i.e. the most recent SDK (SoftwareDevelopment Kit), NOT the JRE (Java Runtime Environment). It contains all the command line tools we are using. You also need the API specification for frequent reference, either as a link, or stored on your machine.

After installation, you might have to add the JDK's bin directory to your PATH variable – just try to typing "javac" and find out whether your system knows this command or not.

Translating and executing Java programs

  1. Open any editor (not word!) , type the famous HelloWorld program and save in in X1 as
public class HelloWorld 
public static void main(String [] arguments) 
System.out.println("Hello World!"); 
  1. If you have a typed version of it, save your Eliza program from as in X1.


  1. Test the java installation by typing "javac". If the system does not know this command, set the PATH-Variable to the Java installation's bin directory (look on drive C).

  2. If you get a long error message, try to understand it (always try to understand error messages)! The command tells you it misses parameters, at least the file to be translated. Move to the X1 directory and type

which should produce no errors, and then

   java HelloWorld
Using the CLASSPATH variable.
  1. Move on to HelloFriend or Eliza from the lecture slides. Download  and store it in a new directory Utils.  Compile and run your program.
public class HelloFriend {
         public static void main (String[] arguments) {
           System.out.println("Hello! My name is Java, what is yours?");
           String input = Console.readln();
           System.out.println("nice to meet you, " + input + "!");
It should not work. Do you understand the error message? The compiler has no idea where to look for Console….
Now set the CLASSPATH variable by adding Z:\X1\Utils to its present value, It will tell both the compiler and the interpreter where to look for or Console.class. If you have done so properly, your program should now compile and run.
Linux hint: Setting your classpath in Linux.


Using JAR Files

6.            Java applications work a lot with libraries or compressed archives, so-called JAR files. You can even run Java programs from a JAR file by double clicking on it.. 

7.            The library which we will use a lot is cs101-lib.jar. This library contains a class Console similar to the one we just used.
Delete both Console files, and try to compile your program – it should not work. Now download cs101-lib.jar, store it in Utils, and try to compile your program. It should fail again.

Add Z:\X1\Utils\cs101-lib.jar to your classpath.
Insert the following statement as first line (i.e. before the class header)  into your program and try again – and hopefully it woill work this time:

The rule behind it is that in the classpath, libraires are treated like folders, so the classpath variable has to point to the JAR file. 
Furthermore, the library is divided into packages, the import statement is required to identify the package which contains Console.class.
  1. To look into a JAR file, you can use standard compression programs like WinZip or WinRAR. You will find folders (or packages) with  java- or class files and maybe a so-called manifest file which is needed for running a jar file. (There is also a command line tool called jar – learn about it by typing jar)
    Use either jar or another compression program to look into cs101-lib.jar. Can you find the class Console mentioned above? Is it a java source or a compiled class?

  2. There are special JAR files which contain complete executable java programs. To run such a compressed program from a JAR file, you can either double click it, or use the interpreter with the option –jar:  
    java –jar myprogram.jar
    We will try this is the final section.

3. Having some Fun

  1. Download  javaeyes.jar (you might have to rename it back to .jar after downloading) and execute it through double clicking. If it does not work, it may be that WinZip or WinRAR have registered for .jar-files: Change the explorer's folder options by registering java.exe for .jar. (tools > folder options)

  2. Now run javaeyes.jar from the command shell: move to the directory where you saved javaeyes.jar and call java –jar javaeyes.jar

  3. Not enough yet? Unpack javaeyes.jar, set the CLASSPATH variable accordingly, and execute it. The class to be interpreted is called JavaEyes. ΰ JavaEyes works with packages, which makes things a little more compllicated: Set the CLASSPATH to the folder containing the folder called javaeyes. Call the interpreter from that folder and add the package name javaeyes to the class name JavaEyes: java javaeyes/JavaEyes. (We will learn why later)

That's enough for this time – if you just rushed through, remember to help other folks!   

© Ilse Schmiedecke 2005 – for remarks and corrections: