Compiling and Running Programs
Serbo-Croatian translation thanks to Jovana Milutinovich.
This section describes how to compile and run leJOS NXJ programs.
Most people will want to write Java programs that run on the NXT brick, but that is not the only option. leJOS NXJ also supports programs that run on the PC and control a NXT robot remotely. Such PC programs can control a NXT robot running the leJOS NXJ menu. They can also remotely control a robot running the standard LEGO firmware. An even more interesting option that leJOS NXJ supports is to write a program that runs partly on the NXT and partly on a PC with the two parts communicating with each other over Bluetooth or USB. The part of your program that runs on the PC has more memory and processing power available to it and can do more complex processing. It can also display interesting user interfaces such as maps of where your robot is exploring. The part of the program that runs on the NXT can respond quickly to sensors and can accurately control motors. If you are interested in writing PC programs see the PC API command line tools section below.
While we are on the subject, it is also worth mentioning that leJOS NXJ programs can also run on mobile phones or other devices that support the Java MicroEdition Environment (JME). Such programs can communicate with the NXT over Bluetooth. We plan to add a section to the tutorial soon that will describe how to develop programs that use the leJOS NXJ JME API.
You can also communicate with leJOS NXJ programs from Android phones - see the Android tutorial page.
Finally, leJOS NXJ programs can be distributed across multiple NXT bricks which can communicate with each other over Bluetooth or via RS485 communication using NXT cables linking port 4 of two or more NXT bricks. Oh, and NXT programs can also communicate with external devices such as Bluetooth GPS Receivers. This is all described in Communications tutorial page.
We shall start, however, with writing programs that run on the NXT brick. You can either compile and run your programs using the leJOS NXJ command line tools or you can use an Integrated Development Environment (IDE).
While command line tools are very useful, programming for leJOS NXJ is best done using an IDE. IDEs have syntax-directed editors that immediately show you any syntax errors in your program, rather than waiting until you compile the program and then showing a list of errors. This, together with color coding of the source, automatic formatting of the code, prompting for method names and signatures, expanding and collapsing parts of your program, and many other editing features, makes creating your program a much faster and more enjoyable experience. But the advantages of the IDE do not end there: they also help you with creating and building projects, debugging, generating documentation, and creating user interfaces. Java IDEs put all the Sun Java tools and a variety of third-party tools at your fingertips. They make supporting new tools simple, either by use of plug-ins or by integration of external tools.
IDEs are easy to set up and use and you should use them for all your leJOS programming – even the simplest of projects. Any type of leJOS NXJ program can be created using an IDE.
You can produce leJOS NXJ programs with any Java IDE. This tutorial currently has sections on how to use two of the most popular Jave IDEs: Eclipse and Netbeans. leJOS NXJ supports plugins for these two IDEs which makes writing and testing your programs even simpler.
To learn how to create, compile and run your programs from an Integrated Development Environment, go to one of the following page:
This section of the tutorial will teach you how to use the command line tools. If you decide to use the command line tools you can write your program using the editor of your choice. Many programmer's editors will let you invoke the tools directly from the editor.
Another option you can use to compile and run your leJOS NXJ programs is to use ant build scripts. ant build scripts are usually used from an IDE. Netbeans does all compiling and building of programs using ant, and it is an option in Eclipse. However, you can also use ant from the command line. ant build scripts are provided for all the leJOS NXJ samples. To run ant build scripts you just change directory to the directory containing the build.xml file and type ant.
The scripts described in the following sections are Windows .bat files or Unix shell scripts, depending on which operating system you are using. They set up the class path, library path and boot class path needed by leJOS NXJ and then call a Java class that does all the work. The exception to this is nxjc which just calls javac. Note that ant build scripts use the same underlying Java classes, but do not use the scripts.Back to top
Using the leJOS NXJ command line tools
leJOS uses the standard Sun Java compiler for compiling programs. However, it needs to replace the standard Java library with leJOS's own version of this - classes.jar. For this reason we provide a command called nxjc that sets the boot class path to classes.jar. Its parameter are the same as those as javac
leJOS NXJ programs are different from normal Java programs in that they do not support dynamic class loading. Instead all the classes used in the program are collected together and packaged in a binary file with a .nxj extension. This process is referred to as linking. The linked binary is then uploaded to the NXT.
The tools for compiling, linking and uploading leJOS NXJ programs are:
Note that you normally only need to use the
You need to open a command window to run these commands.
nxjc – compile a program
Compiles one or more java files.
Usage: nxjc <java-files>
nxjc calls javac with parameters:
-bootclasspath is set because leJOS does not use the standard java.lang classes but has its own versions in classes.jar.
nxjlink – link a program
Calls the leJOS NXJ linker.
Usage: nxjlink [-v|--verbose] [-g|--debug] [-gr|--remotedebug] [-a|--all] [-dm|--disablememcompaction] [-ea|--enableassertions] [-ec|--enablechecks] [-od|--outputdebug <debug-file> ] -o <binary> main-class
Links the specified main class with any classes that it references in the current directory and with the standard leJOS classes from classes.jar to produce a binary NXJ program that can be uploaded and run.
The -v or --verbose flag causes a list of class names and method signatures included in the binary to be sent to the standard output. This output is extremely useful for debugging.
The -g or --debug flag causes a debug monitor to be included with the program. This allows the program to be interrupted while is running (by pressing ENTER+ESCAPE) and gives stack dumps when untrapped exceptions occur.
The -gr or --remotedebug flag is used to switch on remote debugging, which works with the nxjconsole or nxjconsoleviewer tools.
The -od or --outputdebug flag is used to specify an output debug file, which is used by remote debugging and by nxjdebugtool.
The -ec or --enablechecks flag is used to enable additional run time checks. These checks are relatively expensive (and rarely generate errors) and so are off by default. Currently the only check that this setting enables is the testing for ArraystoreExceptions.
The -ea or --enableassertions flag is used to enable the checking of assert statements with the program.
The -dm or --disablememcompactions flag is used to disable memory compaction. Normally the leJOS garbage collector will attempt to move large objects in memory to maximise the amount of contiguous free space, this option disables this feature.
The linker removes methods that are not used. Specify -a or --all to include all methods whether they are used or not. This should never be necessary.
Use the -h or --help flag to print out the options.
nxjupload – upload a program
Usage: nxjupload [-b|--bluetooth] [-u|--usb] [-d|--address address] [-n|--name name] [-r|--run] <binary>
Uploads the binary (.nxj) file. By default USB is tried first and then Bluetooth. If the --bluetooth flag is specified, only Bluetooth is tried. If --usb is specified, only USB is tried.
When Bluetooth is used, a search for Bluetooth devices is done, unless the -address flag is set, when a device with the given address is connected to.
The --name parameter limits the search for a NXT with the given name. If this is not specified, nxjupload tries to connect to each NXT that it finds and will upload to the first NXT that is successfully connects to.
If the --run parameter is specified, the program is run after it has been uploaded.
nxj – link, upload and run a program
Usage: nxj [options] main-class
The nxj command links and uploads a leJOS NXJ program. It is the equivalent of nxjlink followed by nxjupload.
Any of the options for
The default binary name is <main-class>.nxj, e.g. Tune.nxj.
Using PC API command line tools
The tools for compiling and running leJOS PC API programs are:
Reminder: If you are compiling or running a PC API program without using these tools, you need
nxjpcc – compile a PC API program for your pc
Compiles one or more PC API java files.
Usage: nxjpcc [javac-options] <java-files>
nxjpc – run a PC API program on your pc
Usage: nxjpc [java-options] <main-class>
Calls java to run your PC API program.