Java Applet获取错误java.lang.reflect.InvocationTargetException

时间:2012-11-21 16:22:40

标签: java applet jna

刚开始我只是java的新手。

下面是我的applet代码,它通过通知进行编译(注意:CallApplet.java使用未经检查或不安全的操作。注意:使用-Xlint重新编译:未选中以获取详细信息。)但没有错误。

更新

当我通过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>

大家可以看到我使用jna.jar加载自定义热敏打印机DLL。

我收到错误“java.lang.reflect.InvocationTargetException”,我认为更多是因为applet和jna的并发症。

请让我知道如何克服这个问题,并通过此DLL打印此小程序。

2 个答案:

答案 0 :(得分:2)

通常InvocationTargetException只包装根异常。根异常应列在堆栈跟踪中,或者可以使用“getCause()”方法访问。发布堆栈跟踪可能有所帮助。

答案 1 :(得分:0)

我会尝试简化以查找错误或获取堆栈跟踪,如tcbcw所述。尝试从applet访问打印服务,这有用吗?切片代码并找到错误,或获得堆栈跟踪,我们无法做更多。