The Java Main Method

In Java, you need to have a method named main in at least one class. The following is what must appear in a real Java program.

 

public static void main(String [ ] args)
{
      UrRobot Karel = new UrRobot(1, 1, East, 0);
            // Deliver the robot to the origin (1,1),
            // facing East, with no beepers.
      Karel.move();
      Karel.move();
      Karel.move();
      Karel.pickBeeper();
      Karel.turnOff();
}

This method must appear within a class, but it can be any class. So here is the above main method enclosed in a class called Temporary. This would normally be in a file named Temporary.java. Your class should implement the Directions interface so that words like North are understood. We also put all of our files into the kareltherobot package so that all features of the UrRobot class are available. So here is a complete version of the file Temporary.java:

package kareltherobot;

public class Temporary implements Directions
{ 
	public static void main(String [] args)
	{
		UrRobot Karel = new UrRobot(1, 1, East, 0);
		// Deliver the robot to the origin (1,1),
		// facing East, with no beepers.
		Karel.move();
		Karel.move();
		Karel.move();
		Karel.pickBeeper();
		Karel.turnOff();
	}

}

For those wanting explanation of some of the Java words here we provide the following somewhat simplified definitions.

A Java package is a way to collect different parts of a large program together logically. The Java system (JDK or Java Development Kit) comes with several packages of its own, but you can create your own. The name kareltherobot was created by the authors of the simulator for Karel J. Robot as a convenient place in which to collect robot programming code.

In Java the qualifier public means that something is available across packages. It is also possible to restrict visibility of names like Temporary to a single package or even more, but here we make it public.

The particular form of main is required by Java. While the following explanation of its parts is not needed now, and probably not especially understandable, you can return to it later when you know more.

In Java, main is a static method. This means the method is part of its class and not part of objects. Robots are objects. They are defined by classes, which are like factories for the creation of objects. In terms of our metaphor for robot programming, a static method is like the instructions read to robots by the helicoptor pilot, not the instructions known by the robots themselves. We will see ordinary (instance) methods in the Chapter 3.

Strings in Java are sequence of characters, like the characters in this sentence. The matched brackets indicate that an array of Strings is required. An array is a certain kind of linear collection of things. The name args is just a name for this array. This name is the only part of the declaration of main that can vary.

The declaration (String [] args) is in parentheses as part of the declaration of main to indicate that when the class Temporary is executed by your computer's operating system, an array of strings can be sent to the program to help with program initialization. Since they are not referenced again in the body of main (the part between braces {...}) they won't be used here. The parentheses, like the rest, are required. There is no space between [ and ].

Braces { and } are used to collect statements into a "block" in Java and in some other programming languages. Statements in Java end with semicolons.

To execute the above program you first need to translate it into a machine readable form using the Java compiler. Then you need to use the Java run-time to execute the machine readable version. When you do this you give the run time the name of the class that contains your main method.


Since the above is a bit arcane, we would like to simplify it if we can. The Karel J. Robot simulation program provided with the book has a built in class named KarelRunner that has a special kind of main. If you use this technique then KarelRunner will always be your main and always be the class you name to the Java run time.

Then, when you want to write a robot program, create a class like the following. It should be called GetBeeper.java, since it defines a class called GetBeeper. In Java the name of a public class is in a file with the same name and java for an extension. The name of the file and the name of its public class need to match. The name you give to your class and its file should probably be related to the problem you are trying to solve. Here we are trying to get a beeper. Also, in Java, the convention is that class names begin with a capital letter and method names do not.

package kareltherobot;

public class GetBeeper implements RobotTask
{ 
	public void task()
	{
		UrRobot Karel = new UrRobot(1, 1, East, 0);
		// Deliver the robot to the origin (1,1),
		// facing East, with no beepers.
		Karel.move();
		Karel.move();
		Karel.move();
		Karel.pickBeeper();
		Karel.turnOff();
	}
}

Your class needs to implement RobotTask so that the other class, RobotRunner knows how to interpret this and run the task method here. Note that task here is NOT a static method.

In the book we often speak of the "main task block." You can interpret this to either mean a static main method as in the first example above, in which your class implements Directions, or as a task method in some class that implements RobotTask. Either will serve, though the latter is a bit easier to use.


Compiling and Executing Robot Code

Once you have a complete Robot program (or any Java program) you will want to execute it to see its effect. This is a two step process. First you must "build" your program and then "execute" it. If you have a Java environment like Eclipse or JBuilder, you create a "project" in the environment and add your files to it. This will include any code you write as well as the robot simulator code that you can get from the authors. Then it is just a matter of pushing buttons or selecting menu options to build and execute.

However, if you do not have an environment to work with then you need to use the JDK (Java Development Kit) from Sun Microsystems. You can get this from http://java.sun.com. You create your robot programs with a text editor and make sure that the file names end in ".java". The build step is then carried out by the "Java compiler" called javac. A command to do this would be something like

javac -d . -classpath .;KarelJRobot.jar GetBeeper.java

This assumes you are working in a Windows environment and have established a working directory with all of your files. On a unix system (including Macintosh OS X) the semicolon would be replaced by a colon. This command is typed into a command window on your computer.

If there are no errors in your program then (for robot programs) a new directory will be created called kareltherobot and it will contain the machine readable versions of your program in one or more "class" files.

Once you have done the above, you can execute your program with the "Java Virtual Machine", java. The command would look like this.

java -classpath .;KarelJRobot.jar kareltherobot.GetBeeper

The command is case sensitive. You may be able to abbreviate classpath in the above with cp instead. This latter instruction tells java to look in the kareltherobot directory for the file GetBeeper.class and execute the main that it finds there.

All of the above is needed and must appear just about as shown. In particular you need the -classpath option followed by what is shown above as well as the -d option followed by a space and a period in the compile instruction.

In the next chapter we will see that it is possible to have many files to compile. You can do this all at once just by including them all where we have shown GetBeeper.java. Just include a list of filenames in the javac instruction separated by spaces.

In the java command, however, you always name just one class, the one that contains the main that you want to execute.

Note that I assumed in the above that the simulator for robot programs is called KarelJRobot.jar, which is true when this is written. Jar stands for "Java archive". It must be included both when you compile and when you execute (or "run") your robot program. For Java programs other than robot programs, you don't need this file.