2012-11-21 91 views
1

剛開始的時候我只是一個java的新手。 (注意:CallApplet.java使用未檢查或不安全的操作。注意:使用-Xlint重新編譯:未檢查細節。)但沒有錯誤。Java Applet獲取錯誤java.lang.reflect.InvocationTargetException

UPDATE

當我通過JavaScript調用mprintt方法我得到這個錯誤。正如你可以看到它試圖調用DLL實例。

import javax.swing.*; 
import javax.print.*; 
import java.util.ArrayList; 
import com.sun.jna.Library; 
import com.sun.jna.Native; 
import java.awt.print.*; 
import java.security.*; 

public class CallApplet extends JApplet { 

    JTextField output; 

    public void init() { 
     output = new JTextField(20); 
     add(output); 
     validate(); 
    } 

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

    public String getPrinters() { 
     PrintService[] printServices = PrintServiceLookup.lookupPrintServices(null, null); 
     ArrayList<String> myStringArray = new ArrayList<String>(); 

     String s = new String(); 

     int i = 0; 

     for (PrintService printer : printServices) { 
      myStringArray.add(printer.getName()); 

      if (i > 0) { 
       s = s + ","; 
      } 

      s = s + "\"" + printer.getName() + "\""; 

      i++; 
     } 

     s = "[" + s + "]"; 

     String[] simpleArray = new String[ myStringArray.size() ]; 
     myStringArray.toArray(simpleArray); 

     return s; 
    } 

    public void jPrint(String printer) { 

     if (printer.length() <= 0) { 
      return; 
     } 

     //output.setText(printer); 

     //TcsPrint tcsPrinter = new TcsPrint(); 
     //tcsPrinter.print(printer); 
    } 

    public interface TscLibDll extends Library { 
     TscLibDll INSTANCE = (TscLibDll) AccessController.doPrivileged(new PrivilegedAction() { 
      public Object run() { 
       return 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) { 

     TscLibDll.INSTANCE.openport(printer); 
     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.printerfont ("290", "8", "3", "0", "1", "1", "ARTICLE NO"); 
     TscLibDll.INSTANCE.barcode("290", "35", "128", "50", "1", "0", "2", "2", "123456789"); 
     TscLibDll.INSTANCE.printlabel("1", "1"); 
     TscLibDll.INSTANCE.closeport(); 
    } 
} 

下面是我的HTML

<html> 
    <body> 
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script> 
    <script type='text/javascript'> 

    var printers; 

    function selectPrinter() 
    { 
     applet = document.getElementById('output'); 
     printers = applet.getPrinters(); 

    } 

    </script> 

    <br> 
     <applet 
      id='output' 
      code='CallApplet.class' 
      archive='.,./jna.jar' 
      width=100 
      height=100> 
     </applet> 

     <input type="button" onclick="selectPrinter()" value="Show Printers" /> <input type="button" onclick="goPrint" value="Print" /> 
     <select name="printers"> 
     </select> 

     dsdsdg 
    </body> 
</html> 

大家都可以看到I M使用jna.jar加載自定義的熱敏打印機DLL。

我得到錯誤「java.lang.reflect.InvocationTargetException」,我認爲它更是如此應有的小程序和jna併發症。

請讓我知道如何克服這一點,並獲得此小程序通過此DLL打印。

回答

2

通常情況下,InvocationTargetException僅包裝根異常。根異常應該列在堆棧跟蹤中,或者可以使用「getCause()」方法訪問。發佈堆棧跟蹤可能會有所幫助。

+0

我在調用mprintt方法時出現此錯誤。正如你可以看到它試圖調用DLL實例。 – Sudesh

0

我會盡量簡化查找錯誤或獲取堆棧跟蹤tcbcw陳述。試試從applet訪問打印服務,這是否有效?切片代碼並找到錯誤,或者獲取堆棧跟蹤,我們不能做更多事情。

相關問題