我有一个Web应用程序,正在尝试使用Applet从客户端(浏览器)读取串行端口
HTML页面源:

<!DOCTYPE html>
<html>
<head>
<script src="http://www.java.com/js/deployJava.js"></script>
<script type="text/javascript">
    var attributes = {
        code : 'SampleBalanca.class',
        archive : 'sampleBalanca.jar',
        width : 100,
        height : 100,
        id : 'SampleBalanca'
    };
    var parameters = {
        jnlp_href : 'sample-balanca.jnlp'
    };
    deployJava.runApplet(attributes, parameters, '1.7');

    function peso() {
        alert(document.SampleBalanca.peso());
    }
</script>
</head>
<body>
    <input type="button" name="listbutton" value="Show" onclick="peso();" />
</body>
</html>


sampleBalanca.jar的来源是:

import gnu.io.CommPortIdentifier;
import gnu.io.SerialPort;

import java.applet.Applet;

public class SampleBalanca extends Applet {

    private static final long serialVersionUID = 1L;
    SampleBalanca.SerialThread thread;
    SerialPort serialPort;
    String value;

    public String peso() {

        try {
            CommPortIdentifier portIdentifier = CommPortIdentifier.getPortIdentifier("COM1");
            serialPort = (SerialPort) portIdentifier.open("ListPortClass", 1);
            serialPort.setSerialPortParams(4800, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
        } catch (Exception e) {
            e.printStackTrace();
        }

        this.thread = new SampleBalanca.SerialThread();
        this.thread.start();
        return this.value;
    }

    class SerialThread extends Thread {
        public void run() {

            try {
                CommPortIdentifier portIdentifier = CommPortIdentifier.getPortIdentifier("COM1");
                if (portIdentifier.isCurrentlyOwned())
                    System.out.println("Port in use!");

                else {

                    serialPort.getOutputStream().write(5);

                    // serialPort.getOutputStream().flush();

                    Thread.sleep(50);
                    serialPort.getInputStream().read();

                    byte mBytesIn[] = new byte[5];
                    // int n = serialPort.getInputStream().read(mBytesIn);
                    value = new String(mBytesIn);
                    serialPort.close();
                }
            } catch (Exception ex) {
                System.out.println("Exception : " + ex.getMessage());
            }
        }
    }
}


我的sample-balanca.jnlp文件是:

<jnlp spec="1.0+" codebase="path\\to\\project\\src\\main\\webapp\\applet\\" href="path\\to\\project\\src\\main\\webapp\\applet\\sample-balanca.jnlp">
        <information>
        <title>sampleBalanca</title>
            <vendor>Lad</vendor>
    </information>
    <resources>
                    <j2se version="1.7" href="http://java.sun.com/products/autodl/j2se" />
                    <jar href="sampleBalanca.jar" main="true" />
    </resources>
    <security>
             <all-permissions />
    </security>
    <applet-desc name="Sample Balanca Applet" main-class="SampleBalanca" width="100" height="100"></applet-desc>
</jnlp>


当我从浏览器打开小程序时,出现此错误:

java.lang.NullPointerException
    at sun.plugin2.applet.JNLP2Manager.getAppInfo(Unknown Source)
    at sun.plugin2.applet.Plugin2Manager$AppletExecutionRunnable.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)


有人知道错误在哪里吗?

最佳答案

我将所有文件保存在同一目录中,因此更改了codebase=""href="sample-balanca.jnlp"
下一步是签署我的小程序。此后它应该工作良好。

09-10 03:10