我的程序只能在NetBeans中工作,但是当我将其构建到JAR中时,它就无法工作

时间:2018-10-09 13:30:35

标签: java netbeans arduino

我也尝试使用javac从.java构建它,并使用cmd运行.class文件,但是我一直收到错误消息“找不到或加载主类”。该代码按预期运行。我一直在互联网上搜索一个星期,没有任何运气的解决方案。预先感谢您的帮助!这是我的代码:

package handbrake;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStream;
import gnu.io.CommPortIdentifier; 
import gnu.io.SerialPort;
import gnu.io.SerialPortEvent; 
import gnu.io.SerialPortEventListener; 
import java.util.Enumeration;
import java.awt.Robot;

public class Handbrake implements SerialPortEventListener {
SerialPort serialPort;
    /** The port we're normally going to use. */
private static final String PORT_NAMES[] = { 
        /*"/dev/tty.usbserial-A9007UX1", // Mac OS X
                    "/dev/ttyACM0", // Raspberry Pi
        "/dev/ttyUSB0", // Linux */
        "COM3", // Windows
};
/**
* A BufferedReader which will be fed by a InputStreamReader 
* converting the bytes into characters 
* making the displayed results codepage independent
*/
private BufferedReader 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() {
            // the next line is for Raspberry Pi and 
            // gets us into the while loop and was suggested here was suggested http://www.raspberrypi.org/phpBB3/viewtopic.php?f=81&t=32186
         /*   System.setProperty("gnu.io.rxtx.SerialPorts", "/dev/ttyACM0");
*/
    CommPortIdentifier portId = null;
    Enumeration portEnum = CommPortIdentifier.getPortIdentifiers();

    //First, Find an instance of serial port as set in PORT_NAMES.
    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 = new BufferedReader(new InputStreamReader(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 {
            String inputLine=input.readLine();
            System.out.println(inputLine);

                            Robot keyboard = new Robot();

                            if(inputLine.equals("1")) {
                                keyboard.keyPress(32);
                            } else {
                                keyboard.keyRelease(32);
                            }
        } 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 {
    Handbrake main = new Handbrake();
    main.initialize();
    Thread t=new Thread() {
        public void run() {
            //the following line will keep this app alive for 1000 seconds,
            //waiting for events to occur and responding to them (printing incoming messages to console).
            try {Thread.sleep(1000000);} catch (InterruptedException ie) {}
        }
    };
    t.start();
    System.out.println("Started");
}

}

该代码应该侦听COM端口的特定输入(按下按钮,而arduino通过COM端口发送输入),并且每当我们有该特定输入时,都按下键盘。

当我单击“清理并构建”时,这是NetBeans中的输出窗口

ant -f C:\\Users\\Oppilas\\Documents\\NetBeansProjects\\Handbrake -Dnb.internal.action.name=rebuild clean jar
init:
deps-clean:
Updating property file: C:\Users\Oppilas\Documents\NetBeansProjects\Handbrake\build\built-clean.properties
Deleting directory C:\Users\Oppilas\Documents\NetBeansProjects\Handbrake\build
clean:
init:
deps-jar:
Created dir: C:\Users\Oppilas\Documents\NetBeansProjects\Handbrake\build
Updating property file: C:\Users\Oppilas\Documents\NetBeansProjects\Handbrake\build\built-jar.properties
Created dir: C:\Users\Oppilas\Documents\NetBeansProjects\Handbrake\build\classes
Created dir: C:\Users\Oppilas\Documents\NetBeansProjects\Handbrake\build\empty
Created dir: C:\Users\Oppilas\Documents\NetBeansProjects\Handbrake\build\generated-sources\ap-source-output
Compiling 1 source file to C:\Users\Oppilas\Documents\NetBeansProjects\Handbrake\build\classes
compile:
Created dir: C:\Users\Oppilas\Documents\NetBeansProjects\Handbrake\dist
Copying 1 file to C:\Users\Oppilas\Documents\NetBeansProjects\Handbrake\build
Nothing to copy.
Building jar: C:\Users\Oppilas\Documents\NetBeansProjects\Handbrake\dist\Handbrake.jar
To run this application from the command line without Ant, try:
java -jar "C:\Users\Oppilas\Documents\NetBeansProjects\Handbrake\dist\Handbrake.jar"
jar:
BUILD SUCCESSFUL (total time: 4 seconds)

1 个答案:

答案 0 :(得分:0)

如果要运行已构建的应用程序,则NetBeans使您的生活变得轻松,但是在NetBeans外部运行它并不总是那么简单。但是,有一种方法对我始终有效:

  • 在NetBeans中构建项目。
  • Output 窗口中转到内部版本的输出。
  • 找到文字说明行

      

    要在不使用Ant的情况下从命令行运行此应用程序,请尝试:

  • 转到紧接其下的行,然后将其复制。

  • 打开终端/控制台/命令提示符窗口,粘贴复制的文本并运行它。

这种方法对我而言从未失败,因此,如果对您不起作用,请使用详细信息更新您的帖子。

以下是一些构建输出作为示例。对于该示例,您要复制的行是底部的第四行,从C:\Java\jdk-11/bin/java开始。

ant -f D:\\NB072918\\JavaApplication22 -Dnb.internal.action.name=rebuild clean jar
init:
deps-clean:
Updating property file: D:\NB072918\JavaApplication22\build\built-clean.properties
Deleting directory D:\NB072918\JavaApplication22\build
clean:
init:
deps-jar:
Created dir: D:\NB072918\JavaApplication22\build
Updating property file: D:\NB072918\JavaApplication22\build\built-jar.properties
Created dir: D:\NB072918\JavaApplication22\build\classes
Created dir: D:\NB072918\JavaApplication22\build\empty
Created dir: D:\NB072918\JavaApplication22\build\generated-sources\ap-source-output
Compiling 1 source file to D:\NB072918\JavaApplication22\build\classes
compile:
Created dir: D:\NB072918\JavaApplication22\dist
Copying 1 file to D:\NB072918\JavaApplication22\build
Building jar: D:\NB072918\JavaApplication22\dist\JavaApplication22.jar
To run this application from the command line without Ant, try:
C:\Java\jdk-11/bin/java -p lib\commons-lang3-3.8.1.jar -cp D:\NB072918\JavaApplication22\lib\commons-lang3-3.8.1.jar;D:\NB072918\JavaApplication22\dist\JavaApplication22.jar javaapplication22.JavaApplication22
deploy:
jar:
BUILD SUCCESSFUL (total time: 1 second)