This tutorial will show you how to install and configure all the software necessary for you to develop Java programs for the LEGO Mindstorms NXT. Java is much more powerful and flexible than the original NXT-G software that LEGO provides. We will be using the Eclipse editor to write the actual Java code and to download this into the NXT. This combination enables you to conveniently write software and to quickly test it on the NXT. Of course there are many other Java editors, but we consider this combination to be particularly convenient. This tutorial focuses on the Windows XP platform, A Mac OS X tutorial is also available. We have two additional steps for Windows Vista. You will work through the following five steps:
- Installing Java on your computer
- Installing LEGO NXT USB driver on your computer
- Install Lejos on your Computer and on your Mindstorms NXT
- Install and configure Eclipse on your Computer
- Write, download and run a “HelloWorld” program
- Extra steps for Windows Vista
The order of these steps is very important. Please go through them one by on. In addition to this tutorial you may also want to consider reading these books that are of particular use:
Maximum Lego NXT by Brian Bagnall view at Amazon.com |
Building Robots with LEGO Mindstorms NXT by Mario Ferrari view at Amazon.com |
Robot Programming by Joe Jones & Daniel Roth view at Amazon.com |
Extreme NXT by Michael Gasperi, Isabelle L. Hurbain, and Philippe E. Hurbain view at Amazon.com |
1. Install Java on your Computer
Download and install Java SE (Standard Edition) JRE (Java Runtime Environment). You do NOT need to install the JDK (Java Developer Kit). We will install Eclipse later, which does already include all the tools you need to write and compile your programs. After the installation you also do NOT need to set any Path or Classpath variables for Java itself. Java will be installed in “C:\Program Files\Java”. However, if you cannot even get Java to work on your machine, enter the path to your java bin directory into the Path variable.
If you already have Java installed you need to verify that you have at least version 5, preferable of the JRE, but the JDK will work as well. We will install Eclipse, which does require at least this version.
2. Install Lego USB driver on your computer
The NXT can be connected to the computer via USB or Bluetooth. The communication via USB is more reliable and faster than via Bluetooth. The later also depends on each specific computer model. USB is in comparison more standardized. You must first install the driver and only then connect the NXT with the USB cable. You do NOT need to install the any software that comes on the Mindstorms CD because we will NOT program in NXT-G (Lego’s own software that is based on Labview). We only need to install the USB driver which is available on the Mindstorms’ website. If you already installed the original Mindstorms software then you do not need to uninstall it, just check if the Mindstorms’ website has an update for the USB driver.
Download the Mindstorms NXT Driver.
Unzip the file and start setup.exe
Click through the installation program
Windows might ask you to restart the computer after the installation. Next, connect the NXT to the computer with the USB cable. Check the correct installation of the driver by checking the device manager. Right-click on “My Computer” and select “Properties”. Switch to the hardware tab. Click on “Device manager“. It should list “Lego Devices => Lego Mindstorms NXT”.
3. Install Lejos on your Computer and on your Mindstorms NXT
Download Lejos. At the time this tutorial was created, version Beta 0.4 was the latest release. By now, newer versions are available and we cannot guarantee that this tutorial will work for the newer versions.
Create a directory called “ProgramFiles” on your hard disk. The already existing directory “Program Files” does contain a space character between “Program” and “Files” that sometimes causes problems in the usage of Java technology. You will later also install Eclipse into the “ProgramFiles” directory.
Unzip the Lejos ZIP file. This will reveal a directory called “lejos_nxj” . Place it in your “ProgramFiles” directory.
We now need to inform Java of the existence of the Lejos library. Right-click on “My Computer” and select “Properties”. Click on the “Advanced” tab. Click on “Environment Variables” at the bottom. Create a new “System variable” by clicking on “New” at the bottom. Do NOT create a “User variable” at the top.
Name the new system variable:
LEJOS_HOME
and give it the value:
C:\ProgramFiles\lejos_nxj
In short, Lejos needs to know where it has been installed. Notice that we now already use the new directory “ProgramFiles” that does not contain a space character. When you are done, click “OK”.
Verify that the new system variable shows up in the list. Next, we need to add the LEJOS_HOME variable to the system variable “Path”. Select “Path” from the list and click on “Edit”. Add
;%LEJOS_HOME%\bin
to the end of the value. The different values are separated by a colon and no colon is necessary at the end of the line.
Now we will test if Lejos has been installed correctly by using the DOS command prompt. Click on the Window’s “Start” button and then on “All Programs => Accessories => Command Prompt”. Type in:
lejosdl
If the result look like the one in the picture below then everything is fine.
We will now replace the original firmware that came with your NXT with Lejos. You only need to do this once. Lejos will then function as the operating system of your NXT. It implements a Java Virtual Machine on which you can run your Java programs. Make sure that you NXT is connected to your computer with an USB cable and that it has been recognized by the operating system. In case of doubt check the device manager again. You first need to bring the NXT to the firmware upload mode. Use a paper clip to press and hold the hidden button. The NXT will then play a soft pulsing sound that signals its readiness.
Type:
lejosfirmdl
into the command prompt and press enter.
Lejos requires Libusb to communicate with the NXT. If this free software has not yet been installed on your computer, it will start the Libusb installation wizard. Libusb allows programs to access any USB device on Windows in a generic way. Make sure that you do NOT run the test application in the last dialogue. In case that LeJOS is already installed on your NXT you will still have to install LibUSB to be able to download your programs. You can find it in “lejos_nxj\3rdparty\lib\libusb-win32-filter-bin-0.1.12.1.exe”. The name may change when a newer version of libusb becomes available.
Once Libusb is installed the Lejos firmware installer will proceed and hopefully complete the process with success. It should look like the image below.
The NXT will now reboot and shortly display the Lejos Logo before changing to the main menu.
Your NXT is now ready to run Java programs.
4. Install and configure Eclipse on your Computer
Download Eclipse IDE for Java Developers. Eclipse itself is written in Java and does not contain an installer (setup.exe). It comes in a simple ZIP file that you should unzip into the previously created “ProgramFiles” directory. You may want to create a shortcut to “eclipse.exe” on your desktop for your own convenience.
When you start Eclipse you will be asked to choose a workspace. The workspace will contain all the files that you create. Again, you should create a directory in a location so that the path does not contain any space characters ” “. In our example we use the directory “d:\programming\workspace”. You will then be greeted with the welcome screen.
We will now create a Hello World program and upload (others call it download) it into the NXT. First, you need to create a new Java project. Click on “File => New => Java Project”. Give the project a new name, but avoid space characters. We called ours “LeJOS_NXJ”
Next, you need to turn this project into a Lejos project. Right-click onto the project and select “Properties”. Select “Java Build Path” on the left and click on the “Libraries” tab. Afterwards, click on “Add External JARs…” and locate the “lib” directory in your “ProgramFiles\lejos_nxj” directory. Select “classes.jar” and press “Open”. The Lejos library will now be listed.
We stay in the properties and move to the “Java Compiler” section listed in the left panel. Check the box “Enable project specific settings” and choose level 1.3 for the compiler compliance level. This will urge the compiler to optimize for an earlier version of Java. This earlier version is more suitable for the NXT since it was developed for embedded systems. It requires much less resources than the latest version of Java. Click on “Apply” and then”OK” to leave the properties.
The next step is to configure Eclipse for downloading the software into the NXT. Click on “Run => External Tools => Open External Tools Dialog…”.
Select “Program” on the left and then click on the “New” icon. Name the tool “leJOS Download” on top. In the “Main” tab, enter the location of the “lejosdl.bat” file that should be in the “lejos-nxj\bin\” directory ny clicking on “Browse File System”. Enter
${project_loc}\bin
in the “Working directory” and enter
${java_type_name}
in the Arguments section.
Now we will create a shortcut for downloading your program into the NXT. Click on the little green “run” icon (with toolbox) and then select “Organize Favorites…”. Click on “Add…” in the popup window and then check the box in front of “leJOS Download”, which is the external tools we configured in the previous step. Press “OK” and then “Ok” again.
5. Write, download and run a “Hello world” program
First, we need to create the main class of your program. Click on “New => Class”. Name it “HelloWorld” and check the box “public static void(String[] args)”. You will receive an almost empty class. Eclipse is a very smart Java editor that tries to compile your program while you write. Similar to M$ Word, it will underline errors with a red line. Sometimes this automatic compilation does not work and you want to force Eclipse to compile. You can achieve this by simply saving the file.
You now need to to enter a few lines of Java code. In the first line add:
import lejos.nxt.LCD;
This line will enable your program to use the LCD display of your NXT. Next, we need to define what should be displayed and when. Add the following lines to the main method:
LCD.drawString(“Hello World!”, 2, 2);
LCD.refresh();
while(true) {}
Now you can safe the program and then download it into the NXT. The next screenshot walks you through these steps. Before you start the downloading procedure you must make sure that the NXT is connected to the computer and that the NXT is recognized by the operating system. You can verify this in the device manager. You also need to select your main class window in the middle before starting the downloading procedure, so that Eclipse knows which class you want to download. Use the pull down menu (denoted as 4 in the screenshot) to select the downloading favorite your previously created. This will start the downloading procedure.
After the download is complete you can start the program by using the buttons on the NXT. Notice that the program will run forever. To stop it you need to reset the NXT by holding the two middle buttons for a longer time. This will then terminate the USB connection, so you need to wait a little for it to reconnect before you download a new program. Of course there are better ways of ending your program, such as listening to a button press or after a certain delay. Replace the endless while loop with this:
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
}
This will set your NXT to sleep for 5 seconds before the program ends. You will now see “Hello World!” for five seconds and then the program ends. For more information on programming in Java you should check the leJOS page.
Extra steps for Windows Vista
Before you start make sure the ‘User account control’ is turned off. Go to Control Panel → User Accounts
Uncheck Use User Account Control.
If installing the leJOS firmware on the NXT states that LibUSB is not installed:
- Download the latest libusb version for Windows (http://sourceforge.net/projects/libusb-win32/files/)
- Right click on this file. Select Properties -> Compatibility. Click the “Run this program in compatibility mode” box and select “Windows XP (Service Pack2)” from the drop down list.
- Right click again and select “Run as Administrator”. Follow the installation instructions.
Thanks to Niko Vegt & Maarten Brugmans for this information.
Thanks. My XT now can tell the world howdy :-)
I read the eclipse help for leJOS and it pointed out that we can select the main Java class and the menu has an option to d/l to the NXT. It also has an option to set the Java project to be leJOS and get the right library.
I left my version on 1.6. I’ll play with setting it to 1.3 later.
I am running on Vista. It was a bit of pain to get it set up, your instructions for libusb saved me!
Thanks again.
thnx, was running the Lejos version at school but did not know how to install non Legos Firmware, good tut thankyou
Thanks for this fully illustrated tutorial to get things started!
thanks for this clear tutorial. Any chance of creating a Mac version sometime soon? Hoping to show off the NXT using our Macs, no time to explore though.
Thanks for your help.
perfect!
thank you !
Console is displaying “LEJOS_HOME not defined.” when I try to load the class onto the NXT…
Not sure what im doing wrong, any ideas?
Hey Dirk,
It looks like you have some trouble with setting up the system variable. Please check this snapshot: http://www.bartneck.de/wp-content/uploads/2008/03/02lejos_06envvariables_new.png
Make also sure that the path is defined correctly.
/c
Thank you for this tutorial.
When you use Eclipse does it compile the code itself for upload to the NXT?
Would you have to use the Lejos compiler with Eclispe?
Thank you again.
Hello Adam,
Please read the section about the compiler version carefully. Yes, Eclipse compiles the code and no, you do not need to use any Lejos compiler.
/c
Thx for this tutorial!! :)
I have follow all steps, and when I want to upload Hello World program, occure a error.
leJOS NXJ> Linking…
leJOS NXJ> Uploading…
leJOS NXJ> Cannot load USB driver
BlueCove version 2.0.2 on winsock
an error occurred: No NXT found – is it switched on and plugged in (for USB)?
BlueCove stack shutdown completed
The NXT is connected on my PC, my Vista recognized it, but the problem. Can you please help me????
:)
Thx you!!
I follow all steps and when i want to upload the HelloWorld program on my NXT a error occure.
leJOS NXJ> Linking…
leJOS NXJ> Uploading…
leJOS NXJ> Cannot load USB driver
BlueCove version 2.0.2 on winsock
an error occurred: No NXT found – is it switched on and plugged in (for USB)?
BlueCove stack shutdown completed
The nxt is connected on my PC, and Vista recongnised it, but a have this problem. I try difrent things but without results. Can you please help me? :)
This tutorial has not been tested with Vista. My guess would be that there is a problem with LibUSB. Please let me know when you found the solution and post it to here. Thanks!
My problem was with the LibUSB, it is neccesery to install it in XP compatible mode. Now I am able to download HelloWorld program.
But now I have a new problem, when I exucute my program i don’t see clear Hello World on the nxt display, I see many black points but no words.
Hello harex
u try this:
open cmd
input : lejosfirmdl
u will see windows install
if it cant fint this command
u need chect your Windows environment variable
I completed the setup in the tutorial. I have the same problem as harex. There are many blck dots on the nxt display but it does not spell out Hello World. I am using the new LeJOS 0.6.0beta.
This is the output in the Eclipse Console
leJOS NXJ> Linking…
leJOS NXJ> Uploading…
Found nxt name Unknown address 123456780090
leJOS NXJ> Upload successful in 235 milliseconds
I also tried to upload another program for a robot I built.
The Eclipse Console gave the same output and the NXT display read
Data abort
PC 0010404C
AASR 0020073B
ASR 02010102
OPCODE 181
DEBUG1 00000000
DEBUG2 00000000
The brick then made the ticking sound as if it were doing a firmware upload.
I reloaded the LeJOS firmware onto the NXT brick and the problem was fixed. The Eclipse Console output was different from the tutorial.
leJOS NXJ> Linking…
leJOS NXJ> Uploading…
Found nxt name NXT address 001653055720
leJOS NXJ> Upload successful in 1297 milliseconds
This must be the result of chnages in the new LeJOS version from the one in the tutorial. Fun stuff!
Console is displaying “LEJOS_HOME not defined.” when I try to load the class onto the NXt ,
however , the new system variable “LEJOS_home” shows up in the list , ( exactly as in the picture http://www.bartneck.de/wp-content/uploads/2008/03/02lejos_06envvariables_new.png)
I don’t no what is wrong ….
it’s working now , thanks a lot !!
Hello,
I have upgrade the firmware on my NXT to use it for leJOS. Now I need to use my NXT with Lego NXT-G, how can I restore the firmware?
Thx
Very nice tutorial, i haven’t use it yet but it seems very detailed.
I have a question about replacing the original LEGO firmware.
does it mean that i can’t use the LEGO sotware tools to program the NXT (the mindstorms-nxt GUI)?
is it reversable and how?
thanks,
Ofer
Yes, you can restore the original Lego firmware and continue using Lego’s software.
1) Open up your NXT-G software and connect your brick to your computer. Make sure that you’re connected to the internet.
2) Go up to the “Tools” menu and select “Update Firmware”.
3) In the new pop up window, select “download”.
4) After a few seconds, you’ll hear a multi-toned sound. This means that the original firmware is back on the brick.
thanks
I am aloso having the same Vista problem :(
leJOS NXJ> Linking…
leJOS NXJ> Uploading…
leJOS NXJ> Cannot load USB driver
an error occurred: BluetoothStack not detected
Anyone got a solution?
Thank you for this article for installing lejos.
However, How can I restore the original firmware back :(
Hello Hamido,
The instructions to restore your original firmware are in a comment above.
Good luck.
hi there
i have problem when i Run:
Error: Class leJOS (file leJOS.class) not found in CLASSPATH .;C:\ProgramFiles\lejos_nxj\3rdparty\lib\bcel-5.1.jar;C:\ProgramFiles\lejos_nxj\3rdparty\lib\commons-cli-1.0.jar;C:\ProgramFiles\lejos_nxj\lib\pctools.jar;C:\ProgramFiles\lejos_nxj\lib\pccomm.jar;C:\ProgramFiles\lejos_nxj\lib\jtools.jar;C:\ProgramFiles\lejos_nxj\3rdparty\lib\bluecove.jar;C:\ProgramFiles\lejos_nxj\lib\classes.jar
*what do i need to define in CLASSPATH , Java location or LeJOS location or something else ?
can you help me plz
hi there this is me again , what i need to enter in Argument field in eclipse what you mean java_type_name for example what did you enter in your eclipse in that field? I am not clear in that field
http://www.bartneck.de/wp-content/uploads/2008/03/30eclipse_17_lejosdl_15.png
That really helped me learn how to put leJOS on my computer.
Also: Can you help me put Bluetooth on my NXT now?
I saw how descriptive your leJOS was, and I could really use that to help me with Bluetooth!
Thanks!
NXT has Bluetooth hardware and LeJos supports the communication via Bluetooth. I am sorry, but I currently do not have a dedicated tutorial for Bluetooth on the NXT.
It’s okay
Do you know how to run wav. files on the NXT?
Can you post a program?
Thanks.
“an error occurred: BluetoothStack not detected”
I’ve read somewhere that under Windows, the Bluetooth dongle must support the Widcomm Bluetooth Stack; so the solution is to try a NXT compatible device: Bluetooth dongle (item 9847) at http://www.shop.lego.com
I’m also facing the same problem & I ‘ve yet to use the above solution.
I have a question, do yo need a bluetooth device to communicate whit the nxt brick?
because when I’m trying to download the program to the brick it shows and error message like this:
“leJOS NXJ> Linking…
leJOS NXJ> Uploading…
error BluetoothStack not detected
com.intel.bluetooth.BlueCoveImpl.(BlueCoveImpl.java:136)
an error occurred: Bluetooth stack not detected”
and I don’t get it I’m want it to use the usb to download the program not bluetooth
Thanks.
Hello Ivan,
No, you do not need Bluetooth to download the software. USB is just fine. Make sure that the NXT is connected to the computer (check device manager) and that LibUSB is installed. That should do the trick.
Hello,
I have Vista 64bit and am having problems installing the NXT dirvers. When I try I get a message box saying ” The windows installer engine is corrupt. To attemp to resolve this issues, please register the engine by running regsvr32.exe on msi.dll”.
I did this and it registered. Then I re-booted and tried to run the install and received the same message.
Can anyone help?
Thanks…
Hello again,
I am tyring to run the basic ‘Hello World’ app and am having difficulites getting it to run. I get the error message below:
Exception in thread “main” java.lang.UnsatisfiedLinkError: lejos.nxt.LCD.getSystemFont()[B
at lejos.nxt.LCD.getSystemFont(Native Method)
at lejos.nxt.LCD.(LCD.java:25)
at HelloWorld.main(HelloWorld.java:10)
I have followed all the setup steps in the tutorial but continue to get the above error message. I am able to remove all references to legos in the code and get the basic main app to compile so I know my javac is working.
Can anyone hlep?
Thanks again,
Joe
Hi there,
If you are having problems with libusb in Vista , please make sure you do the following:
1. Download the latest libusb version for Windows
2. Right click on this file. Select Properties -> Compatibility. Click
the “Run this program in compatibility mode” box and select
“Windows XP (Service Pack2)” from the drop down list.
3. Right click again and select “Run as “Administrator”. Follow the
installation instructions.
All the installation steps described in the tutorial above remain the same, only the installation of the libusb software is different. Worked for me!
Good luck with the installation and the programming!
Tom
Hey everybody!
This is a very helpful tutorial for me and I could get all the steps done until configuring the external tool in eclipse to download the code to the NXT.
I set the variables just as described in the tutorial:
Location: C:\ProgramFiles\lejos_nxj\bin\lejosdl.bat
Working Directory: ${project_loc}\bin
Arguments: ${java_type_name}
After cllicking “Apply” and then “Run”, eclipse tells me this:
Variable references empty selection: ${project_loc}
Does anyone know what files eclipse needs to locate in working directory and arguments?!? Maybe I can add them manually?!
I appreciate your help, since I’m stuck at this point and already reinstalled everything twice…
Siggi
im at the same position as SIGI ABOVE AND THE EXACT SAME PROBLEM, HELP NEEDED BADLY. THANKS
i got it working, just reset nxt and install driver again. and filter usb first thanks
Solved the problem as well, by installing different eclipse versions and one (of course the last one i tried) actually worked ;-)
Hello there.
I have a question: Whenever i try to upload a program to my nxt, i only get this output:
leJOS NXJ> Linking…
leJOS NXJ> Uploading…
Found nxt name NXT address 001653055720
but the upload successfully output is missing…
and therefore i get this error:
Data abort
PC 0010404C
AASR 0020073B
ASR 02010102
OPCODE 181
DEBUG1 00000000
DEBUG2 00000000
i’d be very happy, if someone could help me
thx
I am apparently trying to use Classes which are documented but nonetheless not implemented. For example, I tried the following code:
import lejos.nxt.*;
public class Battery {
/**
* @param args
*/
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
LCD.drawString(“Battery: ” + Battery.getVoltage(),0,0);
Thread.sleep(2000);
}
}
Apparently the Method getVoltage is not available in lejos.nxt although it is listed at the following site:
http://lejos.sourceforge.net/p_technologies/nxt/nxj/api/lejos/nxt/Battery.html#getVoltage()
If so, is there a list of the Classes and Methods which are actually available?
Thank you.
Ted K
Hello everybody,
It’s me again. As is so often the case, I’ve been able to solve my problem myself, at least partially. I erred in defining the Class Battery when I also want to use the predefined Class Battery, which does indeed have the method getVoltage in the lejos.nxt library. My code apparently still has problems, but I’ll try to sort them out myself.
The list I wanted is available in the list of Referenced Libraries/lejos.nxt in the Explorer panel.
Sorry for any inconvenience I may have caused.
Ted K
I’ve done all steps but i get this error when I try to run and i don:t know how to fix it?
Can anybody help me?
leJOS NXJ> Linking…
an error occurred: org/apache/bcel/util/ClassPath
Hello All,
Have used this tutorial in the past for a class, now doing graduate work a year later and I find myself doing this all again on a different machine.
All goes to plan (I guess) until I try to download the HelloWorld program onto the brick. I get the following:
Exception in thread “main” java.lang.UnsatisfiedLinkError: C:\ProgramFiles\lejos_nxj\bin\jlibnxt.dll: Can’t find dependent libraries
at java.lang.ClassLoader$NativeLibrary.load(Native Method)
at java.lang.ClassLoader.loadLibrary0(Unknown Source)
at java.lang.ClassLoader.loadLibrary(Unknown Source)
at java.lang.Runtime.loadLibrary0(Unknown Source)
at java.lang.System.loadLibrary(Unknown Source)
at lejos.pc.comm.NXTCommLibnxt.(NXTCommLibnxt.java:64)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at lejos.pc.comm.NXTCommand.search(NXTCommand.java:47)
at lejos.pc.tools.Upload.upload(Upload.java:34)
at lejos.pc.tools.NXJLinkAndUpload.run(NXJLinkAndUpload.java:102)
at lejos.pc.tools.NXJLinkAndUpload.main(NXJLinkAndUpload.java:27)
Not sure what its complaining about, the file C:\ProgramFiles\lejos_nxj\bin\jlibnxt.dll is there, but perhaps its looking for something else that isn’t.
Any help much appreciated.