我一直在撕掉我的頭髮幾天,現在試圖找出爲什麼這似乎從來沒有工作!首先,這裏是我的配置:試圖從Java Applet將數據寫入串行端口?
的Windows 7 64位系統
JDK 7的x86
JRE 7條86
火狐86
的Rails 3薄擔任
Java設置是這樣的: 「下一代插件」 ISN 't主動(但它不斷重新啓動本身不知何故!!!)
起初我試過RXTX,但我一直在得到「沒有找到類」的錯誤。我現在轉向winjcom。我現在得到的錯誤是這樣的:java.security.AccessControlException: access denied ("java.lang.RuntimePermission" "loadLibrary.winjcom")
。
我也注意到我的服務器日誌因我使用的瀏覽器而異。如果我使用Firefox,當瀏覽器嘗試加載小程序時,不會顯示任何GET日誌(即,當小程序加載失敗時,沒有進一步下載)。如果我在IE9中嘗試,除了「PortWriter.class」GET日誌之外,所有日誌都在那裏......這意味着它由於某種原因沒有被檢索到。
當我避免使用JNLP時,我得到了安全警告彈出窗口,並且沒有任何錯誤......當然,當我運行「發送」方法時出現安全訪問錯誤。但是,當我使用JNLP時,IE加載它並且仍然給出錯誤...但是當我關閉時崩潰(我必須結束iexplorer進程)。 Firefox只是不加載頁面......它停在進度條上。
更新 - 我有事情要做,如果我通過java的安全策略文件繞過安全,小程序將起作用。但是,JNLP不起作用 - 我認爲這就是爲什麼通常在通過applet標籤運行時發生安全錯誤的原因。當我直接訪問JNLP文件時,出現錯誤,指出找不到「PortWriter」類。我的jar有什麼問題嗎?我注意到其他jar文件夾的佈局使得目錄結構完全匹配它們的包佈局(例如,如果包爲com.myname.serialport.PortWriter,則爲「com \ myname \ serialport \ PortWriter.jar」) 。然而,我的jar版面複製我的物理文件夾佈局(例如,「D:\ Files \ Websites \ pos \ assets \ java \ PortWriter.jar」)。這是什麼導致錯誤?我手動更改了jar內容(包括清單文件)以匹配根目錄,但也許我做了錯誤的事情。我也在這個問題中更新了我的JNLP佈局,以說明JaNeLa驗證的最新變化。
這是我的java文件:
import com.engidea.comm.CommPort;
import com.engidea.comm.CommPortIdentifier;
import com.engidea.comm.SerialPort;
import com.engidea.comm.SerialPortEvent;
import com.engidea.comm.SerialPortEventListener;
import com.engidea.win32jcom.WinjcomIdentifier;
import java.io.*;
import java.util.Iterator;
import java.util.List;
import java.applet.*;
import java.security.*;
/*
WinJcom is a native interface to serial ports in java.
Copyright 2007 by Damiano Bolla, [email protected]
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This can be used with commercial products and you are not obliged
to share your work with anybody.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with this library; if not, write to the Free
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
/**
* Simple class that can list system ports and allow IO
*/
public class PortWriter extends Applet
{
private CommPortIdentifier portIdentifier;
private SerialPort serport;
public void init() {System.out.println("Booting...");}
@SuppressWarnings("unchecked")
public void Sends(String port, String message) throws Exception
{
final String com_port = port;
final String send_message = message;
AccessController.doPrivileged(new PrivilegedAction() {
public Object run() {
System.out.println("Begin...");
portIdentifier = new WinjcomIdentifier(0);
System.out.println("Selecting Port...");
selectComport(com_port);
new Thread(new PortReader()).start();
System.out.println("Sending...");
typeSendBytes(send_message);
return true;}
});
}
private void typeSendBytes(String message)
{
try
{
System.out.println("Trying To Send...");
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String aStr = "";
while (aStr != null)
{
aStr = message + "\r\n";
// WARNING: be careful, you shoulod select the encoding !
// This will timeout if you have FLOW CONTROL and theline is stuck !
byte []buf = aStr.getBytes();
serport.write(buf,0,buf.length);
}
}
catch (Exception exc)
{
exc.printStackTrace();
}
}
private SerialPort openPort (String portName)
{
try
{
CommPort aPort = portIdentifier.getCommPort(portName);
aPort.open();
return (SerialPort)aPort;
}
catch (Exception exc)
{
System.out.println("exc="+exc);
exc.printStackTrace();
}
return null;
}
private void selectComport (String portName)
{
try
{
serport = openPort(portName);
serport.setSerialPortParams(9600,8, SerialPort.STOPBITS_2, SerialPort.PARITY_NONE);
serport.enableReceiveTimeout(20000);
serport.setEventListener(new EventListener());
serport.notifyOnDSR(true);
serport.notifyOnCarrierDetect(true);
serport.notifyOnCTS(true);
}
catch (IOException exc)
{
System.out.println("Exc="+exc);
exc.printStackTrace();
}
}
private final class EventListener implements SerialPortEventListener
{
public void serialEvent(SerialPortEvent ev)
{
System.out.println("Got event="+ev);
}
}
private final class PortReader implements Runnable
{
public void run()
{
try
{
// This will timeout if nothing is received in the specified time.
byte []buff = new byte[1];
while ( serport.read(buff,0,buff.length) > 0)
{
// NOTE: you should be checking the encoding !
System.out.print(new String(buff));
}
}
catch (Exception exc)
{
exc.printStackTrace();
}
}
}
}
...和我的JNLP文件:
<?xml version="1.0" encoding="utf-8"?>
<jnlp codebase="http://localhost/assets/" href="PortWriter.jnlp">
<information>
<title>RS232 Communication Applet</title>
<vendor>My Company</vendor>
<description>RS232 Applet for communicating with POS Display Pole</description>
<offline-allowed />
</information>
<security>
<all-permissions/>
</security>
<update check="background" />
<resources>
<jar href="PortWriter.jar" part="com" main="true" />
<jar href="winjcom.jar" part="com" />
<nativelib href="jcomport.jar" part="com" />
</resources>
<applet-desc
name="RS232 Communication Applet"
main-class="PortWriter"
width="1" height="1" />
</jnlp>
...和我的HTML:
<applet id="SerialPort" width="1" height="1" codebase="/assets/" code="PortWriter.class" archive="PortWriter.jar">
<param name="jnlp_href" value="PortWriter.jnlp" />
</applet>
我如何讓這些東西起作用?我是一個Java新手,但我只想得到一個可行的解決方案。這是我在Rails中製作的公司的POS。
最後的文件是:
在服務器的/資產/ JAVA /:
1)jcomport.jar(未簽名...)
2)PortWriter.class(和所有相關的類文件)
3)PortWriter.jar
4)PortWriter。JNLP
在本地HD在%Java主%/
1)/lib/ext/jcomport.jar(未簽名)
2)/bin/winjcom.dll
JNLP無效。嘗試使用[JaNeLA]驗證它(http://pscode.org/janela/)。 –
我做了,最終得出了我答案中指定的佈局......雖然這不一定是問題。 – JakeTheSnake