简体   繁体   English

Java Applet 出现错误 java.lang.reflect.InvocationTargetException

[英]Java Applet getting error java.lang.reflect.InvocationTargetException

Just to start with I m just a newbie to java.首先,我只是java的新手。

Below is my applet code, it compiles with a notice (Note: CallApplet.java uses unchecked or unsafe operations. Note: Recompile with -Xlint:unchecked for details.) but no errors.下面是我的小程序代码,它编译时会提示(注意:CallApplet.java 使用了未经检查或不安全的操作。注意:使用 -Xlint:unchecked 重新编译以了解详细信息。)但没有错误。

UPDATE更新

I get this error when i call mprintt method via javascript.当我通过 javascript 调用 mprintt 方法时出现此错误。 As you can see it tries to call DLL instance.如您所见,它尝试调用 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();
    }
}

Below is my html下面是我的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>

As you all can see im using jna.jar to load a custom thermal printer DLL.正如你们所看到的,我正在使用 jna.jar 加载自定义热敏打印机 DLL。

I m getting error "java.lang.reflect.InvocationTargetException", I think it is more so due applet and jna complications.我收到错误“java.lang.reflect.InvocationTargetException”,我认为这更多是由于小程序和 jna 的并发症。

Please let me know how to overcome this and get this applet to print via this DLL.请让我知道如何克服这个问题并通过此 DLL 打印此小程序。

Usually an InvocationTargetException just wraps the root exception.通常 InvocationTargetException 只是包装根异常。 The root exception should be listed in the stack trace or is accessible using the "getCause()" method.根异常应该在堆栈跟踪中列出,或者可以使用“getCause()”方法访问。 Posting the stack trace might help.发布堆栈跟踪可能会有所帮助。

I would try to simplify to find the bug or get the stack trace as tcbcw stated.我会尝试简化以找到错误或获取 tcbcw 所述的堆栈跟踪。 Try just accessing the print services from the applet, does that work?尝试仅从小程序访问打印服务,这行得通吗? Slice the code and find the bug, or get the stack trace, not much more we can do.切片代码并找到错误,或者获取堆栈跟踪,我们无能为力。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM