我从Eclipse应用程序创建了一个.jar。 所以我做出口 - > JAR文件 - >我选择我的Package + lib - >定义我的主类 我的程序使用RxTx lib。
这是我的主框架:
package Main;
import gnu.io.CommPortIdentifier;
import gnu.io.SerialPort;
import java.awt.BorderLayout;
...
public class MainFrame{
public final static String PathLogo1 = "A:/ICOMSSOFT/IVT Corporation/BlueSoleil/icon-logo.png";
public final static String PathLogo2 = "A:/ICOMSSOFT/IVT Corporation/BlueSoleil/ICOMS LOGO.jpg";
public final static String PathBlueSoleilIcon = "A:/ICOMSSOFT/IVT Corporation/BlueSoleil/BluetoothImage.png";
protected static final JComboBox<String> JB_ChoixPortCom = null;
static JButton JB_Connect;
static String ChoixPortCom = null;
//**********************************************************************************
//************************ RxTx Variable *******************************************
//**********************************************************************************
static CommPortIdentifier serialPortId;
static Enumeration<?> enumComm;
static SerialPort serialPort;
static OutputStream outputStream;
static InputStream inputStream;
static Boolean SerialPortCheck = false;
static int baudrate = 9600;
static int dataBits = SerialPort.DATABITS_8;
static int stopBits = SerialPort.STOPBITS_1;
static int parity = SerialPort.PARITY_NONE;
JPanel panel = new JPanel (new GridBagLayout());
JPanel panelSetup = new JPanel(new GridBagLayout());
JPanel panelCommunication = new JPanel(new GridBagLayout());
static JComboBox<String> selection = new JComboBox<String>();
JButton open = new JButton("Open");
JButton close = new JButton("Close");
JButton actualise = new JButton("Actualisé");
JButton envoie = new JButton("Message envoie");
JTextField message = new JTextField();
JCheckBox echo = new JCheckBox("Echo");
static JTextArea recoit = new JTextArea();
JScrollPane recoitJScrollPane = new JScrollPane();
//**********************************************************************************
//**********************************************************************************
//**********************************************************************************
public static void FenetrePrincipal() {
final JFrame JF_Principal = new JFrame("ICOMSSOFT");
JF_Principal.setVisible(true);
JF_Principal.setSize(1000,900);
JF_Principal.setLocationRelativeTo(null);
JF_Principal.setResizable(false);
JF_Principal.setIconImage(Toolkit.getDefaultToolkit().getImage(PathLogo1));
//******************************************************************************************
//******************************** JPANEL NORTH ********************************************
//******************************************************************************************
JPanel JP_North = new JPanel(); // container du BorderLayout.North
JP_North.setPreferredSize(new Dimension(600,150));
JPanel JP_Logo = new JPanel(); // container du JLabel
JP_Logo.setBackground(Color.white);
JLabel JL_Logo = new JLabel(new ImageIcon(PathLogo2));
JP_Logo.add(JL_Logo);
JP_North.add(JP_Logo);
JP_North.setBackground(Color.white);
JP_North.setVisible(true);
//******************************************************************************************
//******************************** JPANEL Center ********************************************
//******************************************************************************************
final JPanel JP_Center = new JPanel();
JP_Center.setPreferredSize(new Dimension(600,700)); // 900- 600 = 300 left
JP_Center.setBackground(Color.white);
final JPanel JP_ChoixConnection = new JPanel();
JP_ChoixConnection.setBorder(BorderFactory.createTitledBorder("Choix Connection : "));
JP_ChoixConnection.setPreferredSize(new Dimension(540, 175));
JP_ChoixConnection.setBackground(Color.white);
ImageIcon pic = new ImageIcon(PathBlueSoleilIcon);
JButton JB_LaunchBlueSoleil = new JButton(pic);
JB_LaunchBlueSoleil.setPreferredSize(new Dimension(100, 135));
JP_ChoixConnection.add(JB_LaunchBlueSoleil);
JP_Center.add(JP_ChoixConnection);
//******************************************************************************************
//******************************** JPANEL South ********************************************
//******************************************************************************************
JPanel JP_South = new JPanel();
JP_South.setBackground(Color.white);
JP_South.setPreferredSize(new Dimension(600,50));
JPanel JP_Bouton = new JPanel();
JP_Bouton.setBackground(Color.white);
JButton JB_Quit = new JButton("Quitter");
JB_Quit.setPreferredSize(new Dimension(85, 35));
JP_South.add(JP_Bouton);
JP_Bouton.add(JB_Quit);
JF_Principal.getContentPane().add(JP_North, BorderLayout.NORTH);
JF_Principal.getContentPane().add(JP_Center, BorderLayout.CENTER);
JF_Principal.getContentPane().add(JP_South, BorderLayout.SOUTH);
JF_Principal.setVisible(true);
//******************************************************************************************
//******************************** Action Listener *****************************************
//******************************************************************************************
JB_LaunchBlueSoleil.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
try {
Bluetooth.Launch(Bluetooth.GetPath());
} catch (IOException e) {
e.printStackTrace();
}
String[] ListPortCom = rxtx.GetPortCom();
int nbrPort = Integer.parseInt(ListPortCom[99]); // String -> int
final JPanel JP_ChoixPortCom = new JPanel();
JP_ChoixPortCom.setBorder(BorderFactory.createTitledBorder("Choix Port Com : "));
JP_ChoixPortCom.setPreferredSize(new Dimension(500, 100));
JP_ChoixPortCom.setBackground(Color.white);
final JComboBox<String> JB_ChoixPortCom = new JComboBox<String>();
for(int i = 0; i < nbrPort; i++) JB_ChoixPortCom.addItem(ListPortCom[i]);
JB_Connect = new JButton("Connect");
JP_ChoixPortCom.add(JB_ChoixPortCom);
JP_ChoixPortCom.add(JB_Connect);
JP_Center.add(JP_ChoixPortCom);
System.out.println("JP_ChoixConnection -> bye bye");
JP_ChoixConnection.setVisible(false);
CommunicationBT.BT(JP_Center);
JB_ChoixPortCom.addItemListener(new ItemListener(){public void itemStateChanged(ItemEvent e) {
ChoixPortCom = (String)JB_ChoixPortCom.getSelectedItem();
}});
JB_Connect.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
if (ChoixPortCom.equals(null) == true ) JOptionPane.showMessageDialog(null, "Choose Port COM");
else rxtx.CheckPort(ChoixPortCom);
}
});
}
});
JB_Quit.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
JF_Principal.dispose();
}
});
}
public static void main(String[] args) throws IOException, InterruptedException {
FenetrePrincipal();
}
}
这就是我称之为GetPortCom的地方:
package Main;
import gnu.io.*; // RXTX
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Enumeration;
public class rxtx {
static CommPortIdentifier serialPortId;
static Enumeration<?> enumComm;
static SerialPort serialPort;
static OutputStream outputStream;
static InputStream inputStream;
int baudrate = 9600;
int dataBits = SerialPort.DATABITS_8;
int stopBits = SerialPort.STOPBITS_1;
int parity = SerialPort.PARITY_NONE;
static Boolean SerialPortCheck = false;
public static String[] GetPortCom(){
String[] List = new String[100]; // List[99] = nbrPort
int nbrPort = 0;
CommPortIdentifier serialPortId;
Enumeration<?> enumComm;
enumComm = CommPortIdentifier.getPortIdentifiers();
while (enumComm.hasMoreElements()) {
serialPortId = (CommPortIdentifier) enumComm.nextElement();
if(serialPortId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
//System.out.println(serialPortId.getName());
List[nbrPort] = serialPortId.getName();
nbrPort++;
}
List[99] = Integer.toString(nbrPort); // Int -> String
}
for (int i = 0; i < nbrPort; i++) {
System.out.println(List[i]);
}
return List;
}
public static boolean CheckPort(String portName){
boolean foundPort = false;
if (SerialPortCheck != false) {
System.out.println("Serial Port déjà ouvert");
return true;
}
enumComm = CommPortIdentifier.getPortIdentifiers();
while(enumComm.hasMoreElements()) {
serialPortId = (CommPortIdentifier) enumComm.nextElement();
if (portName.contentEquals(serialPortId.getName())) {
foundPort = true;
break;
}
}
if (foundPort != true) {
System.out.println("Serialport pas trouver: " + portName);
return false;
}
try {
serialPort = (SerialPort) serialPortId.open("Ouvert + Envoie", 500);
} catch (PortInUseException e) {
System.out.println("Port -> OK");
}
try {
outputStream = serialPort.getOutputStream();
} catch (IOException e) {
System.out.println("Probleme en OutputStream");
}
try {
inputStream = serialPort.getInputStream();
} catch (IOException e) {
System.out.println("Probleme en InputStream");
}
SerialPortCheck = true;
return true;
}
我不明白的是,为什么它在Eclipse中完美运行但没有使用jar,所有路径都是正确的,所以我不明白:/
更多信息:
所以当你点击按钮时,这个应用程序会调用另一个程序(BlueSoleil)。 我的.jar的问题,当我点击按钮时程序正确启动但它阻止了其余部分没有出现。假设通过切换另一个JPanel来改变我的JP_Center(JPanel) - &gt;启动getPortCom,然后使用所有PortCom创建一个JCombobox。