USB communication clarification please!

This is where you talk about the NXJ software itself, installation issues, and programming talk.

Moderators: roger, imaqine, 99jonathan

New User
Posts: 8
Joined: Sun Nov 27, 2011 4:19 pm

USB communication clarification please!

Postby sioux » Sun Nov 27, 2011 4:37 pm

Hello. As you probably have noticed, i am new to the forum and also
new to the nxt interface. First of all I would like to thank you for your work
and for this api that has been proved quite useful to me.

Anyway to the point. I have written a fuzzy contoller for the nxt and i am developing
some other ai applications such as some unsupervised neural algorithms and stuff
which I intend to run partially on the brick and partially on the computer.

My Java experience isn't that great i would say, so I have some questions.

First of all, here is the code i implemented for the connectivity, which is linked to the user interface
in other classes that i believe are irrelevant now.

public class ConnectionHandler {

protected static NXTComm nxtComm = null;
private NXTInfo[] nxtInfo = null;

public ConnectionHandler() {


public int connectBt(String s) throws NXTCommException {

nxtComm = NXTCommFactory.createNXTComm(NXTCommFactory.BLUETOOTH);
nxtInfo =, NXTCommFactory.BLUETOOTH);

if (nxtInfo.length == 0)
return -2;
else {[0]);
return 0;


public int connectUsb(String s) throws NXTCommException {

nxtComm = NXTCommFactory.createNXTComm(NXTCommFactory.USB);
nxtInfo =, NXTCommFactory.USB);

if(nxtInfo.length == 0)
return -2;
else if(nxtInfo == null)
return -2;
else {[0]);
return 0;

As you can see I use the NXTCommFactory to open my BT and USB connections. Thing is, as I understand
the usb connection uses the jfantom.dll library. When I run the program from eclipse or netbeans it seems
to be working perfectly, as it is running depending on the machine variables. What I want though, is make a self-dependent portable
jar file or at most with a supplied /lib folder.
My question is what do I have to do so the library is loaded correctly when running a jar application?
Thank you for your time :)

New User
Posts: 8
Joined: Fri Nov 18, 2011 11:37 am

Re: USB communication clarification please!

Postby Starky » Sun Nov 27, 2011 6:48 pm

Hey Sioux - I was having a problem like this today using the NXTConnector class outside of the Eclipse environment and without the normal environment settings.

To get this working I had a directory which contained the following:

- My class (in your case the ConnectionHandler class)
- a folder called 'native' which contains the jfantom.dll
- a folder called lejos which contained the pc and comm directories with all of the compiled classes in there (for instance NXTInfo and NXTCommFantom classes)

Hopefully you should be able to wrap those up and get what you're looking for.

leJOS Team Member
Posts: 1550
Joined: Thu Oct 30, 2008 4:54 pm

Re: USB communication clarification please!

Postby skoehler » Sun Nov 27, 2011 6:59 pm

The native folder and pccomm.jar have to be in the same directory. If that is the case, the native libraries will be found without a hassle.
In case you planning to unpack pccomm.jar and repack it into a big fat JAR, then the native folder has to be in the same directory as the big fat JAR.
However, instead of making big fat JARs you should probably learn how to define a classpath using a JAR file's manifest.

I know, that there are other solutions, e.g. bluecove unpacks the native libraries from a JAR to load them, but that is crap because it leaves temporary files behind and is prone all sorts of errors and synchronization problems. And yes, what I'm saying applies to the loader used by bluecove as well.

New User
Posts: 8
Joined: Sun Nov 27, 2011 4:19 pm

Re: USB communication clarification please!

Postby sioux » Tue Nov 29, 2011 9:03 am

Thank you both very much for your information it was extremely helpful.

Yes skoehler I think you are right. Thing is I can do a lot of things in Java
and I love the object oriented approach in my projects, but I don't have
experience in that kind of applications so I followed your advice and
will supply my libraries separately. From what I understand Java is designed
with this thought in mind anyway so there is no point in trying to stuff
everything inside.

Thanks again you've been a great help.

New User
Posts: 8
Joined: Sun Nov 27, 2011 4:19 pm

Re: USB communication clarification please!

Postby sioux » Tue Nov 29, 2011 9:51 am

For future reference to others that have the same problem,

I packed my jar program with only the necessary files and resources(images etc.)
I provide a separate lib/ folder that has all the libraries in it.

my jar's manifest has a classpath of
Class-Path: lib/bcel.jar lib/bluecove-gpl.jar lib/bluecove.jar lib/com
mons-cli.jar lib/jtools.jar lib/pccomm.jar lib/pctools.jar

as would be required to run. I created the directory lib/native as instructed by the guys and
in it I copied jfantom.dll and that's it.

Ok I admit it it was a bit retarded of me not to notice that in the actual pc library pccomm.jar is
in the same folder as native and had focused the problem elsewhere.
For anyone as smart as me :p ==Check the libraries structure first==

Thank you for taking the time to reply to my post :p

Return to “NXJ Software”

Who is online

Users browsing this forum: Bing [Bot] and 3 guests