简体   繁体   中英

Java Applet getting error java.lang.reflect.InvocationTargetException

Just to start with I m just a newbie to 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.

UPDATE

I get this error when i call mprintt method via javascript. As you can see it tries to call DLL instance.

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>
    <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.

I m getting error "java.lang.reflect.InvocationTargetException", I think it is more so due applet and jna complications.

Please let me know how to overcome this and get this applet to print via this DLL.

Usually an InvocationTargetException just wraps the root exception. The root exception should be listed in the stack trace or is accessible using the "getCause()" method. Posting the stack trace might help.

I would try to simplify to find the bug or get the stack trace as tcbcw stated. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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