java 我的程序只能在NetBeans中运行,但当我将其构建到JAR中时,它无法运行

vaqhlq81  于 2023-01-11  发布在  Java
关注(0)|答案(2)|浏览(106)

我也试过用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)
vbkedwbf

vbkedwbf1#

如果您想运行您构建的应用程序,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)
pjngdqdw

pjngdqdw2#

我知道这是很久以前,但我最近有同样的问题,我必须做的是创建项目为“Java与Ant”,而不是“Java与Maven”,我成功地使Jar文件在netbeans之外打开。
希望这能帮助到一些人。

相关问题