Tutorial on how to install and run Java on Lego Mindstorms NXT using Eclipse
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.
shareshareshareshare
I have the problem that was described above:
an error occurred: BluetoothStack not detected
But i don’t have bluetooth at all, i use USB. My NXT is connected and seen in the manager as USB device…
…i know that the problem in LibUSB…cause there isn’t such folder in .zip version.
Help me please…where to get this LibUSB?
I have installed LibUSB from previous vertion (0.6)…It installed ok…but this problem is still exist:
leJOS NXJ> Linking…
leJOS NXJ> Uploading…
an error occurred: BluetoothStack not detected
Help please…O.o…
i have solved this problem by installing lower version (0.6) but not the last (0,7)…it works normally!
Thnx for this topic!!!!
vistimoris,
version (0.6) : I can download this version ?
Does anyone have a tutorial on how to get bluetooth working going from PC to NXT.
thanku
Eclipse continues to give me “LEJOS_HOME not defined.” Error even when the Path and LEJOS_HOME is defined.
Any solutions?
Hi,
Thnx for the detailed tutorial..Could you please give me a tip of info?
After i follow the above steps, can my NXT brick still work with its own software or it is permanently programmed to execute java instructions?
Thnx in advance :)
Sounds like you guys know your stuff! Can you help out a novice who is trying to implement Robotics into a Primary School?
Our Computer system has been “upgraded” (supposedly) to VISTA and now the robots and computer will not ‘communicate’ via USB. We don’t have Bluetooth.
Is there anything special we need to do when installing the software on our server so we can access it across the school? Has anyone else had trouble with NXT 1.1 and Vista? It worked fine on XP!!!
PLEASE HELP URGENTLY!
Thanks
Hi
I get this error. I’m new to Java so I don’t know where to look. Can anyone help ?
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:14)
Hey Everybody,
First of all, great tutorial. I just wanted to say that I got both the “jlibnxt.dll: Can’t find dependent libraries” error and the “BluetoothStack not detected” error. The first error I fixed by downloading libusb.dll from here: http://libusb-win32.sourceforge.net/ . When I first installed libusb.dll, I got rid of the first error, but then got the second one. Apparently if you are running Vista, you need to run the executable in compatibility mode (right click the executable: Properties -> Compatibility, check the radio button “Run this program in Compat….”, make sure the dropdown menu is on Windows XP (Service Pack 2), then hit apply, and run the program). This got rid of the second error. If you get the first error even after installing libusb.dll, I would try using the Dependency Walker (http://www.dependencywalker.com/) to see which library files it cannot find. Also, like it was mentioned, this tutorial works for version Beta 0.4. I had problems with the newer 0.7. I hope this helps. Just keep banging away at it and it’ll work.
I need help i followed the tutorial and i have a project due soon. I am trying to run the hello world program and i get.
Exception in thread “main” java.lang.UnsatisfiedLinkError: lejos.nxt.LCD.getSystemFont()
plz help me
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:14)
When i run NXJ or NXJUpload, at the upload portion on NXJ, i get the error
leJOS NXJ> Linking…
an error occurred: No NXT found – is it switched on and plugged in (for USB)?
leJOS NXJ> Uploading…
Found nxt name Unknown address 000000000000
But if the nxt isnt turned on, it tries bluecove, so i know its connected. How do i fix it?
Hello,
i have the same error reported before from the several people:
leJOS NXJ> Linking…
leJOS NXJ> Uploading…
leJOS NXJ> Cannot load USB driver
By executing “lejosdl” or “lejosfirmdl” in CMD it seems that the command is found but nothing happens and it waits for a new command
Runtimevars:
LEJOS_HOME = D:\Coden\leJOS NXJ
PATH = %SystemRoot%\System32\Wbem;%JAVA_HOME%\bin;%LEJOS_HOME%\bin;%NXJ_HOME%\bin
Iam running WINXP SP3, can anybody help me i become desperate^^
ok iam very sry a little reboot and it works, thougth i had still done this
i get this error when i try to download my files
Error: Class com.ibm.oti.vm.VM (file com/ibm/oti/vm/VM.class) not found in CLASSPATH C:\Users\David\Desktop\Workspace\TETRIX\bin;\TETRIX\src;C:\ProgramFiles\lejos_nxj\lib\classes.jar;C:\ProgramFiles\lejos_nxj\lib\jtools.jar;C:\ProgramFiles\lejos_nxj\lib\pccomm.jar;C:\ProgramFiles\lejos_nxj\lib\pctools.jar;C:\Users\David\Documents\NetBeansProjects\NXT_0_6_Sample\Bluecove\bluecove-2.1.0.jar;C:\Users\David\Documents\NetBeansProjects\NXT_0_6_Sample\motej-library-0.8\motej-library-0.8.jar;C:\Users\David\Documents\NetBeansProjects\NXT_0_6_Sample\Commons\commons-logging.jar;C:\Archivos de programa\leJOS NXJ\lib\classes.jar;C:\Archivos de programa\Java\jre6\lib\resources.jar;C:\Archivos de programa\Java\jre6\lib\rt.jar;C:\Archivos de programa\Java\jre6\lib\jsse.jar;C:\Archivos de programa\Java\jre6\lib\jce.jar;C:\Archivos de programa\Java\jre6\lib\charsets.jar;C:\Archivos de programa\Java\jre6\lib\ext\dnsns.jar;C:\Archivos de programa\Java\jre6\lib\ext\jmf.jar;C:\Archivos de programa\Java\jre6\lib\ext\localedata.jar;C:\Archivos de programa\Java\jre6\lib\ext\QTJava.zip;C:\Archivos de programa\Java\jre6\lib\ext\sound.jar;C:\Archivos de programa\Java\jre6\lib\ext\sunjce_provider.jar;C:\Archivos de programa\Java\jre6\lib\ext\sunmscapi.jar;C:\Archivos de programa\Java\jre6\lib\ext\sunpkcs11.jar
Hello,
I just installed Java (6u13), libusb, LeJOS 0.7 beta and Eclipse Ganymede SR2, setup the environment variables and the external tools (compiler lejosjc and lejosdl) in Eclipse according to the setup list above, but nothing runs. The workspace does not have any .class nor .lej files. Who has any idea?
Many thanks, Teoman
Great post!
Any chance of you doing a tutorial for MACOSX using Lejos?
We were thinking about creating a Mac OS X version, but did not yet find the time.
[...] Tutorial on how to install and run Java on Lego Mindstorms NXT using Eclipse [...]
Thank you so much for this! Works like a charm. I really needed a quick way to setup everything. So thatnks.
By the way, how do you setup a download process in IntelliJ IDEA – do you know? Thank you.
Hi,
I am trying to install lejos on the NXT brick. I am using lejos obeta 0.8 version and windows vista.. I followed the steps given in the tutorial and brought NXT to uodate mode too. When the give the command “lejosfirmdl” in the command prompt, it says “cannot load comm driver”. Can anyone please help me proceed..
Thank you,
Sneha
Hi.
i’m trying to download my java code via bluetooth. it gives me following error:
leJOS NXJ> Linking…
leJOS NXJ> Uploading…
BlueCove version 2.0.3 on bluesoleil
an error occurred: No NXT found – is it switched on and plugged in (for USB)?
BlueCove stack shutdown completed
does someone know what should i do?
thanx in advance
Varand.
Anyone know where I can find directions for using c++ with lego mindstorms?
every goes as it should (i think), but then when i try to run the program on the nxt, it says the following:
Java Exception:
Class: 4
Method: 0
[...] Tutorial on how to install and run Java on Lego Mindstorms NXT using Eclipse [...]
Admin, Please is there a Netbeans work around for lejos version-0.8? i have tired all means but still can’t get it working.
Is anyone successfully usin netbeans and lejos please help me out!!! i need it for my final years projects.
Thanks
This tutorial is just awesome! Many Many Thanks!!!
Karter
Well done! Worked perfectly with 8.5 and WindowsXP
This tutorial (good though it is) is now rather old. Many of the steps (like installing libusb etc.) are no longer needed. Please see the leJOS web site for updated instructions on how to install leJOS and use it with Eclipse…
Hello !! , is a nice pleasure talking to you !! , you really helped me with your tutorial!!.
I have a little error running the sample colordetector of lejos. when i m downloading the project to the brick , this message apears on the console
“an error occurred: Class lejos.nxt.ColorSensor (file lejos/nxt/ColorSensor.class) not found in CLASSPATH C:\Archivos de programa\leJOS NXJ\lib\classes.jar;.”
what can i do in this case?
will be a nice pleasure receiving your answer as soon as posible!!
I really liked this tutorial cuase it is the most clear and easiest to understand than any other tutorial i took online, however you can’t use it for other eclipse projects like normal programing that are not based on the NXT robot.
Anyway, i thank you for your helpful info.
God bless you.
It works really well! thanks for the tutorial! :D
Great tutorial! The screenshots posted makes the tutorial very easy to follow; keep up the good work!
[...] Using Eclipse with Java NXT [...]
[...] First of all, I installed the NXT Fantom driver found on LEGO’s website [source]. Then, I installed LeJOS NXJ and flashed my robot with LeJOS NXJ firmware flasher [source]. Finally, I configured my Eclipse to allow it to compile my programs directly on the robot thanks to the step four of this tutorial [source]. [...]
[...] try it on eclipse [...]
This run on Windows 7 32 bits?