I decided to follow the Stanford’s computer science open courses and to start with the simple cs106a. This course is an introduction to programming. Even if I do not learn advanced programming techniques, I find it perfect to study Java. It is a piece of Geek culture by itself and, if you need a last motivation, professor Mehran Sahami is really inspirational.
The course starts with a simple entertaining programming environment based on a robot called Karel. You have to move the robot on a grid and accomplish goals. This environment is delivered in the Stanford’s version of Eclipse which us outdated. I already had a recent Eclipse installed on my computer and did not want to change for an older version.
This blog post describes how to make Karel the robot work on any version of Eclipse.
PREPARATION
I use the last version of Eclipse, named Juno (at the time of this blog post has been published), version 4.2.1. Grab the starter code on the course assignment page. This course assignment contains the package to play with Karel. The tricky part is that this package is compatible with a given version of JRE, the 1.6, which is not the last one.
A short reminder of the Java versions you have to deal with:
- as Eclipse is written in Java itself, you need a JRE to run it. In a typical installation, it is the JRE of your computer, the one that is called for running any Java program.
- compiler compliance level, which is the JRE version compatible with the byte code that you generate with the JDK. You can use JDK 1.7 and compile byte code compatible with the JRE version 1.5 for example.
- the version of the JRE that as to be called to run a given project which may be different from the one used to run Eclipse.
So you got the point, the idea is to use JRE 1.7 to run Eclipse, select a compliance level to 1.6 and have the project byte code run by JRE 1.6.
I explain here how I installed on Windows 7 Java and the different versions.
ADD A NEW JRE TO JAVA
The first step is to add a JRE definition in Eclipse. This is done in the menu “Window, Preferences”, and then “Java, Installed JRE”:
Select “Add” and “Standard VM” and finally select the folder of the JRE 1.6.
CREATE AN EMPTY PROJECT
This project will be used to import the course assignments (I use only one folder for all assignments). Select “File, New, Java Project”. A window is displayed with the configuration of your project. The important parameter is the project specific JRE, that should be jre6:
The compliance level of the compiler is automatically decreased to 1.6.
IMPORT THE COURSE ASSIGNMENT CODE
Select the project you have just created, and then go to “File, Import”. The import type is “General, File System”. Browse to the Assignment1 folder and select everything. If this step ran well, you should see something like that:
You are very close to the goal now.
CREATE A RUN CONFIGURATION
The main() class is hidden somewhere in the Karel package. So you have to define how to call the program in the run configuration. Enter the parameters as shown bellow:
The parameters are:
- Main class = stanford.karel.Karel
- Include system libraries = yes
- Include inherited mains = yes
You also have to give a run argument to select the pattern of the grid:
From there, you can check the JRE, which should be jre6:
TAA-DAA!
Finally, run the empty code and here it is:
(personal note: this Karel development environment reminds me LOGO, which I learnt in primary school)
The post Playing with Stanford’s Karel the robot on a standard Eclipse appeared first on Zombie Brainz' Juice.