Using Netbeans

Using NetBeans

Installing Netbeans

You can download Netbeans from Netbeans downloads. If you are only going to use Netbeans for leJOS NXJ programs, you will only need standard Java development capabilities - you will not need a version that supports the Java Enterprise Edition or other languages such as C++. A package such as "Java SE" is sufficient for leJOS NXJ.

If you are not familiar with NetBeans, try the NetBeans quick start tutorial

For an introduction to developing general Java applications, see

And to learn some shortcuts in using the Editor, see

The next two sections are about using the samples that come with leJOS NXJ. If you want to skip this and start creating your own project, go to Installing the Netbeans Plugin.

Using the samples project

You can open the leJOS NXJ samples project by selecting File > Open Project and browsing to the samples folder in the location where you installed the leJOS java projects. If you used the Windows installer this will be leJOSNXJProjects in your home directory.Otherwise it will be lejos_nxj/projects.

There is a source folder within the samples project for each sample. Open up the BlueStats source folder and the "default package" that it contains and you will see the sample file and a build.xml file. The build.xml file is a build file for the sample written using "ant". Ant is a build system specially designed for Java - it comes bundled with Netbeans.

Ant build files have different "targets". The default target for the sample build.xml files is "uploadandrun". This means that if you run the build.xml file and select this target it will compile the Java source, link it with the standard leJOS classes (classes.jar) to produce a binary file and then upload ad run the binary file.

Turn on your NXT, make sure it is connected by USB or can be connected to via Bluetooth. Then right-click on the build.xml and select "Run Target" and "uploadandrun":

You should see output like the following in the Console window:

Buildfile: C:\Users\Lawrie\leJOSProjects\samples\BlueStats\build.xml
    [javac] Compiling 1 source file to C:\Users\Lawrie\leJOSProjects\samples\BlueStats
     [java] Class 0: java.lang.Object
     [java] Class 1: java.lang.Thread
     [java] Class 2: java.lang.String
     [java] Class 3: java.lang.Throwable
     [java] Class 4: java.lang.Error
     [java] Class 5: java.lang.OutOfMemoryError
     [java] Class 6: java.lang.NoSuchMethodError
     [java] Class 7: java.lang.StackOverflowError
     [java] Class 8: java.lang.NullPointerException
     [java] Class 9: java.lang.ClassCastException
     [java] Class 38: lejos.nxt.Flash
     [java] Method 0: Class: java.lang.Object Signature: ()V PC 3222 Signature id 2
     [java] Method 1: Class: java.lang.Object Signature: notifyAll()V Native id 5
     [java] Method 2: Class: java.lang.Object Signature: wait()V Native id 6
     [java] Method 3: Class: java.lang.Object Signature: wait(J)V Native id 7
     [java] Method 4: Class: java.lang.Object Signature: toString()Ljava/lang/String; PC 3223 Signature id 95
     [java] Method 147: Class: lejos.nxt.Flash Signature: writePage([BI)V Native id 75
     [java] Method 148: Class: lejos.nxt.Flash Signature: ()V PC 9337 Signature id 3
     [java] Master record    : 16 bytes.
     [java] Class records    : 39 (390 bytes).
     [java] Field records    : 80 (80 bytes).
     [java] Static fields    : 53 (106 bytes).
     [java] Static state     : 53 (202 bytes).
     [java] Constant records : 26 (104 bytes).
     [java] Constant values  : 26 (252 bytes).
     [java] Method records   : 149 (1788 bytes).
     [java] Exception records: 67 (536 bytes).
     [java] Code             : 117 (6122 bytes).
     [java] Total            : 9597 bytes.
     [java] Found nxt name NOISY address 001653007848
     [java] leJOS NXJ> Upload successful in 1974 milliseconds
Total time: 4 seconds

This verbose output from the leJOS linker tells you all the classes and methods that have been included in your program. It is very useful for debugging your programs.

You should see the Bluetooth statistics on your NXT LCD screen. You can stop the BlueStats program by pressing ESCAPE on the NXT.

You can browse through the samples and try them out. Some of the samples need to communicate with a program on the PC - see "Using the PC samples project" below. Other samples need specific sensors attached to the NXT or a robot with specific characteristics - such as a steerable wheeled vehicle. See the comments at the start of each of the Java files to understand the requirements of the sample.

Note that the samples project uses default package names for simplicity. This is not good practice for Java programming so when we create our own project, we will use a proper package name.

Note also that the samples project contains multiple programs each with a main method. This is convenient for showing lots of examples of leJOS NXJ programming in one project, but is not best practice. When we create our own projects, we will have a separate Netbeans project for each leJOS NXJ program

Back to top

Using the PC samples project

You can open the leJOS NXJ samples project by selecting File > Open Project and browsing to the samples folder in the location where you installed the leJOS java projects.

Open the BTSend folder. BTSend works with the BTReceive sample. runs on the PC and runs on the NXT.

There are no build files for the PC samples as they are not necessary. You can run by right-clicking on it and selecting, "Run as Java application", but first we need BTReceive running on the NXT.

Go to the samples application, select the build.xml file for BTReceive, and run it (Run as Ant Build) to upload BTReceive to the NXT.

Now go back to the pcsamples project and run BTSend should now run and connect to BTReceive, send it 100 integers and display the values it gets back. BTReceive will then wait for another connection.

You can use BTSend with the NXTReceive sample instead. NXTReceive is similar to BTReceive but it lets you choose which protocol and which mode you want to use to send data.

You can look through the other PC samples and try them out in a similar way. Some of them are paired with NXT samples. Look at the comment at the start of the Java files. Some of them run a subset of the leJOS NXJ API on the PC using remote execution. AccelDemo, SensorTest and TachoCount are examples of this. TachoCount shows you how to control which NXT brick you connect to if you have multiple NXT bricks available.

Back to top

Installing the Netbeans Plugin

It is very easy to install the Netbeans Plugin. Just Open Netbeans.In Tools > Plugins, click on the Downloaded tab, and click on Add Plugins. Then browse to the directory where you installed the leJOS NXJ projects. In it, find NXJPlugin/build/nxjplugin.nbm . Click on it, and it will be installed.

Back to top

Creating your own project using the Netbeans Plugin

You should not try to write code for the NXT using the standard Java Project of NetBeans. Instead, you need a special type of project that will use the leJOS tools to link and upload your code. The easiest way to create a NXJ project is to use the NetBeans plugin. If instead, you wish to create your own project with a custom ant build file, open the org.lejos.example project, and look at what that does. It is a free form Java project. It is in fact, the template project that the Netbeans plugin uses.

To create a NXJ project using the plugin, select File>New Project. then "Samples". In the Projects tab, select "NXJSample" and click next. The New Project window will open. Type in the name you want to use, such as "myproject" and click "finish'"

You should now see "myproject" in the Projects tab. In Source Packages > sample you will see, which by default is the Main class in your project. Click on it to open it in the editor.

The plugin copies a build.xml file and into your project. You can select and run targets in build.xml just as you did with the Samples project. Or can use the various shourcut keys the work with standard Java projects that you learned in the NetBeans tutorials. (Do not use the "Netbeans zip" target unless you know what you are doing. It makes your project the template for the Netbeans plugin, but you need to rebuild and re-install the NXJPlugin project to activate this).

You can rename the HelloWorld class by selecting the name, and using the Refactor > Rename. Likewise, you can change the name of the package the same way. After renaming it, you might wat to change the project name, description and the "main.class" property in the build.xml file. This is not strictly necessary if you are only going to use the "Run File" and "Compile File" targets but changing "main.class" allows you to run the targets in the build.xml file by right-clicking on it. There is a bug in the 0.85 version of the plugin which means you will also need to change the "program" property to your new class name (without the package name).

Right-click on your project, select "Properties" and go to "Java Sources Classpath". You will notice that there is a relative path to classes.jar. You might want to delete this and add an external jar file and set it to classes.jar in your leJOS NXJ installation's lib folder.

To test your project, right click on the java file of your main class, and select "Run File". This should compile, link and upload your program to the NXT and run it.

If you want to change the target that Run File uses, go to the Files view and edit the nbproject/ide-file-targets.xml.

Back to top