-1

下面是小程序代码,它使用 jna.jar ( https://github.com/twall/jna ) 来访问 system32 中的 DLL 文件。

import javax.swing.*;
import javax.print.*;

import java.security.*;
import java.util.ArrayList;
import com.sun.jna.Library;
import com.sun.jna.Native;
import java.awt.*;
import java.awt.event.*;
import java.io.PrintWriter;
import java.io.StringWriter;

import com.sun.jna.Pointer;
import com.sun.jna.platform.win32.WinDef.HWND;
import com.sun.jna.platform.win32.WinUser;
import com.sun.jna.platform.win32.WinUser.WNDENUMPROC;
import com.sun.jna.win32.StdCallLibrary;

public class CallApplet extends JApplet implements ActionListener {

    /**
     * 
     */
    private static final long serialVersionUID = 4654357272074276081L;
    static JTextField output;
    private final String ButtonText = "Print";

    public void init() {

        Container contentHolder = getContentPane();

        contentHolder.setLayout(new BorderLayout(18,18));

        output = new JTextField(20);
        //add(output);

        contentHolder.add(output, BorderLayout.CENTER);

        JPanel buttonPanel = new JPanel();

        JButton b = new JButton(ButtonText);
        b.addActionListener(this);

        buttonPanel.add(b);
        contentHolder.add(buttonPanel, BorderLayout.SOUTH);
        validate();
    }

    public void actionPerformed(ActionEvent evt)
    {
        //get the text of the button that was pushed
        String command = evt.getActionCommand();

        output.setText(command);

        // if myButton was pressed, output a message
        if(ButtonText.equals(command)) {
            try {
                this.mprintt("TSC TTP-244 Plus");
            } catch (Exception e) {

            }
        }
    }

    public void setMessage(String message) {
        output.setText(message);
    }

    public interface TscLibDll extends Library {
        TscLibDll INSTANCE = (TscLibDll) Native.loadLibrary ("TSCLIB", TscLibDll.class);
        int about ();
        int openport (String pirnterName);
        int closeport ();
        int sendcommand (String printerCommand);
        int setup (String width,String height,String speed,String density,String sensor,String vertical,String offset);
        int downloadpcx (String filename,String image_name);
        int barcode (String x,String y,String type,String height,String readable,String rotation,String narrow,String wide,String code);
        int printerfont (String x,String y,String fonttype,String rotation,String xmul,String ymul,String text);
        int clearbuffer ();
        int printlabel (String set, String copy);
        int formfeed ();
        int nobackfeed ();
        int windowsfont (int x, int y, int fontheight, int rotation, int fontstyle, int fontunderline, String szFaceName, String content);
    }

    public void mprintt(String printer) {

        try {
            //TscLibDll.INSTANCE.about();
            TscLibDll.INSTANCE.openport(printer);
            //TscLibDll.INSTANCE.downloadpcx("C:\\UL.PCX", "UL.PCX");
            TscLibDll.INSTANCE.sendcommand("REM ***** This is a test by JAVA. *****");
            TscLibDll.INSTANCE.setup("35", "15", "3", "8", "0", "3", "-1");
            TscLibDll.INSTANCE.clearbuffer();
            //TscLibDll.INSTANCE.sendcommand("PUTPCX 550,10,\"UL.PCX\"");
            TscLibDll.INSTANCE.printerfont ("290", "8", "3", "0", "1", "1", "ARTICLE NO");
            TscLibDll.INSTANCE.barcode("290", "35", "128", "50", "1", "0", "2", "2", "123456789");
            //TscLibDll.INSTANCE.windowsfont(400, 200, 48, 0, 3, 1, "arial", "DEG 0");
            //TscLibDll.INSTANCE.windowsfont(400, 200, 48, 90, 3, 1, "arial", "DEG 90");
            //TscLibDll.INSTANCE.windowsfont(400, 200, 48, 180, 3, 1, "arial", "DEG 180");
            //TscLibDll.INSTANCE.windowsfont(400, 200, 48, 270, 3, 1, "arial", "DEG 270");
            TscLibDll.INSTANCE.printlabel("1", "1");
            TscLibDll.INSTANCE.closeport();
            output.setText("printed");
        } catch (Exception e) {
            StringWriter sw = new StringWriter();
            PrintWriter pw = new PrintWriter(sw);
            e.printStackTrace(pw);
            output.setText(sw.toString());
        }
    }
}

我的 HTML 代码

<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<applet codebase ="." code="CallApplet.class"
        archive="CallApplet.jar,jna.jar,platform.jar"
        height="100" width="100"/>
</body>
</html>

当我尝试单击打印时,我可以看到加载的小程序和浏览器中的所有摆动元素,它不会做任何事情。在eclipse运行它工作正常。

我知道这是安全问题,所以我还添加了策略(C:\Program Files\Java\jre7\lib\security\java.policy 文件)

grant codeBase "http://localhost:8080/appletproj/*" {
    permission java.security.AllPermission;
};

堆栈跟踪

Exception in thread "AWT-EventQueue-7" java.lang.NoClassDefFoundError: com/sun/jna/Library
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClass(Unknown Source)
    at java.security.SecureClassLoader.defineClass(Unknown Source)
    at java.net.URLClassLoader.defineClass(Unknown Source)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at sun.plugin2.applet.Plugin2ClassLoader.defineClassHelper(Unknown Source)
    at sun.plugin2.applet.Plugin2ClassLoader.access$100(Unknown Source)
    at sun.plugin2.applet.Plugin2ClassLoader$2.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at sun.plugin2.applet.Plugin2ClassLoader.findClassHelper(Unknown Source)
    at sun.plugin2.applet.Applet2ClassLoader.findClass(Unknown Source)
    at sun.plugin2.applet.Plugin2ClassLoader.loadClass0(Unknown Source)
    at sun.plugin2.applet.Plugin2ClassLoader.loadClass(Unknown Source)
    at sun.plugin2.applet.Plugin2ClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at CallApplet.mprintt(CallApplet.java:185)
    at CallApplet.actionPerformed(CallApplet.java:60)
    at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
    at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
    at java.awt.Component.processMouseEvent(Unknown Source)
    at javax.swing.JComponent.processMouseEvent(Unknown Source)
    at java.awt.Component.processEvent(Unknown Source)
    at java.awt.Container.processEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$200(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)
Caused by: java.lang.ClassNotFoundException: com.sun.jna.Library
    at sun.plugin2.applet.Applet2ClassLoader.findClass(Unknown Source)
    at sun.plugin2.applet.Plugin2ClassLoader.loadClass0(Unknown Source)
    at sun.plugin2.applet.Plugin2ClassLoader.loadClass(Unknown Source)
    at sun.plugin2.applet.Plugin2ClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    ... 55 more

但即使这样也无济于事。请指导我哪里出错了。

解决方案

坦率地说,我没有得到解决方案,而是这不是一个安全问题,所以用实际错误创建新问题

因此,要获得我启用了 java 控制台的确切错误(由 andrew 建议),您可以转到此处了解如何操作。

更新

几天后我让它工作了,问题是 jna.jar 和 platform.jar 已经签名所以每当我使用 jarsigner 签名它们时它从来没有工作过,我从来没有遇到任何错误(所以我从来没有看过它们),当我验证时它使用 jarsigner 我想通了。然后我取消签名并使用与我的 jar 相同的密钥对其进行签名(希望它会帮助其他人)

4

1 回答 1

1

..还添加了政策..但即使这样也无济于事。

策略文件很少(如果有的话)是一个好主意。如果需要信任代码,请对存档进行数字签名。

于 2012-12-26T07:25:22.037 回答