尝试执行相同java代码的不同输出
我正在使用NETBeans7IDE和Java6开发我的java项目。我使用以下编码从串行端口读取值:尝试执行相同java代码的不同输出,java,swing,Java,Swing,我正在使用NETBeans7IDE和Java6开发我的java项目。我使用以下编码从串行端口读取值: import java.io.*; import java.util.*; import javax.comm.*; public class SimpleRead implements Runnable, SerialPortEventListener { static CommPortIdentifier portId; static Enumeration portList;
import java.io.*;
import java.util.*;
import javax.comm.*;
public class SimpleRead implements Runnable, SerialPortEventListener {
static CommPortIdentifier portId;
static Enumeration portList;
InputStream inputStream;
SerialPort serialPort;
Thread readThread;
public static void main(String[] args) {
portList = CommPortIdentifier.getPortIdentifiers();
JOptionPane.showMessageDialog(null, "portList :"+portList.hasMoreElements());
while (portList.hasMoreElements()) {
portId = (CommPortIdentifier) portList.nextElement();
if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
if (portId.getName().equals("COM3")) {
// if (portId.getName().equals("/dev/term/a")) {
SimpleRead reader = new SimpleRead();
}
}
}
}
public SimpleRead() {
try {
serialPort = (SerialPort) portId.open("SimpleReadApp", 2000);
} catch (PortInUseException e) {System.out.println(e);}
try {
inputStream = serialPort.getInputStream();
} catch (IOException e) {System.out.println(e);}
try {
serialPort.addEventListener(this);
} catch (TooManyListenersException e) {System.out.println(e);}
serialPort.notifyOnDataAvailable(true);
try {
serialPort.setSerialPortParams(9600,
SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE);
} catch (UnsupportedCommOperationException e) {System.out.println(e);}
readThread = new Thread(this);
readThread.start();
}
public void run() {
try {
Thread.sleep(20000);
} catch (InterruptedException e) {System.out.println(e);}
}
public void serialEvent(SerialPortEvent event) {
switch(event.getEventType()) {
case SerialPortEvent.BI:
case SerialPortEvent.OE:
case SerialPortEvent.FE:
case SerialPortEvent.PE:
case SerialPortEvent.CD:
case SerialPortEvent.CTS:
case SerialPortEvent.DSR:
case SerialPortEvent.RI:
case SerialPortEvent.OUTPUT_BUFFER_EMPTY:
break;
case SerialPortEvent.DATA_AVAILABLE:
byte[] readBuffer = new byte[20];
try {
while (inputStream.available() > 0) {
int numBytes = inputStream.read(readBuffer);
}
System.out.print(new String(readBuffer));
} catch (IOException e) {System.out.println(e);}
break;
}
}
}
当我从netbeans执行时,JOptionPane显示“portList:true”
但是,当我双击jar文件并尝试运行时,JOptionPane显示“portList:false”
为什么有不同的输出试图执行相同的java代码为什么在我看来只有
main
的前两行才是真正相关的时候,却显示了这么多代码?我敢打赌这与权限有关……请回答任何问题,并对评论中的任何陈述进行评论或询问。我们不能用魔法解决这个问题,我们需要确凿的事实。顺便说一句,有人认为这是一个很好的评论?惊人..清单文件中的代码清单版本:1.0 Ant版本:Apache Ant 1.8.2创建人:1.6.0_32-b05(Sun Microsystems Inc.)类路径:lib/comm.jar X-COMMENT:Main类将由build Main类自动添加:sensorcount.SimpleRadi复制并将javax.comm.properties放在jrehome/lib文件夹中。现在它运行良好。