Compiling and Running Programs


Compiling and Running Programs

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.

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 sections:

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 .cmd or .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:

  • nxjc
  • nxjlink
  • nxjupload
  • nxj

Note that you normally only need to use the nxjc and nxj commands, as nxj does the equivalent of nxjlink followed by nxjupload.

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>

Example:

nxjc View.java

nxjc calls javac with parameters:

  • -bootclasspath <path to classes.jar>
  • <java-files>

-bootclasspath is set because leJOS does not use the standard java.lang classes but has its own versions in classes.jar.

Back to top

nxjlink – link a program

Calls the leJOS NXJ linker.

Usage: nxjlink [-v|--verbose] [-g|--debug] [-a|--all] main-class –o <binary>

Example:

nxjlink -v Tune -o Tune.nxj

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 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.

Back to top

nxjupload – upload a program

Usage: nxjupload [-b|--bluetooth] [-u|--usb] [-d|--address address] [-n|--name name] [-r|--run] <binary>

Example:

nxjupload Tune.nxj

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.

Back to top

nxj – link, upload and run a program

Usage: nxj [options] main-class

Example:

nxj -r Tune

The nxj command links and uploads a leJOS NXJ program. It is the equivalent of nxjlink followed by nxjupload.

Any of the options for nxjlink and nxjupload can be specified.

The default binary name is <main-class>.nxj, e.g. Tune.nxj.

Back to top

Using PC API command line tools

The tools for compiling and running leJOS PC API programs are:

  • nxjpcc
  • nxjpc

Reminder: If you are compiling or running a PC API program without using these tools, you need pccomm.jar and bluecove.jar (Linux users also need bluecove-gpl) in your CLASSPATH and classes.jar must be removed. Also, the java library path needs to be set.

nxjpcc – compile a PC API program for your pc

Compiles one or more PC API java files.

Usage: nxjpcc [javac-options] <java-files>

Example:

nxjpcc SensorTest.java

Back to top

nxjpc – run a PC API program on your pc

Usage: nxjpc [java-options] <main-class>

Calls java to run your PC API program.

Example:

nxjpc SensorTest

Back to top