gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 04:36:06 24 4
gpt4 key购买 nike

首先,我只是 java 的新手。

下面是我的小程序代码,它编译时有一个通知(注意:CallApplet.java 使用未经检查或不安全的操作。注意:使用 -Xlint:unchecked 重新编译以了解详细信息。)但没有错误。

更新

当我通过 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.InitationTargetException”,我认为这更多是由于 applet 和 jna 复杂性造成的。

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

最佳答案

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

关于Java Applet 出现错误 java.lang.reflect.InitationTargetException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13497580/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com