Я также пытался собрать его из .java с помощью javac и запустить файл .class с помощью cmd, но я получаю сообщение об ошибке «Не удалось найти или загрузить основной класс».Код работает так, как и должно быть.Я уже неделю ищу в интернете решение без какой-либо удачи.Заранее спасибо за вашу помощь!Вот мой код:
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)