Java 从构建路径运行application.jar时出现问题
我正试图用串行通信编写一个程序。为此,我使用了Java通信API(comm.jar)。当我从IDE(NetBeans)运行我的程序时,它可以工作,但当我试图从它的构建位置(从'C:\Users\Bipin panday\Documents\NetBeansProjects…\dist'文件夹)运行时,我的应用程序.jar没有运行 从生成位置运行时无法工作的程序代码为:Java 从构建路径运行application.jar时出现问题,java,Java,我正试图用串行通信编写一个程序。为此,我使用了Java通信API(comm.jar)。当我从IDE(NetBeans)运行我的程序时,它可以工作,但当我试图从它的构建位置(从'C:\Users\Bipin panday\Documents\NetBeansProjects…\dist'文件夹)运行时,我的应用程序.jar没有运行 从生成位置运行时无法工作的程序代码为: CommPortIdentifier portIdentifier = portEnum.nextElement();
CommPortIdentifier portIdentifier = portEnum.nextElement();
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
java.util.Enumeration<CommPortIdentifier> portEnum = CommPortIdentifier.getPortIdentifiers();
int i = 0;
String[] r = new String[10];
while (portEnum.hasMoreElements()) {
CommPortIdentifier portIdentifier = portEnum.nextElement();
r[i] = portIdentifier.getName();//+ " - " + getPortTypeName(portIdentifier.getPortType()) ;
i++;
}
jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(r));
}
private void jComboBox1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
Object selectedItem = jComboBox1.getSelectedItem();
String com = selectedItem.toString();
SimpleRead(com);
}
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
serialPort.close();
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(Test.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(Test.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(Test.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(Test.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
new Test().setVisible(true);
}
});
}
public void SimpleRead(String com) {
portList = CommPortIdentifier.getPortIdentifiers();
while (portList.hasMoreElements()) {
portId = (CommPortIdentifier) portList.nextElement();
if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
if (portId.getName().equals(com)) {
// if (portId.getName().equals("/dev/term/a")) {
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(57600,
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[25];
try {
while (inputStream.available() > 0) {
//int numBytes = inputStream.read(readBuffer);
inputStream.read(readBuffer);
}
waiting(2);
String y = new String(readBuffer);
System.out.println(new String(readBuffer));
jTextArea1.setText(y);
} catch (IOException e) {
System.out.println(e);
}
break;
}
}
CommPortIdentifier-portIdentifier=portEnum.nextElement();
私有void jButton1ActionPerformed(java.awt.event.ActionEvent evt){
//TODO在此处添加您的处理代码:
java.util.Enumeration portEnum=CommPortIdentifier.getPortIdentifiers();
int i=0;
字符串[]r=新字符串[10];
while(portEnum.hasMoreElements()){
CommPortIdentifier portIdentifier=portEnum.nextElement();
r[i]=portIdentifier.getName();//+“-”+getPortTypeName(portIdentifier.getPortType());
i++;
}
setModel(新的javax.swing.DefaultComboxModel(r));
}
私有void jcombox1ActionPerformed(java.awt.event.ActionEvent evt){
//TODO在此处添加您的处理代码:
对象selectedItem=jcombox1.getSelectedItem();
字符串com=selectedItem.toString();
SimpleRead(com);
}
私有void jButton2ActionPerformed(java.awt.event.ActionEvent evt){
//TODO在此处添加您的处理代码:
serialPort.close();
}
/**
*@param指定命令行参数
*/
公共静态void main(字符串参数[]){
/*设置Nimbus的外观和感觉*/
//
/*如果Nimbus(在JavaSE6中引入)不可用,请使用默认的外观。
*详情请参阅http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
试一试{
for(javax.swing.UIManager.LookAndFeelInfo:javax.swing.UIManager.getInstalledLookAndFeels()){
if(“Nimbus”.equals(info.getName())){
setLookAndFeel(info.getClassName());
打破
}
}
}捕获(ClassNotFoundException ex){
getLogger(Test.class.getName()).log(java.util.logging.Level.SEVERE,null,ex);
}catch(实例化异常){
getLogger(Test.class.getName()).log(java.util.logging.Level.SEVERE,null,ex);
}捕获(非法访问例外){
getLogger(Test.class.getName()).log(java.util.logging.Level.SEVERE,null,ex);
}catch(javax.swing.UnsupportedLookAndFeelException ex){
getLogger(Test.class.getName()).log(java.util.logging.Level.SEVERE,null,ex);
}
//
/*创建并显示表单*/
invokeLater(new Runnable()){
@凌驾
公开募捐{
新测试().setVisible(真);
}
});
}
公共void SimpleRead(字符串com){
portList=CommPortIdentifier.getPortIdentifiers();
while(portList.hasMoreElements()){
portId=(CommPortIdentifier)portList.nextElement();
if(portId.getPortType()==CommPortIdentifier.PORT_串行){
if(portId.getName().equals(com)){
//if(portId.getName().equals(“/dev/term/a”)){
试一试{
serialPort=(serialPort)portId.open(“simpleradapp”,2000);
}捕获(PortinUseeException e){
系统输出打印ln(e);
}
试一试{
inputStream=serialPort.getInputStream();
}捕获(IOE异常){
系统输出打印ln(e);
}
试一试{
serialPort.addEventListener(此);
}捕获(ToomanyListenerSE异常){
系统输出打印ln(e);
}
serialPort.notifyOnDataAvailable(true);
试一试{
serialPort.setSerialPortParams(57600,
SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,
串行端口。奇偶校验(无);
}捕获(不支持的操作异常){
系统输出打印ln(e);
}
readThread=新线程(此线程);
readThread.start();
}
}
}
}
公开募捐{
试一试{
睡眠(20000);
}捕捉(中断异常e){
系统输出打印ln(e);
}
}
public void serialEvent(SerialPortEvent事件){
开关(event.getEventType()){
case SerialPortEvent.BI:
案例SerialPortEvent.OE:
case SerialPortEvent.FE:
case SerialPortEvent.PE:
case SerialPortEvent.CD:
case SerialPortEvent.CTS:
案例SerialPortEvent.DSR:
case SerialPortEvent.RI:
case SerialPortEvent.OUTPUT\u BUFFER\u EMPTY:
打破
案例SerialPortEvent.DATA_可用:
字节[]读缓冲区=新字节[25];
试一试{
while(inputStream.available()>0){
//int numBytes=inputStream.read(readBuffer);
inputStream.read(readBuff