Site Overlay

Karel 2023 Reloaded

About 7 years ago, I wrote a post how to run Stanford University’s Karel robot which is used in their highly popular online CS106a course. While it’s been 16 years now since the course page has last been updated and Stanford switched from Java to Python for their introductory computer science lessons, the classic Java lectures still remain one of the best free online lessons to learn the basics of programming.

As Java and Eclipse have evolved, not only will Stanford’s original karel.jar file refuse to run in modern Eclipse but also the workaround I suggested in my post 7 years ago. Time for a new “How to run Stanford’s Karel in modern Java IDEs.”

Making good old Karel fit for modern Java environments

Prepare your environment

There is a lot of confusion what exactly is needed to run the legacy karel.jar in today’s modern Eclipse environment. Therefore some clarifications regarding the Java prerequisites and the Karel versions:

Java Prerequisites

  • Do I need to have the whole JDK installed or is the JRE sufficient? – The JRE is sufficient to run Stanford’s Karel code.
  • It is not necessary to install additional legacy Java JREs like Java 1.6 (which anyway can no longer be installed on today’s current operating systems and is no longer offered for download afaik).
  • The (at the time of writing) current Java 17 version works. My Ubuntu system ships with that version by default. So if you are on a Linux Ubuntu system or some similar distro, you don’t need to install anything.
  • Windows users might need to download and manually install a Java JRE (in that case Oracle still offers the Java Version 8u JRE – which dates back to 2014. Oracle offers no more updates for Windows machines for non-commercial users.)

Karel version

The more important thing is to get hold of a recent Karel version. The karel.jar which comes with the original assignment files on Stanford’s SEE course page, has obviously been designed to work with Java 1.6, which is no longer available for download. Even if you downgrade to the oldest available Java legacy version (1.8 in my case), this jar file is not going to work.

The most recent jar I could find from Stanford dates back to 2010 and has been published on Eric Robert’s personal Stanford page. Download the Assignment1.zip file from that page. In case the page is no longer available, I have included a copy on my server.

Create Eclipse project from Assignment zip-file

  • Open your Eclipse IDE and click File / Import in the menu bar.
  • In the dialogue that opens, from the General section, select Existing Projects into Workspace and click Next.
  • Activate Select archive file and navigate to the Assignment1.zip file you downloaded from the Stanford Server and click Finish. Eclipse will automatically unzip the file and create a project structure from the content. Your content of your Project Explorer in your Eclipse main window should look like this now:

Replace the outdated karel.jar file in your project

In case you imported your assignment file from the official Stanford CS106a course page, the project files in your Eclipse IDE will contain an outdated karel.jar file which will not run with modern Java JREs of version 8 or higher. You can check this by right clicking on karel.jar in the Project Explorer list of your Eclipse IDE and then choosing Properties. The outdated karel.jar will have a size of 274,627 bytes and a creation date of April 6, 2006:

Outdated karel.jar shows a file size of 274’627 bytes and a creation date of 2006.

Replace this by the karel.jar file which you have extracted from the 2010 Assignment1.zip file which you downloaded from Eric Robert’s personal Stanford page. It is sufficient to open your file manager of your OS, copy the 2010 karel.jar file from where you extracted the Eric Roberts Assignment1.zip file (make sure it’s the file with 378 kB size) and paste it over the outdated karel.jar file in your project directory:

When you return to your Eclipse IDE window, you can confirm that the file was actually replaced by right-clicking karel.jar in the Project Explorer pane and checking the Properties:

Most recent karel.jar version swapped into our project files now shows ~ 368 kB

You can now start programming in your IDE.

Add your code to the first assignment problem

If you expand the (default package) section in the Project Explorer of your IDE, you will see the templates for the coding challenges that Stanford has prepared for you:

The first problem of the assignment in the in the student handout is CollectNewspaperKarel.java (read page 2 of the handout to find out what your program is expected to do).

So we double-click on CollectNewspaperKarel.java in the Project Explorer which opens the prepared programming template in the editor window. There is a commented line which says // Your code here. Guess what you should do now…

So why not read through the problem description in the handout and put some code to give Karel a spin?

Now here’s our code which should solve the problem:

Running your code

  • In the menu bar of your Eclipse, click on Run then Run Configurations
  • In the window that opens, Click on Java Application (not Java Applet) from the left list
  • Make sure that the Main class entry in the right pane is stanford.karel.Karel
  • Select the Arguments tab in the right pane and enter code=CollectNewspaperKarel into the Program arguments box.
  • Click Run, then confirm with OK in the Save and Launch dialog.
  • Unsatisfactorily, in the program that opens, you will need to manually load the “world” for the corresponding Karel world. Click Load World, then select CollectNewspaperKarel.w from the File dialog and click Open.
  • Click Start Program and see if your code works.
Yippie! It’s finally working!

Will it also work on Windows?

It does. Here’s my testing setup:

  • Windows 11 inside a VirtualBox
  • Manual installation of Java 8 JRE (probably not even needed because I later found out that the Eclipse installer adds a fully loaded OpenJDK Java 17
  • Installation of default Eclipse for Java Developers edition via Eclipse’s Oomph Installer
  • Directed Eclipse to the workspace with the Java code created above and ran the code.
  • Unlike under Ubuntu, Eclipse seems to load the appropriate Karel world automatically.

Here’s the result: