Pages: [1]   Go Down
Author Topic: Installing java to use RXTX  (Read 4796 times)
0 Members and 1 Guest are viewing this topic.
USA
Offline Offline
Newbie
*
Karma: 0
Posts: 24
Uno Otto Otto Cinque
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I'm using this code and I'm trying to use it to start RXTX with Arduino.  I get the following compile errors:

C:\Users\cwc\java>java SerialTest.java
Exception in thread "main" java.lang.NoClassDefFoundError: SerialTest/java
Caused by: java.lang.ClassNotFoundException: SerialTest.java
        at java.net.URLClassLoader$1.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
Could not find the main class: SerialTest.java.  Program will exit.

I followed these directions:
Windows 64-bit

Copy the win64/rxtxSerial.dll into C:\Windows\SysWOW64
Append the directory containing rxtxSerial.dll into your PATH environment variable.

?????  I'm not sure how to do this: Append the directory containing rxtxSerial.dll into your PATH environment variable.

Any ideas?

/////////////////////////////////////////////////////////////////////////////////
import java.io.InputStream;
import java.io.OutputStream;
import gnu.io.CommPortIdentifier;
import gnu.io.SerialPort;
import gnu.io.SerialPortEvent;
import gnu.io.SerialPortEventListener;
import java.util.Enumeration;

public class SerialTest implements SerialPortEventListener {
   SerialPort serialPort;
        /** The port we're normally going to use. */
   private static final String PORT_NAMES[] = {          "COM3"};
   /** Buffered input stream from the port */
   private InputStream input;
   /** The output stream to the port */
   private OutputStream output;
   /** Milliseconds to block while waiting for port open */
   private static final int TIME_OUT = 2000;
   /** Default bits per second for COM port. */
   private static final int DATA_RATE = 9600;

   public void initialize() {
      CommPortIdentifier portId = null;
      Enumeration portEnum = CommPortIdentifier.getPortIdentifiers();

      // iterate through, looking for the port
      while (portEnum.hasMoreElements()) {
         CommPortIdentifier currPortId = (CommPortIdentifier) portEnum.nextElement();
         for (String portName : PORT_NAMES) {
            if (currPortId.getName().equals(portName)) {
               portId = currPortId;
               break;
            }
         }
      }

      if (portId == null) {
         System.out.println("Could not find COM port.");
         return;
      }

      try {
         // open serial port, and use class name for the appName.
         serialPort = (SerialPort) portId.open(this.getClass().getName(),
               TIME_OUT);

         // set port parameters
         serialPort.setSerialPortParams(DATA_RATE,
               SerialPort.DATABITS_8,
               SerialPort.STOPBITS_1,
               SerialPort.PARITY_NONE);

         // open the streams
         input = serialPort.getInputStream();
         output = serialPort.getOutputStream();

         // add event listeners
         serialPort.addEventListener(this);
         serialPort.notifyOnDataAvailable(true);
      } catch (Exception e) {
         System.err.println(e.toString());
      }
   }

   /**
    * This should be called when you stop using the port.
    * This will prevent port locking on platforms like Linux.
    */
   public synchronized void close() {
      if (serialPort != null) {
         serialPort.removeEventListener();
         serialPort.close();
      }
   }

   /**
    * Handle an event on the serial port. Read the data and print it.
    */
   public synchronized void serialEvent(SerialPortEvent oEvent) {
      if (oEvent.getEventType() == SerialPortEvent.DATA_AVAILABLE) {
         try {
            int available = input.available();
            byte chunk[] = new byte[available];
            input.read(chunk, 0, available);

            // Displayed results are codepage dependent
            System.out.print(new String(chunk));
         } catch (Exception e) {
            System.err.println(e.toString());
         }
      }
      // Ignore all the other eventTypes, but you should consider the other ones.
   }

   public static void main(String[] args) throws Exception {
      SerialTest main = new SerialTest();
      main.initialize();
      System.out.println("Started");
   }
}
Logged

cwc 1885

Offline Offline
Edison Member
*
Karma: 19
Posts: 1041
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I didn't throroughly read your post, but you have to compile java code before you can run it. You need to make sure you have a JDK, and then run something like
Code:
javac SerialTest.java # Generates bytecoe
java SerialTest.class # Actually runs it
Logged

USA
Offline Offline
Newbie
*
Karma: 0
Posts: 24
Uno Otto Otto Cinque
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I've got java installed.  I can compile and run .  I've done it thousands of times. 
I'm not sure where to put
RXTXcomm.jar goes in \jre\lib\ext
rxtxSerial.dll goes in \jre\bin
using windows

thanks for the line.
Logged

cwc 1885

USA
Offline Offline
Newbie
*
Karma: 0
Posts: 24
Uno Otto Otto Cinque
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I appended rxtxSerial.dll goes in \jre\bin
Now I get these errors:

C:\Users\cwc\java>javac SerialTest.java
SerialTest.java:3: package gnu.io does not exist
import gnu.io.CommPortIdentifier;
             ^
SerialTest.java:4: package gnu.io does not exist
import gnu.io.SerialPort;
             ^
SerialTest.java:5: package gnu.io does not exist
import gnu.io.SerialPortEvent;
             ^
SerialTest.java:6: package gnu.io does not exist
import gnu.io.SerialPortEventListener;
             ^
SerialTest.java:9: cannot find symbol
symbol: class SerialPortEventListener
public class SerialTest implements SerialPortEventListener {
                                   ^
SerialTest.java:10: cannot find symbol
symbol  : class SerialPort
location: class SerialTest
        SerialPort serialPort;
        ^
SerialTest.java:79: cannot find symbol
symbol  : class SerialPortEvent
location: class SerialTest
        public synchronized void serialEvent(SerialPortEvent oEvent) {
                                             ^
SerialTest.java:23: cannot find symbol
symbol  : class CommPortIdentifier
location: class SerialTest
                CommPortIdentifier portId = null;
                ^
SerialTest.java:24: cannot find symbol
symbol  : variable CommPortIdentifier
location: class SerialTest
                Enumeration portEnum = CommPortIdentifier.getPortIdentifiers();
                                       ^
SerialTest.java:28: cannot find symbol
symbol  : class CommPortIdentifier
location: class SerialTest
                        CommPortIdentifier currPortId = (CommPortIdentifier) por
tEnum.nextElement();
                        ^
SerialTest.java:28: cannot find symbol
symbol  : class CommPortIdentifier
location: class SerialTest
                        CommPortIdentifier currPortId = (CommPortIdentifier) por
tEnum.nextElement();
                                                         ^
SerialTest.java:44: cannot find symbol
symbol  : class SerialPort
location: class SerialTest
                        serialPort = (SerialPort) portId.open(this.getClass().ge
tName(),
                                      ^
SerialTest.java:49: cannot find symbol
symbol  : variable SerialPort
location: class SerialTest
                                        SerialPort.DATABITS_8,
                                        ^
SerialTest.java:50: cannot find symbol
symbol  : variable SerialPort
location: class SerialTest
                                        SerialPort.STOPBITS_1,
                                        ^
SerialTest.java:51: cannot find symbol
symbol  : variable SerialPort
location: class SerialTest
                                        SerialPort.PARITY_NONE);
                                        ^
SerialTest.java:80: cannot find symbol
symbol  : variable SerialPortEvent
location: class SerialTest
                if (oEvent.getEventType() == SerialPortEvent.DATA_AVAILABLE) {
                                             ^
16 errors

C:\Users\cwc\java>
Logged

cwc 1885

USA
Offline Offline
Newbie
*
Karma: 0
Posts: 24
Uno Otto Otto Cinque
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Moved  RXTXcomm.jar to C:\Program Files\Java\jdk1.6.0_31\jre\lib\ext
and thing compile.  Now I get a runtime error:


C:\Users\cwc\java>java SerialTest
Exception in thread "main" java.lang.NoClassDefFoundError: gnu/io/SerialPortEven
tListener
        at java.lang.ClassLoader.defineClass1(Native Method)
        at java.lang.ClassLoader.defineClassCond(Unknown Source)
        at java.lang.ClassLoader.defineClass(Unknown Source)
        at java.security.SecureClassLoader.defineClass(Unknown Source)
        at java.net.URLClassLoader.defineClass(Unknown Source)
        at java.net.URLClassLoader.access$000(Unknown Source)
        at java.net.URLClassLoader$1.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
Caused by: java.lang.ClassNotFoundException: gnu.io.SerialPortEventListener
        at java.net.URLClassLoader$1.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        ... 12 more
Could not find the main class: SerialTest.  Program will exit.

C:\Users\cwc\java>
Logged

cwc 1885

UK
Offline Offline
Shannon Member
****
Karma: 223
Posts: 12630
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I've got java installed.  I can compile and run .  I've done it thousands of times. 

In that case I'm baffled that you tried to run "java SerialTest.java", and also that you didn't then smack yourself on the forehead when java explained "Could not find the main class: SerialTest.java". The argument to java must be either the name of your main class, or the name of a JAR file containing a manifest which specifies the main class. It is not the name of a java source file.

You will need to provide javac and java with access to the external classes that you are using. The easiest way to do that is to put the JARs containing their class files on the classpath.

Logged

I only provide help via the forum - please do not contact me for private consultancy.

USA
Offline Offline
Newbie
*
Karma: 0
Posts: 24
Uno Otto Otto Cinque
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

In that case I'm baffled that you tried to run "java SerialTest.java", and also that you didn't then smack yourself on the forehead when java explained "Could not find the main class: SerialTest.java". The argument to java must be either the name of your main class, or the name of a JAR file containing a manifest which specifies the main class. It is not the name of a java source file.

You will need to provide javac and java with access to the external classes that you are using. The easiest way to do that is to put the JARs containing their class files on the classpath.

Thank you so much for the help. I really don't follow your response. I'm a bit slow on this one. Make that a lot slow. 
I think what you are writing is:  use "javac" directly with the class path included ignoring external classes? (classes that come with java)
I poked around for a while and I found the RXTXcomm.jar in tha arduino app folder.

I think I need to get this script to work?

set PATH=%PATH%;C:\Program Files (x86)\arduino-0017\
"C:\Program Files (x86)\Java\jdk1.6.0_12\bin\javac" -cp "C:\Program Files (x86)\arduino-0017\lib\RXTXcomm.jar" SerialTest.java
"C:\Program Files (x86)\Java\jdk1.6.0_12\bin\java" -cp "C:\Program Files (x86)\arduino-0017\lib\RXTXcomm.jar;." SerialTest
Logged

cwc 1885

UK
Offline Offline
Shannon Member
****
Karma: 223
Posts: 12630
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

All you should need is
Code:
javac -classpath c:\somewhere\RXTXcomm.jar SerialTest.java
java  -classpath c:\somewhere\RXTXcomm.jar SerialTest

The java process will also need to be able to load rxtxSerial.dll. There are various ways to achieve that; putting it in C:\Windows\SysWOW64 (if you're using a 32-bit version of Windows) or the directory containing java.exe should work.
Logged

I only provide help via the forum - please do not contact me for private consultancy.

USA
Offline Offline
Newbie
*
Karma: 0
Posts: 24
Uno Otto Otto Cinque
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks again for your help.  I got side tracked with another project.
I do believe if I keep at it I will get things to work.
I want to do serial input and monitor serial in a custom Java application.
When we get things working I'll write back.

Cheers
Craig
Logged

cwc 1885

USA
Offline Offline
Newbie
*
Karma: 0
Posts: 24
Uno Otto Otto Cinque
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I finally got things working in Mac OSX and Linux.  I just needed to put things in the correct location.   I am having some problems with Linux Mint and /dev/ttyACM0 not being recognized.
Logged

cwc 1885

Offline Offline
Newbie
*
Karma: 0
Posts: 1
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Why not tell us where the correct locations are? I'm having a similar problem in Mac OS X and it would be nice to know what you found out.
Logged

UK
Offline Offline
Tesla Member
***
Karma: 141
Posts: 8015
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Once you have the JVM installed it is very forgiving about where anything else goes - as long as you give it the correct path to the stuff.

I use JRuby - which runs on the JVM - regularly to interface with my Arduinos. When I create a new project I just have a directory called /lib within my project and I put the Rxtx files in there. That means I can make a copy of the project and all the dependencies go with it automatically.

I'm using Ubuntu and I have a short shell script
Code:
#!/bin/bash

export PATH=$PWD:$PATH;
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$PWD/lib;
export GEM_HOME=$PWD/GemHome;
x-terminal-emulator

in the project directory. When I double-click that it opens a terminal at the project directory with the necessary paths set.

I've done as little Java programming as I could get away with but as far as I know this technique is standard Java.

I did some experimenting about 12 months ago and I believe the same technique works on Windows, though you would use a .bat file in place of the shell script.

...R
Logged

Pages: [1]   Go Up
Jump to: