Skip to main content

Compiling and running programs

Posted in

This tutorial results from the official leJos web site.


 

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

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

 

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.

 

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.

 

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.

If the -r parameter is specified, the program is run after it has been uploaded.