我能';t生成使用RXTX的jar文件
我制作了以下源代码,并成功地通过RS232进行通信。 然后,我尝试将jar文件作为源代码。我可以创建可运行的jar文件。但当我执行双击时,它无法执行程序 在另一台PC中(如果我使用我的PC,jar文件会正确执行) 我应该怎么做才能解决这个问题。 我读了一些涉及的问题,但我不知道我该怎么办。 所以,请告诉我如何制作jar文件的详细过程 [我以前制作过其他程序的jar文件,但后来它没有包含外部jar文件。在这一次,我使用了一个jar文件(RxTxcomm.jar)和两个dll文件来制作程序。所以我不知道如何相互链接。] 我的源代码 [MyRxTx.java]我能';t生成使用RXTX的jar文件,jar,rxtx,Jar,Rxtx,我制作了以下源代码,并成功地通过RS232进行通信。 然后,我尝试将jar文件作为源代码。我可以创建可运行的jar文件。但当我执行双击时,它无法执行程序 在另一台PC中(如果我使用我的PC,jar文件会正确执行) 我应该怎么做才能解决这个问题。 我读了一些涉及的问题,但我不知道我该怎么办。 所以,请告诉我如何制作jar文件的详细过程 [我以前制作过其他程序的jar文件,但后来它没有包含外部jar文件。在这一次,我使用了一个jar文件(RxTxcomm.jar)和两个dll文件来制作程序。所以我不
package sample;
import javafx.application.Platform;
import gnu.io.CommPortIdentifier;
import gnu.io.SerialPort;
import gnu.io.SerialPortEvent;
import gnu.io.SerialPortEventListener;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Enumeration;
public class MyRxTx implements SerialPortEventListener {
SerialPort serialPort;
Main sample_serial;
public byte[] SendBuffer;
/**
* The port we're normally going to use.
*/
private final String PORT_NAMES[] = {
"COM29",
"COM30",
"COM31",
};
private InputStream inputStream;
private OutputStream outputStream;
private static final int TIME_OUT = 2000;
private static final int Baud_Rate = 115200;
public void initialize() throws InterruptedException {
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;
} else {
System.out.println("Port Name: " + portId.getName() + "\n"
+ "Current Owner: " + portId.getCurrentOwner() + "\n"
+ "Port Type: " + portId.getPortType());
}
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(
Baud_Rate,
SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE);
// open the streams
inputStream = serialPort.getInputStream();
outputStream = serialPort.getOutputStream();
serialPort.addEventListener(this);
serialPort.notifyOnDataAvailable(true);
} catch (Exception e) {
System.err.println(e.toString());
}
SendBuffer=new byte[]{(byte)0x41,(byte)0x54};
write(SendBuffer);
}
@Override
public void serialEvent(SerialPortEvent Serial_event){
int received_data = 0;
StringBuffer buffer = new StringBuffer();
if(Serial_event.getEventType() == SerialPortEvent.DATA_AVAILABLE){
while(true){
try{
received_data = inputStream.read();
if(received_data == -1) break;
if((char)received_data != '\r'){
buffer.append((char)received_data);
}
else{
buffer.append('\n');
break;
}
}
catch(IOException ex){}
}//while()文ここまで。
System.out.println("Receive Data:"+buffer);
Platform.runLater( () ->{
sample_serial.label_receive.setText("Receive Data:"+buffer);
});
}
}
protected void write(byte[] buffer) throws InterruptedException{
try {
outputStream.write(buffer);
outputStream.flush();
} catch (IOException e) {
System.err.println("Cannot write:" + e.getMessage());
}
}
/**
* 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();
}
}
}
[Main.java]
package sample;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXMLLoader;
import javafx.geometry.Pos;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.HBox;
import javafx.scene.layout.HBoxBuilder;
import javafx.scene.layout.VBox;
import javafx.scene.layout.VBoxBuilder;
import javafx.scene.text.Font;
import javafx.scene.text.FontPosture;
import javafx.stage.Stage;
import javafx.stage.StageBuilder;
import java.util.logging.Level;
import java.util.logging.Logger;
public class Main extends Application {
MyRxTx myRxTx;
private TextField text_Send;
public static Label label_receive;
@Override
public void start(Stage primaryStage) throws InterruptedException {
Button btn_send = ButtonBuilder.create().text("Send")
.prefWidth(100).alignment(Pos.CENTER)
.id("Send").build();
text_Send = TextFieldBuilder.create().text("Sending message").alignment(Pos.CENTER)
.prefWidth(150).build();
Font Font_receive = Font.font("Arial", FontPosture.REGULAR,20);
label_receive = LabelBuilder.create().text("Recieved message")
.alignment(Pos.TOP_CENTER).prefWidth(400).font(Font_receive).build();
HBox root1 = HBoxBuilder.create().spacing(100).children(text_Send,btn_send).build();
HBox root2 = HBoxBuilder.create().spacing(100).children(label_receive).build();
VBox root3 = VBoxBuilder.create().spacing(15).children(root1,root2).build();
Scene scene = new Scene(root3);
myRxTx = new MyRxTx();
myRxTx.initialize();
scene.addEventHandler(ActionEvent.ACTION,actionHandler);
primaryStage = StageBuilder.create().width(410).height(340).scene(scene).title("Serial Communication Test Tool").build();
primaryStage.show();
}
EventHandler<ActionEvent> actionHandler = new EventHandler<ActionEvent>(){
public void handle (ActionEvent e){
Button src =(Button)e.getTarget();
String text = src.getId();
System.out.println("Select Button:"+text);
String text_send = text_Send.getText();
if(text.equals("Send")){
try {
myRxTx.write(text_send.getBytes());
} catch (InterruptedException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
};
/* main*/
public static void main(String[] args) {
launch(args);
}
}
包装样品;
导入javafx.application.application;
导入javafx.event.ActionEvent;
导入javafx.event.EventHandler;
导入javafx.fxml.fxmloader;
导入javafx.geometry.Pos;
导入javafx.scene.Parent;
导入javafx.scene.scene;
导入javafx.scene.control.*;
导入javafx.scene.layout.HBox;
导入javafx.scene.layout.HBoxBuilder;
导入javafx.scene.layout.VBox;
导入javafx.scene.layout.VBoxBuilder;
导入javafx.scene.text.Font;
导入javafx.scene.text.fontpostation;
导入javafx.stage.stage;
导入javafx.stage.StageBuilder;
导入java.util.logging.Level;
导入java.util.logging.Logger;
公共类主扩展应用程序{
MyRxTx MyRxTx;
私有文本字段文本_发送;
公共静态标签接收;
@凌驾
public void start(Stage primaryStage)抛出中断异常{
Button btn_send=ButtonBuilder.create().text(“发送”)
.预调宽度(100).对齐(位置中心)
.id(“发送”).build();
text_Send=TextFieldBuilder.create().text(“发送消息”).alignment(位置中心)
.prefWidth(150.build();
Font\u receive=Font.Font(“Arial”,fontpostation.REGULAR,20);
label_receive=LabelBuilder.create().text(“接收到的消息”)
.alignment(Pos.TOP_居中)。prefWidth(400)。font(font_receive)。build();
HBox root1=HBoxBuilder.create().间距(100).子项(text_Send,btn_Send).build();
HBox root2=HBoxBuilder.create().spating(100).children(label_receive).build();
VBox root3=VBoxBuilder.create().spating(15).children(root1,root2.build();
场景=新场景(root3);
myRxTx=新的myRxTx();
初始化();
scene.addEventHandler(ActionEvent.ACTION,actionHandler);
primaryStage=StageBuilder.create().width(410).height(340).scene(scene).title(“串行通信测试工具”).build();
primaryStage.show();
}
EventHandler actionHandler=新的EventHandler(){
公共无效句柄(ActionEvent e){
按钮src=(按钮)e.getTarget();
String text=src.getId();
System.out.println(“选择按钮:“+文本”);
字符串text\u send=text\u send.getText();
if(text.equals(“发送”)){
试一试{
write(text_send.getBytes());
}捕获(中断异常例外){
Logger.getLogger(Main.class.getName()).log(Level.SEVERE,null,ex);
}
}
}
};
/*主要*/
公共静态void main(字符串[]args){
发射(args);
}
}
在我看来,当试图在initialize()方法中检索CommPortIdentifier时,硬编码的端口名称[]数组可能会导致一些问题。在不同的PC上,这些端口名可能不匹配,使您的portId为null。在我看来,硬编码的port_names[]数组在尝试在initialize()方法中检索CommPortIdentifier时可能会导致一些问题。在不同的PC上,这些端口名可能不匹配,导致portId为空。