Getting started with Java on EV3

leJOS is a Java virtual machine which runs on a LEGO MINDSTORMS control brick. It was originally designed for the RCX system, and later it was ported to NXT and EV3. leJOS is named after the Spanish word lejos, for “far”, and is pronounced like “le-hoss”.

Installing the leJOS software

To begin using leJOS, you need to install the software on your computer. The latest version can be obtained from SourceForge. There is an installer available for Windows (*_win32_setup.exe), but Linux and OS X users will need to download the .tar.gz archive and unpack the files in an appropriate location (for example, /opt/lejos_ev3). You also need to set the EV3_HOME environment variable to the installation directory location; this is done for you automatically when using the Windows installer.

Setting up the EV3 brick

Unlike older versions of MINDSTORMS where new firmware can be flashed directly onto the brick, EV3 requires you to load alternative operating systems onto an external microSD card. This means that in order to run Java programs on the EV3 brick, you will need a microSD card per EV3 brick. It should have at least 2 GB storage capacity, but no more than 32 GB, and not be an SDSX card.

To begin with, you need to format the SD card so there is only a single FAT32 partition. Some new SD cards might already be set up like this, but others may have hidden partitions that could mess up leJOS. For Windows, the leJOS wiki recommends this program. On Linux, I recommend using GParted.

There is no need to load an operating system image on the card, as leJOS sets everything up automatically. All you need to do is place some files in the root of the card. One is the lejosimage.zip file from inside the software installation directory; the other is a Java runtime that can be downloaded from the Oracle Java website. As of May 2015, only Java 7 is available for use with leJOS EV3, and attempting to install with Java 8 will fail. Once these two files are written to the card, simply remove the card from the computer, insert it into the slot on the side of the EV3 brick, turn the brick on, and wait for leJOS to install.

Installing the Eclipse plugin

The leJOS software includes a number of command line and standalone GUI tools for use with the EV3. While it is possible to use these tools to manually compile and download Java code to the EV3 brick, it is recommended to use a plugin for an IDE such as Eclipse. To install the Eclipse plugin, open the Help » Install New Software menu inside Eclipse, and click the Add button to add a new source. Enter leJOS EV3 as the name and the following URL for the location:

http://lejos.sourceforge.net/tools/eclipse/plugin/ev3

Once the plugin is installed, you will need to open up the preferences and select leJOS EV3 from the list. Most of the default settings are fine, but you will need to set the EV3_HOME setting to the software installation directory (see above), and you might want to turn on ‘Run program after upload’.

Writing your first program

In order to begin developing with leJOS EV3 inside Eclipse, you will need to create a new leJOS EV3 project. To do this, visit the File » New » Project… menu and select leJOS EV3 » leJOS EV3 Project. Then create a new class by right clicking on the newly created project in the package explorer and select the New » Class menu. Set the package to something appropriate (such as yourname.ev3 or yourname.ev3.someproject) and remember to select the create main method option. Now you can begin writing Java code and utilizing the EV3 APIs. Try adding this sample code to the main method:

[sourcecode lang=”java”]
LCD.drawString("Hello world!", 0, 4);
Button.waitForAnyPress();
[/sourcecode]

Remember to import the leJOS packages by hovering over the red lines and selecting the import option. To download the program to the EV3 brick, ensure the brick is turned on with the SD card inserted and connected to the computer, and press the green run button or right click on the class name and select Run as » leJOS EV3 Program. The message should display on the LCD screen until you press one of the EV3 buttons, and then the program should exit.

References

Leave a Reply