Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Hot can I access the serial port? 1

Status
Not open for further replies.

aryajur

Programmer
Jul 2, 2003
45
0
0
US
Hello,
Can anyone help me as to how can I access the serial port thru a Java application ?

 
I tried the api and I tried to execute the following code in one of the examples:

import java.io.*;
import java.util.*;
import javax.comm.*;

public class SimpleWrite {
static Enumeration portList;
static CommPortIdentifier portId;
static String messageString = "Hello, world!\n";
static SerialPort serialPort;
static OutputStream outputStream;

public static void main(String[] args) {
portList = CommPortIdentifier.getPortIdentifiers();

while (portList.hasMoreElements()) {
portId = (CommPortIdentifier) portList.nextElement();
if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
if (portId.getName().equals("COM1")) {
//if (portId.getName().equals("/dev/term/a")) {
try {
serialPort = (SerialPort)
portId.open("SimpleWriteApp", 2000);
} catch (PortInUseException e) {}
try {
outputStream = serialPort.getOutputStream();
} catch (IOException e) {}
try {
serialPort.setSerialPortParams(9600,
SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE);
} catch (UnsupportedCommOperationException e) {}
try {
outputStream.write(messageString.getBytes());
} catch (IOException e) {}
}
}
}
}
}


It compiles fine but when I execute it it gives the message:

Exception in thread "main" java.lang.NoClassDefFoundError: SimpleWrite

What is wrong here??

 
The directory that you have your SimpleWrite.class file is not in your CLASSPATH.

In the console you will run your code, do

(in dos)
set CLASSPATH=%CLASSPATH%;.

(in unix/linux [syntax for csh a bit different mind])
export CLASSPATH=$CLASSPATH:.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top