Using Karel J. Robot in Various Environments

This is new material. If you find it misleading or wrong, please let me know so I can fix it.

No IDE | Eclipse | Codewarrior | JCreator | JBuilder | BlueJ

 

Using Karel J. Robot Without Benefit of any IDE

This works on most systems except Macintosh OS 9. You will need an editor that lets you create text files and name them as you like. Note Pad in Windows is a poor choice since it won't let you easily avoid the .txt extension. Java files need to end in .java. Text Pad (http://www.textpad.com/) is good and cheap. BBEdit (http://www.barebones.com/) on the Macintosh is good. There is a free lite version. Emacs (http://www.gnu.org/software/emacs/emacs.html) on unix is good. There are lots of choices.

Create a directory/folder for your work. This should NOT be the JDK directory/folder. In fact, for for the safety and preservation of your machine, don't put any code it the JDK directories except the original JDK distribution files. The directory you create in this step is called the "working directory"

Create a path variable for your system, if necessary, so that the system can find the JDK directory from anywhere, or at least from your working directory.

Put the KarelJRobot.jar file in this working directory. Put world files in this directory also. It may be necessary to put the four supplied gif files here as well. Put your own java files in here too. To avoid a lot of clutter, put all of your java code into packages. For Robot programs, I suggest using the kareltherobot package. Note that the .class files will then go into subdirectories of the working directory, but the source files can stay in the working directory itself.

When you want to work cd (or otherwise move) to your working directory and open your editor. Create a new file with a .java suffix in the working directory or open an existing one there and make what additions and changes you like. Save the file.

Compile your work from a command line (terminal) window. Again, in this terminal window, make sure you are "in" the working directory. CD or otherwise move to it if necessary.

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

If there are errors, fix them in the editor and compile again. If this instruction doesn't work, your path is not correctly set up. On unix systems, replace the semicolon with a colon. Don't forget the -d option with a period after a space. This gets your class files placed correctly.

When it compiles without error you can run it. If it is a Robot Program and contains your class has a main, you can run the main in that class with:

java -cp .;KarelJRobot.jar kareltherobot.MyClass

This is case sensitive, even if your system is not. If you are working on unix (including Mac OS X) replace the semicolon in the above with a colon instead. Use your own class name, of course. Your program should run. Run time errors will be reported in your terminal window. Note that when you run a program, the name of the package is a necessary prefix to the name of the class, BUT the .class extension is not specified. The period in the classpath (-cp) is necessary and says that the system can begin its look for the class to use in the current directory. You can only run a java program from a directory on the classpath and the classpath has to contain the root of the package hierarchy you use. Here that is just "kareltherobot". For some versions of the JDK you need to spell out the word classpath. On others you can abbreviate cp.

You may have an editor that lets you automate the commands above. Look for such an option.

If you have Windows or unix, use a batch file or a shell file to automate the rather messy commands above.


Using Karel J. Robot with Eclipse (http://eclipse.org)

Eclipse is a professional, free, and open source development environment with many capabilities. It is written in Java and so runs nearly everywhere (except Mac OS 9). To use Eclipse you must create a project for each program, though a program may consist of many files. A project contains all the necessary files and it manages the compiling and running of your code itself.

Download and install Eclipse from the web site (above).

Eclipse manages its own files and will make copies of the files in the download directory as necessary. Unzip the distribution file in a place of your choice and then open Eclipse. The Left Hand pane of the window should show the Hierarchy View. Right click in this pane and select New > Project. Select Java project in the dialog and then click Next. Give your project a name. If you want to also use the Test Infected framework click Next again (otherwise click Finish). To use Test Infected, click the Libraries Tab and then the Add External Jars button. Navigate to the plugins directory of your Eclipse installation and select the org.junit... folder (currently org.junit.3.8.1, but this may change). Inside that folder select junit.jar and click Open. Then click Finish.

NEXT find the window in your operating system that you used to unzip the distribution and drag the KarelTheRobot.jar file over the Project icon in the Hierarchy pane and drop it there. Alternatively, you can add this as an external jar just as you did the junit.jar above. Only the navigation will be different.

NEXT, right click again on the new project file (with the name you just created). Select New > Package in the popup menu. Give this package the name "kareltherobot" without the quotes, and with no capitalization, all one word. You will create your classes in this directory.

You can drop existing java files onto this package icon provided they have a package kareltherobot; statement at the beginning, of course.

You can also create new classes. Right click on the package icon and select New > Class. Give your class a name and a superclass. You may want kareltherobot.ur_Robot, perhaps, if you are creating a class of robots. You can browse to the class with the Browse button. That opens a window that has a search field at the top. Type the initial part of a class name here and it will show all of the possibilities that begin that way. Back on the Class naming dialog, don't forget to fill in any needed interfaces. You can browse for these also. Directions is frequently needed. (BTW, Enclosing type is for creating inner classes.) The three check boxes at the bottom let you add a main automatically if your class needs one as well as putting in stubs for inherited abstract methods and superclass constructors. This is helpful for robot classes, of course, where you need the constructors.

Once you have your Java files you can build from the Project menu and run from the Run menu. Errors are shown in the Tasks pane at the bottom, which becomes the Console when you are actually running.

Eclipse has many other options (Window Menu) including one to automatically build when you save a file.


Using Karel J. Robot with Codewarrior (http://www.metrowerks.com/)

Codewarrior is a professional quality development environment that is inexpensive for education and very complete. It runs on many platforms and supports many languages. If you want to do Java on Macintosh OS 9 this is about your only choice, except for the online version in the JJ system. To use CW you create a "project" for each program though a program may consist of many files. A project contains all the necessary files and it manages the compiling and running of your code itself.

Create a directory/folder in which to put the files from the distribution. Unzip the distribution file here. Your project files can be in this directory or anywhere else you like. To get started on a new program/project, open Codewarrior and select New from the File menu. Select Java Stationery in the dialog and give your project a name. You can navigate to the location in which you want the new folder to be created. Codewarrior likes to create a new folder for your project. In the next Dialog you can select Java 1.1 (or other, but this is sufficient). and then Java Application (or applet if that is what you want). CW will create a new project window for you that contains listings of all your files. If your system supports Drag and Drop, then drag the KarelJRobot.jar file to this window. Otherwise select Add Files from the Project menu and add it that way. Add any other files you want to the project similarly. If drag and drop works you may also be able to drag from one project window to another.

In addition to the window, CW has created a new folder for you as suggested above. On a Macintosh (OS 9) you may need to put the four gif files into that folder. Otherwise I don't think you will need them ( there are copies in the jar, actually).

CW has also added a file named TrivialApplication.java to your project. You can either discard this, or edit it, renaming it as appropriate. Remember that Java expects the file name and its single public class to have the same name. So if you edit it you may need to Save AS... so that you can change the name. You can also create new files and add them to your project.

Next, you need to tell CW what your main class is. In the project window there is a set of buttons near the top. One of them (looks like a page with wings to the left) brings up the project options (not the same as IDE preferences from the Edit menu). It is also found at Java Application and Release Settings in the Edit menu. In the project options select Java Target and fill in the name of your main class. It is TrivialApplication by default and you need to edit this. Remember also, that the name of a class includes its package structure, so if KarelMain is in package kareltherobot, then you need to enter

kareltherobot.KarelMain

into the main class field. You can set other options, of course, but if you work on a Macintosh (OS 9) you should leave the Java Output option set to its default "jar file" though the name doesn't matter. This is because OS 9 has a limit on the length of file names and with package prefixes, these can get quite long for inner classes. Files in a jar don't have this restriction. Save your changes to the project options and close the window.

World files should also go in the project folder so that they can be found without a path.

CW has a "feature" on the Macintosh (OS 9) that can be irritating. If you import a world from another system, it won't have its internal type and creator set. In fact it won't even have a "resource fork." If the file's type isn't 'TEXT' the CW won't open it, so you can't read it. If you open it in aother editor that isn't so fussy (BB Edit, say or QUED) you should be able to save it to have its type changed to 'TEXT' so that CW can open it. Note that you don't change the EXTENSION of the file. That has no effect on the Macintosh. you need to change its internal type. The same is true if you use the world builder to create a world file. Java applications don't set the file's type, so you need to do this some other way if you think you need to. The extension can stay kwld or anything you like.

The restrictions mentioned above for OS 9 do not apply to OS X or to other systems.

Note that some versions of Codewarrior seem to require that the Direction class, which defines the actual values of North, South, etc., be referred to by Directions.Direction instead of just Direction. If you get a class not found error on kareltherobot.Direction, try this substitution. The error message should also point you to the line that needs the change.


Using Karel J. Robot with JCreator (Windows) (http://www.jcreator.com/)

JCreator is a simple but relatively complete (inexpensive) Java IDE that runs on Windows. To use it you create a "project" for each program though a program may consist of many files. A project contains all the necessary files and it manages the compiling and running of your code itself.

Create a working directory for yourself and put the files extracted from the distribution zip file there.

JCreator uses workspaces as well as projects. A workspace is a set of related projects. You can have one workspace for all your Karel J. Robot projects, for example. Create a new project from the Project menu. Select the Basic Java Application icon (or Applet if you prefer), and give it a name. For a location you can fill in (or browse to) any directory. I suggest that you browse to the working directory mentioned above first, and then fill in a name for your project. The project stuff will be put in a folder named for your project within the working directory, so all will be easy to find and manage. Say you call it TestProject.

JCreator will also create a file with the name of your project and add it to the project (TestProject.java). You can either discard this file (right mouse button) or edit it if you like. It will have a package statement, that you will want to change, probably to kareltherobot. You can add other files to the project by right clicking on the project icon itself in the left pane or selecting Add Files from the Project menu.

You need to make JCreator aware of the KarelJRobot.jar file. to do this, select Project Settings from the Project menu and click the Required Libraries tab. Click New, and then type a name (Karel) into the top field and click Add (Add package) and navigate to the jar file and select it and click Open. Then OK. Finally, make sure the little box next to the thing you just created (Karel) is checked and click OK again (Phew).

Now you can build your project. there are buttons for this, but you can also do it from the Build menu or use F7.

Before you can run, however, you need to put a copy of KarelJRobot.jar into the project directory. It needs to be both in the project itself, AND in the project directory. Not ideal, but I haven't found a shortcut.

Next, you need to tell JCreator which is your main file. When you Execute Project (F5) from the build menu you will get a dialog. Use it to navigate to the .class file of your main. This should be below your project directory in a directory called classes and furhter in a subdirectory named for your package (kareltherobot). Perhaps it is KarelMain.class in this directory. Select it to run.


Using Karel J. Robot with JBuilder (http://www.borland.com/jbuilder/)

JBuilder is a Java environment that is inexpensive or free for education. It runs on many platforms (Windows, Macintosh OS X, and unix). To use it you create a "project" for each program though a program may consist of many files. A project contains all the necessary files and it manages the compiling and running of your code itself.

Create a working directory for yourself and put the files extracted from the distribution zip file there.

Open JBuilder and select New Project from the File menu. Give your project a name and navigate to the folder in which you want to store the files. I suggest a subdirectory of your working directory. Click Finish.

You will now have a project AND a new directory. Copy KarelJRobot.jar to this new directory along with any world files and any java files you want in the project.

Go back to the project itself and right click on its icon in the left pane of the IDE. Select properties from the bottom of the menu. Click the Paths tab then the Required Libraries sub tab. Click Add. In the new Dialog, click New... Type a name into the top field of the new dialog (Karel), set the Location to be Project, and click Add. In the next dialog, navigate to the KarelJRobot.jar file and select it. Click OK. Back at the earlier Dialog click OK again. Back in a yet earlier dialog select the name you just added to Project and click OK. Back in the properties dialog click OK once again.

From the Project menu you can select Add Files/Packages to add additional files to your project, or you can create them with New from the File menu.

You can then Make your project from the Project menu.

You can then Run your project from the Run menu (or a button). You will need to navigate to the class file in the popup dialog. You can simply enter kareltherobot.KarelMain (or whatever the name of your file is).

If you want to change the main for a project once you have set it, select Configurations from the Run menu.


Using Karel J. Robot with BlueJ (http://www.bluej.org/)

Michael Kölling has provided the following instructions for making Karel J Robot work with BlueJ (updated Aug. 2, 2004)


1 - Create a new BlueJ project. I named mine 'karel', but that doesn't matter much.

2 - In BlueJ, select Tools-Preferences-Libraries. Select 'Add' and add the file 'KarelJRobot.jar' from your Karel installation as a user library to BlueJ. Quit and restart BlueJ. BlueJ now knows the Karel classes and you can refer to them.

3 - In your new 'karel' project, select Edit-Add Class from File... Select the file KarelMain.java from your Karel installation. BlueJ notices that the package declaration is incorrect and will remove it.

4 - You need to declare that you want to use the Karel classes. Open KarelMain, and add the line

import kareltherobot.*;

at the top of the class. You need to add this line to all karel examples you write.

5 - Copy the file 'test.kwld' from the karel directory into the new 'karel' project directory. (This file is used by the KarelMain example class.)

Now compile and run. You can develop your own karel classes in similar ways as the example class.

- First get the Karel Premier version from the previous page

- unzip the file (which creates a karelpremier folder)

- create a folder 'kareltherobot' inside 'karelpremier' and moved all your *.java files into it (this is necessary since they are all declared to be in a package called 'kareltherobot', so they have to live in a directory with the same name to make it a standard java structure)

- move the jar file 'KarelJRobot.jar' outside the karelpremier directory (this is important - BlueJ doesn't like it too much if library files are inside project directories

- start BlueJ, use Preferences/Libraries/Add to add KarelJRobot.jar to the user libraries; restart BlueJ

- use 'Open Non-BlueJ' and selected the 'karelpremier' directory. The project opened fine.

- click Compile; KarelMain and KarelApplet compiled fine; Test and Test2 didn't, because their superclass RobotTester was not found (I didn't find it either, so I decided to ignore the test classes for now)

- run the main method of KarelMain, and (ta-taaa) it seems to work fine.


Back to the Simulator Page.

Last Updated October 10, 2003