JAVA原碼可以執行但是Jar無法順利執行 |
尚未結案
|
yves7878
一般會員 發表:1 回覆:0 積分:0 註冊:2019-10-04 發送簡訊給我 |
各位高手你們好
我是新來的, 遇到一個問題我解不了, 我也google過也試過一些方法, 但是都沒用, 還麻煩各位高手給我指點, 先謝謝各位高手了 我的問題是這樣的: 我寫了一支和機械設備通訊的程式, 這個程式是透過socket將XML格式的資料傳出去並接收機器的資料, 但是很奇怪的是我用我的原始程式和機器做通訊時是正常的, 但是我把程式打包成JAR後(我用的IDE是Eclipse)程式會卡在丟資料和接收資料的程式碼, 但是console卻沒秀出任何錯誤訊息, 我本來以為是ManiFest的問題, 但是改了好多版本都不行, 我將JAR檔附上, 裡面有我編寫的MANIFEST檔再幫我看看可以嗎? 以下是我處理資料的程式碼: package idv.xml.serverclient; import java.awt.Color; import java.io.BufferedReader; import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.PrintWriter; import java.net.ServerSocket; import java.net.Socket; import javax.swing.SwingUtilities; import javax.swing.text.BadLocationException; import javax.swing.text.SimpleAttributeSet; import javax.swing.text.StyleConstants; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import javax.xml.transform.TransformerFactoryConfigurationError; import org.jdom2.Document; import org.jdom2.output.XMLOutputter; //import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.NodeList; import org.xml.sax.SAXException; public class Server1 extends Thread { private Socket mySocket; private ServerSocket myServer; private int intPort; private ReadFile readFile; private ReadData ReadData; private String isContinue = "0"; WriteFile wftolog = new WriteFile(); public Server1() { // 預設建構子 } public Server1(int intPort) { // super(); this.intPort = intPort; } @Override public void run() { // TODO Auto-generated method stub super.run(); while (true) { readFile = new ReadFile(); try { myServer = new ServerSocket(intPort); System.out.println("Server started at port:" intPort); // 等待Client的連接 System.out.println("Waiting for Client Connet....."); wftolog.addLog("Waiting for Client Connet....."); // 設置監聽器等待client的連線 mySocket = myServer.accept(); SwingUtilities.invokeLater(new Runnable() { @Override public void run() { // TODO Auto-generated method stub try { SimpleAttributeSet keyWord = new SimpleAttributeSet(); StyleConstants.setForeground(keyWord, Color.RED); StyleConstants.setBackground(keyWord, Color.YELLOW); StyleConstants.setBold(keyWord, true); HMI_Window.doc.insertString(0, "工作中........\n", keyWord); } catch (BadLocationException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } }); } catch (NullPointerException e) { e.getStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println("Accept Client " mySocket); System.out.println("------Connection Created--------"); wftolog.addLog("連線成功, 工作中........"); if (ReadFile.isPTP == 0) { System.out.println("進去讀LIN資料串"); readBufferLIN(); wftolog.addLog("傳輸LIN資料成功。"); } else { //確認是否是最後一個檔案 if (ReadFile.fileCounter == (idv.xml.serverclient.ReadData.fileNumber)) { isContinue = "1"; } System.out.println("進去讀PTP資料串"); readBufferPTP(); //程式是在這裡停住, 很奇怪的是try catch裡的程式碼都不會執行, 會直接跳到finally關閉socket, 那client端 的程式因為沒收到資料封包就跳錯誤了 wftolog.addLog("傳輸PTP資料成功。"); System.out.println("readFile.fileCounter=" (ReadFile.fileCounter-1) ";readData.fileNumber=" (idv.xml.serverclient.ReadData.fileNumber - 1)); if (ReadFile.fileCounter == (idv.xml.serverclient.ReadData.fileNumber)) { break; } } // 完成連線 System.out.println("success"); wftolog.addLog("完成資料傳輸"); } } public void readBufferLIN() { // ----------- XML傳送資料 (JDOM)----------- try { // 生成Document物件 Document docJDOM = new Document(); XMLOutputter XMLOut = new XMLOutputter(); // 生成XML字符串,即XML文件的内容,包含很多换行符 String xmlString = XMLOut.outputString(docJDOM); xmlString ="" "\n" "Example message" "\n" "" "\n" " "A2=\"" ReadFile.A2[0] "\"" " " "A3=\"" ReadFile.A3[0] "\"" " " "A4=\"" ReadFile.A4[0] "\"" " " "A5=\"" ReadFile.A5[0] "\"" " " "A6=\"" ReadFile.A6[0] "\"" " " "E1=\"" ReadFile.E1[0] "\"" " " "E2=\"" ReadFile.E2[0] "\"" " " "E3=\"" ReadFile.E3[0] "\"" " " "E4=\"" ReadFile.E4[0] "\"" " " "E5=\"" ReadFile.E5[0] "\"" " " "E6=\"" ReadFile.E6[0] "\"" "/>" "\n" "" "\n" "0.9842" "\n" "" "\n" "" "\n" "" String.valueOf(ReadFile.lineNumber) "" "\n" "" "\n" "0" "\n" "" "\n" "" "\n" "" "\n" "" "\n" "" "\n" "" "\n" "" "\n" "" "\n" "" "\n" "" "\n" "" "\n" "" "\n" "" "\n" "" "\n" "" "\n" "" "\n" "" "\n" "" "\n" "" "\n" "" "\n" "" "\n" "" "\n" "" "\n" "" "\n" "" "\n" "" "\n" "" "\n" "" "\n" "" "\n" "" "\n" "" "\n" "" "\n" "" "\n" "" "\n" "" "\n" "" "\n" "" "\n" "" "\n" "" "\n" "" "\n" "" "\n" "" "\n" "" "\n" "" "\n" "" "\n" "" "\n" "" "\n" "" "\n" "" "\n" "" "\n" "" "\n" "" "\n" "" "\n" "" "\n" "" "\n" "" "\n" "" "\n" "" "\n" "" "\n" "" "\n" "" "\n" "" "\n" "" "\n" "" "\n" "" "\n" "" "\n" "" "\n" "" "\n" "" "\n" "" "\n" "" "\n" "" "\n" "" "\n" "" "\n" "" "\n" "" "\n" "" "\n" "" "\n" "" "\n" "" "\n" "" "\n" "" "\n" "" "\n" "" "\n" "" "\n" "" "\n" "" "\n" "" "\n" "" "\n" "" "\n" "" "\n" "" "\n" "" "\n" "" "\n" "" "\n" "" "\n" "" "\n" "" "\n" "" "\n" "" "\n" "" "\n" "" "\n" "" "\n" "Taginfo in attributes" "\n" "2912" "\n" ""; // 发送XML字符串 PrintWriter out = new PrintWriter(mySocket.getOutputStream(), true); out.println(xmlString); out.flush(); // ------------ XML接收資料 (DOM)------------ InputStream is = mySocket.getInputStream(); BufferedReader br = new BufferedReader(new InputStreamReader(is)); String brString = br.readLine(); System.out.println(brString); // 將STRING轉成NODE物件 // 得到DocumentBuilderFactory的實體 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); // 得到DocumentBuilder的實體 DocumentBuilder db = dbf.newDocumentBuilder(); // 得到Document的實體 org.w3c.dom.Document document = db.parse(new ByteArrayInputStream(brString.getBytes())); // 得到Element的實體, 此物件是實際的XML內容 Element el = document.getDocumentElement(); // 得到節點的資料 NodeList nl = el.getElementsByTagName("LastPos"); int nlInt = nl.getLength(); System.out.println(nlInt); // X的值為LastPos的屬性所以要先getAttributes() String xPosString = nl.item(0).getAttributes().getNamedItem("X").getNodeValue(); String yPosString = nl.item(0).getAttributes().getNamedItem("Y").getNodeValue(); System.out.println("X=" xPosString " Y=" yPosString); } catch (IOException | ParserConfigurationException | SAXException | TransformerFactoryConfigurationError e) { // TODO Auto-generated catch block wftolog.addLog("ERROR:傳輸LIN資料失敗。"); e.printStackTrace(); } finally { try { mySocket.close(); myServer.close(); } catch (IOException e) { // TODO Auto-generated catch block wftolog.addLog("ERROR:傳輸LIN資料成功, 關閉通道失敗。"); e.printStackTrace(); } } } public void readBufferPTP() { // ----------- XML傳送資料 (JDOM)----------- try { // 生成Document物件 Document docJDOM = new Document(); XMLOutputter XMLOut = new XMLOutputter(); // 生成XML字符串,即XML文件的内容,包含很多换行符 String xmlString = XMLOut.outputString(docJDOM); xmlString = "" "\n" "Example message" "\n" "" "\n" " "A2=\"" readFile.A2[0] "\"" " " "A3=\"" readFile.A3[0] "\"" " " "A4=\"" readFile.A4[0] "\"" " " "A5=\"" readFile.A5[0] "\"" " " "A6=\"" readFile.A6[0] "\"" " " "E1=\"" readFile.E1[0] "\"" " " "E2=\"" readFile.E2[0] "\"" " " "E3=\"" readFile.E3[0] "\"" " " "E4=\"" readFile.E4[0] "\"" " " "E5=\"" readFile.E5[0] "\"" " " "E6=\"" readFile.E6[0] "\"" "/>" "\n" "" "\n" "0.9842" "\n" "" "\n" "" "\n" "" String.valueOf(readFile.lineNumber) "" "\n" "" "\n" "1" "\n" "" "\n" "" "\n" "" "\n" "" "\n" "" "\n" "" "\n" "" "\n" "" "\n" "" "\n" "" "\n" "" "\n" "" "\n" "" "\n" "" "\n" "" "\n" "" "\n" "" "\n" "" "\n" "" "\n" "" "\n" "" "\n" "" "\n" "" "\n" "" "\n" "" "\n" "" "\n" "" "\n" "" "\n" "" "\n" "" "\n" "" "\n" "" "\n" "" "\n" "" "\n" "" "\n" "" "\n" "" "\n" "" "\n" "" "\n" "" "\n" "" "\n" "" "\n" "" "\n" "" "\n" "" "\n" "" "\n" "" "\n" "" "\n" "" "\n" "" "\n" "" "\n" "" "\n" "" "\n" "" "\n" "" "\n" "" "\n" "" "\n" "" "\n" "" "\n" "" "\n" "" "\n" "" "\n" "" "\n" "" "\n" "" "\n" "" "\n" "" "\n" "" "\n" "" "\n" "" "\n" "" "\n" "" "\n" "" "\n" "" "\n" "" "\n" "" "\n" "" "\n" "" "\n" "" "\n" "" "\n" "" "\n" "" "\n" "" "\n" "" "\n" "" "\n" "" "\n" "" "\n" "" "\n" "" "\n" "" "\n" "" "\n" "" "\n" "" "\n" "" "\n" "" "\n" "" "\n" "" "\n" "" "\n" "" "\n" "" "\n" "" "\n" "" "\n" "" "\n" "Taginfo in attributes" "\n" "2912" "\n" ""; // 发送XML字符串 PrintWriter out = new PrintWriter(mySocket.getOutputStream(), true); out.println(xmlString); out.flush(); // ------------ XML接收資料 (DOM)------------ InputStream is = mySocket.getInputStream(); BufferedReader br = new BufferedReader(new InputStreamReader(is)); String brString = br.readLine(); System.out.println(brString); // 將STRING轉成NODE物件 // 得到DocumentBuilderFactory的實體 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); // 得到DocumentBuilder的實體 DocumentBuilder db = dbf.newDocumentBuilder(); // 得到Document的實體 org.w3c.dom.Document document = db.parse(new ByteArrayInputStream(brString.getBytes())); // 得到Element的實體, 此物件是實際的XML內容 Element el = document.getDocumentElement(); // 得到節點的資料 NodeList nl = el.getElementsByTagName("LastPos"); int nlInt = nl.getLength(); System.out.println(nlInt); // X的值為LastPos的屬性所以要先getAttributes() String xPosString = nl.item(0).getAttributes().getNamedItem("X").getNodeValue(); String yPosString = nl.item(0).getAttributes().getNamedItem("Y").getNodeValue(); System.out.println("X=" xPosString " Y=" yPosString); } catch (IOException | ParserConfigurationException | SAXException | TransformerFactoryConfigurationError e) { // TODO Auto-generated catch block wftolog.addLog("ERROR:傳輸PTP資料失敗。\n"); e.printStackTrace(); } finally { try { mySocket.close(); myServer.close(); } catch (IOException e) { // TODO Auto-generated catch block wftolog.addLog("ERROR:傳輸PTP資料成功, 關閉通道失敗。\n"); e.printStackTrace(); } } } }
------
馮耀樟 |
本站聲明 |
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。 2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。 3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇! |