2012-12-26 75 views
-1

下面是小程序代碼,它使用jna.jar(https://github.com/twall/jna)來訪問system32中的DLL文件。小程序在eclipse中運行,但不在瀏覽器中 - java安全

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> 

我可以看到裝載的小應用程序和瀏覽器中的所有元素擺動,當我嘗試點擊打印它只是不會做任何事情。在日食運行它工作正常。

我知道這是安全問題,所以我也加入了政策(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 

但即使這樣也無濟於事。請指導我哪裏錯了。

SOLUTION

坦率地說,我沒有得到解決工作,而它不是那麼與實際誤差

產生新的問題是安全問題因此得到確切的錯誤我啓用Java控制檯(由安德魯·建議),你可以go here如何做到這一點。

UPDATE

之後幾天,我得到它的工作,問題是jna.jar和platform.jar它已經簽署所以每當我使用的jarsigner它從來沒有工作,我從來沒有任何錯誤(所以我簽了字從來沒有看過他們),當我用jarsigner驗證它時,我發現了它。然後我簽署了他們,並使用我用我的罐子使用相同的密鑰(希望它會幫助其他人)簽署它

回答

1

..還補充政策..但即使這並沒有幫助。

政策文件很少,如果有的話,一個好主意。如果代碼需要信任,則對檔案進行數字簽名。

+0

你可以指向一些網址,顯示如何使用startssl.com提供的免費證書和數字簽名jar文件 – Sudesh

+0

1)免費證書用於簽署代碼的方式與支付的方式完全相同。 2)我懷疑該網站提及的證書不適用於簽署代碼。 3)AFAIU可用的* only * free * code *簽名證書,由SDK生成。 –

+0

只是爲了讓你知道我已經使用jarsigner簽署了jar(自簽名) – Sudesh

相關問題