Developing with leJOS - alpha version

Lawrie Griffiths

Developing with leJOS - alpha version


This section shows you how you can develop programs for the EV3 using leJOS. The basic steps are:
1. Install the development environment.
2. Download the current leJOS source code.
3. Write your first leJOS EV3 program.
4. Run your first program

Install the development environment

leJOS uses the Eclipse development environment. Because of the tools we use make sure it is a recent version. Eclipse can be downloaded from here:
http://www.eclipse.org/downloads/
The best configuration for leJOS development is "Eclipse IDE for Java Developers". Documentation on how to install Eclipse can be found here:
http://wiki.eclipse.org/Eclipse/Installation
You will also need to install a Java JDK. Both the Oracle version and OpenJDK work fine. Version 7 is recommended, but version 6 works with some limitations.

Download the current leJOS source code

The leJOS source code is stored in a Sourceforge git repository. In this section we will obtain a copy of this for local use. If you are not familiar with git you may want to read the following:
http://git-scm.com/book
http://wiki.eclipse.org/EGit/User_Guide

To import the leJOS files:
1. Click on the "File" menu and choose "Import...".
2. For the import source choose "git" and then "projects from git" and click next.
3. Choose "URI" and click next.
4. To obtain a read only copy of the repository enter the following URI:
git://git.code.sf.net/p/lejos/ev3
click next.
5. Ensure that the branch is set to master, and click next.
6. Check that you are happy with the chosen local destination directory (probably best not to change this unless you know what you are doing) and click next.
7. After a short while the git clone should be complete and you can click next.
8. You should now see a list of projects that you can import. Deselect any projects that you do not require, you must have the "DbusJava" and "ev3classes" projects, others are optional. Click finish.
9. All of the projects should import without any errors (there may be warnings), if there are any errors, resolve these before proceeding.
10. The master version of the projects that you have imported is the very latest HEAD version. This may have changes in it that are incompatible with the latest SD card release of leJOS. This particularly applies to the ev3classes project. You should switch that project to the latest tagged version of leJOS. To do this you right-click on the project name in Eclipse, and select Team > Switch To> Other... > Tags, and select the latest tag, which is currently 0.8.0-alpha. You should check the forums to see if any other projects have changes made to them that are incompatible with the latest SD card release.

Write your first leJOS EV3 program

Now we get to create your first EV3 program. First we need to create a new project:
1. Click on "File" and choose "new" then "Java project".
2. Set the project name to be "EV3FirstProgram" then click next.
3. Click the "Projects" tab and then click on "Add", check "ev3classes" then click OK.
4. Ensure that ev3classes is now shown in the "Projects" tab and click Finish.

We now need to add a simple class to the project:
1. In "package explorer" click on "EV3FirstProgram" right click on src and choose "new" then "class".
2. Set the "Name" to be "EV3FirstProgram" check the "public static void main(String[] args)" option then click finish.
3. In the editor window change things until the code looks like this:

import lejos.hardware.Button;
import lejos.hardware.lcd.LCD;

public class EV3FirstProgram {

    public static void main(String[] args) {
        LCD.clear();
        LCD.drawString("First EV3 Program", 0, 5);
        Button.waitForAnyPress();
        LCD.clear();
        LCD.refresh();

    }

}
~~~~~~~~~~~~~~~~~~~~~~~~

Click the "File" menu and then "Save".

Run your first program
----------------------

To run your program on the EV3, it needs to be packaged as a runnable jar file and copied to the /home/lejos/programs directory on your EV3. One way to do this is to use an ant build file. In the future, we will provide an Eclipse plugin to make this even easier.

Your ant build file should be called build.xml. Create it by write clicking on the EV3FirstProgram folder, and select New then File.

Its contents should be:
<project name="EV3FirstProgram" basedir="." default="copy"> <property name="source.dir" value="src"/> <property name="lib.dir" value="../ev3/ev3classes"/> <property name="class.dir" value="build"/> <property name="jar.dir" value="dist"/> <property name="main-class" value="EV3FirstProgram"/> <property name="lejos.home" value="/home/root/lejos"/> <property name="ev3.host" value="10.0.1.1"/>
<path id="libraries.path">    
    <fileset dir="${lib.dir}">
        <include name="*.jar"/>
    </fileset>
</path>

<target name="clean" description="delete old files">
    <delete dir="${class.dir}"/>
    <delete dir="${jar.dir}"/>
</target>

<target name="compile" description="build class files" depends="clean">
    <mkdir dir="${class.dir}"/>
    <javac srcdir="${source.dir}" destdir="${class.dir}">
        <classpath refid="libraries.path"/>
    </javac>
</target>

<target name="jar" depends="compile">
    <mkdir dir="${jar.dir}"/>
    <jar destfile="${jar.dir}/${ant.project.name}.jar" basedir="${class.dir}">
        <manifest>
            <attribute name="Main-Class" value="${main-class}"/>
            <attribute name="Class-Path" value="${lejos.home}/lib/ev3classes.jar ${lejos.home}/libjna/usr/share/java/jna.jar"/>
        </manifest>
    </jar>  
</target>

<target name="copy" depends="jar">
    <scp todir="root:@${ev3.host}:/home/lejos/programs/" trust="true">
        <fileset file="${jar.dir}/${ant.project.name}.jar"/>
    </scp>
</target>

</project>
~~~~~~~~~~~~~~~~~~~~~~~

Copy this to the new file and save it. You may have to change some of the properties at the start of the file, such as ev3.host and possible lib.dir.

If you are using Wifi then ev3.host property should be changed to your Wifi IP address which typically starts 192.168 and is displayed on the menu on the EV3.

The lib.dir property above assumes that ev3classes is in workspace/ev3 and your EV3FirstProgram project is in workspace, where workspace is your Eclipse workspace. (The full name may be something like c:\Users\me\workspace). You can check the location of your projects by right-clicking on their name, selecting Properties. lib.dir can be set to a relative or an absolute path.

To use ant build files, your must have a Java JDK installed. If you installed that earlier, you should be OK, but sometimes ant cannot find the the javac compiler. If you get this problem, look at http://wiki.eclipse.org/FAQ_Why_can't_my_Ant_build_find_javac%3F.

Also, sometimes ant cannot find the scp task used to copy jar files to the EV3, The scp task is provided by the jsch library, see http://www.jcraft.com/jsch/. If running the ant build file gives you a message, saying this is not available, you should download it and add it to Eclipse. The download is at https://sourceforge.net/projects/jsch/files/jsch.jar/0.1.50/jsch-0.1.50.jar/download. To ensure this library is available to ant in Eclipse, go to Windows > Preferences > Ant > Runtime, and add it as an External Jar to Ant Home Entries.

Before you can build your first program, you need to use the supplied ant build files to build the DbusJava and ev3classes projects, which you should have imported earlier. Do DbusJava first and then ev3lasses. Open up the project, right-click or build.xml, and select Run as> Ant Build. DBusJava may fail with character set problems in cross_test_client.java. If this happens, you can delete cross_test_client.java and cross_test_server.java as they are not needed. Alternatively, you can change the character set for these files to UTF8, my selecting the file, and then selecting File, Properties, and Other > UTF8 for Text file Properties. If you prefer, you can set the Eclipse default to UTF8 in Window > Preferences > General > Workspace.

The EV3 should be switched on when you build DbusJava and ev3classes as the new versions are copied to the EV3. You will need to change the ev3,host property for the build.xml files in the DbusJava and ev3classes projects if you are using Wifi. You can either change ev3.host in the build.xml file, or set up a local.properties file for each of these projects and add a property "ev3.host=<ip-address>" to it.

Once dbusjava.jar and ev3classes.jar are built, you can build EV3FirstProgram.

To build the program and copy it to to the EV3, make sure your EV3 is switched on, and then right-click on buil.xml, and select Run As, and then Ant Build. This should compile your program, package it as a runnable jar file and copy it to the EV3.

To run the program, go to the EV3 and select the File menu and find EV3FirstProgram in the files list. Then press Enter to select it, and Enter again to run the program.

If all is well after a short while a message should appear on the EV3 screen. Press any button to end the program, and control should return to the menu.

Make yourself a cup of tea (or a beverage of your choice!) to celebrate!

Congratulations you can now create and run Java programs on the EV3. Be sure to post on the forum and let us know how you are using leJOS!


Discussion

  • Frank Hulsman

    Frank Hulsman - 2014-02-12

    I received the error message stating that files from jsch were missing. I followed the link above... but those need to be compiled and placed in a jar file. At: http://www.jcraft.com/jsch/index.html I was able to download the complete jar file. After downloading, the "Windows > Preferences > Ant > Runtime, and add it as an External Jar to Ant Home Entries." worked!

     
  • Frank Hulsman

    Frank Hulsman - 2014-02-13

    I did not realize that I could use the USB cable to connect to the EV3. When I connected the USB cable, all the ANT files worked.... without using WIFI!

    Hallelujah! I got my first Lejos / Java EV3 program to work!!!!!

     
  • Eric PASCUAL

    Eric PASCUAL - 2014-02-15

    If sshexec Ant plugin is installed, you can add the following target to the Ant build to launch the program directly from your dev station :

    <target name="run" depends="upload">
        <sshexec host="${ev3.host}" username="${ev3.user}" password=""
        command="jrun -cp ${ev3.programs.dir}/${jar} ${main-class}" />
    </target>
    

    It uses the following definition :

    <property name="jar" value="${ant.project.name}.jar" />
    

    since I've replaced ${ant.project.name}.jar by ${jar} everywhere to let me change it easily to something else if needed.

    Using this task has a benefit and a drawback :
    - benefit :
    println's are displayed in Eclipse console since stdout/err are redirected to it by ssh. Nice for debugging
    - drawback :
    Since the graphical menu does not know the program is running, it overwrites what we display. This can be fixed by suspending the the menu process before launching jrun and resuming it after jrun completion.

    This sequence could be wrapped in a small shell script in /home/root/lejos/bin, which would be executed by sshexec instead of jrun, but at this time I've found no way to retrieve the PID of the EV3Menu process, since doing something like "ps | grep EV3Menu" returns nothing because the pattern occurs after the console default line length (I could check this in an interactive ssh session, where grep succeeds only if the window is wide enough to let the "EV3Menu" string be visible). Anybody a clue ?

     
    Last edit: Eric PASCUAL 2014-02-15
  • Cody Henrichsen

    Cody Henrichsen - 2014-02-27

    When will ev3 lejos be more ready for primetime? I am looking forward to doing more development with it but the overhead is a bit much with early students.

     
  • Ali Onur Cinar

    Ali Onur Cinar - 2014-03-25

    Regarding jsch, you can also download it automatically by doing:

    ant -f $ANT_HOME/fetch.xml networking -Ddest=user

    (assuming that ANT_HOME is set to ANT directory)

     

Log in to post a comment.

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:





No, thanks