java - 我的程序只能在 NetBeans 中运行,但是当我将它构建到 JAR 中时它不起作用
问题描述
我还尝试使用 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)
解决方案
如果您想运行已构建的应用程序,NetBeans 让您的生活变得轻松,但在 NetBeans 之外运行它并不总是那么简单。但是,有一种方法一直对我有用:
- 在 NetBeans 中构建您的项目。
- 在“输出”窗口中转到构建的输出。
找到说明文字的行
要在没有 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)
推荐阅读
- javascript - React 钩子改变 postMessage 上的状态
- angular - 重用组件上的单向数据绑定
- android - 未解决的参考:FragmentTitleBinding
- java - 未找到 Maven 插件 - IntelliJ IDEA 2019.2(社区版)
- ruby - 如何仅从 1 个 API 中排除葡萄中间件?
- java - @ModelAtribute 在 Spring 框架中是可选的吗?
- android - 如何解决:“无法将活动投射到片段
- react-native - react-native-swiper 下一个/上一个按钮事件
- python - 我发现我可以像这样使用带有类方法的装饰器内部。但是好像错了?
- c# - 服务器返回错误:404 not found