gpt4 book ai didi

Java/HTML - 抛出 InvocationTargetException

转载 作者:太空宇宙 更新时间:2023-11-04 15:31:50 27 4
gpt4 key购买 nike

我正在写一个状态检查器,它是一个 java web-start 小程序。我可以在我的电脑上运行它,但是它运行得很好;当我在网页上运行它时,出现运行时错误 java.lang.reflect.InvocationTargetException。这是我用来运行 jar 文件的 html 文件

<html>
<head>
<applet code="StatusCheckerMain" width=380 height=172 archive="Launch.jar">
<param name="servername"
value="Evolution-X"/>
<param name="ip"
value="127.0.0.1"/>
<param name="port"
value="43594"/>
Java is not installed on your machine or your browser does not allow Java Web-Start Applets to run.<br /><br />Get the latest Java technology at <a href="http://www.java.com/">http://www.Java.com/</a>
</applet>
</head>
</html>

这是我的源文件

import java.awt.*;
import java.io.*;
import java.net.*;
import javax.swing.*;
/**
*
* @author Dan
*/
public class GUIChecker extends JApplet {

public GUIChecker() { }

public void init() {
try {
java.awt.EventQueue.invokeAndWait(new Runnable() {
public void run() {
initComponents();
}
});
} catch (Exception ex) {
ex.printStackTrace();
}
}
private String getServerName() {
if (isActive()) {
return (getParameter("servername"));
} else {
return "Default";
}
}
private String getIP() {
if (isActive()) {
return (getParameter("ip"));
} else {
return "localhost";
}
}
private int getPort() {
if (isActive()) {
return (Integer.parseInt(getParameter("port")));
} else {
return 43594;
}
}
private boolean checkOnline(String ip, int port) throws UnknownHostException, IOException {
try {
Socket sock = new Socket(ip, port);
return sock.isConnected();
} catch (ConnectException e) {
return false;
}
}
private String getConnection(String ip, int port) throws UnknownHostException, IOException {
try {
Socket sock = new Socket(ip, port);
return (sock.isConnected() ? "ONLINE" : "OFFLINE");
} catch (ConnectException e) {
return ("OFFLINE");
}
}
@SuppressWarnings("unchecked")
private void initComponents() {

jScrollPane1 = new JScrollPane();
jTextArea1 = new JTextArea();
setVisible(true);
jTextArea1.setEditable(false);
jTextArea1.setColumns(20);
jTextArea1.setRows(5);
try {
Font font = new Font("Verdana", Font.BOLD, 12);
if (checkOnline(getIP(), getPort())) {
jTextArea1.setForeground(Color.GREEN);
} else {
jTextArea1.setForeground(Color.RED);
}
jTextArea1.setFont(font);
jTextArea1.setText(getServerName()+" IP: "+getIP()+" on port: "+getPort()+" is currently: "+getConnection(getIP(), getPort()));
} catch (IOException e) {
e.printStackTrace();
}

GroupLayout layout = new GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jScrollPane1, GroupLayout.DEFAULT_SIZE, 385, Short.MAX_VALUE)
.addContainerGap())
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(0, 11, Short.MAX_VALUE)
.addComponent(jTextArea1, GroupLayout.PREFERRED_SIZE, 383, GroupLayout.PREFERRED_SIZE)
.addGap(0, 11, Short.MAX_VALUE)))
);
layout.setVerticalGroup(
layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jScrollPane1, GroupLayout.DEFAULT_SIZE, 121, Short.MAX_VALUE)
.addContainerGap())
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(0, 12, Short.MAX_VALUE)
.addComponent(jTextArea1, GroupLayout.PREFERRED_SIZE, 119, GroupLayout.PREFERRED_SIZE)
.addGap(0, 12, Short.MAX_VALUE)))
);
validate();
}
private JScrollPane jScrollPane1;
private JTextArea jTextArea1;
}

enter image description here在我的桌面上运行它时没有错误,但是当我在我的网页上运行它时,我得到的就是这个。

Clear classloader cache ... completed.
Trace level set to 5: all ... completed.basic: Added progress listener: sun.plugin.util.ProgressMonitorAdapter@844c3d
basic: Plugin2ClassLoader.addURL parent called for file:/C:/Users/Dan/Desktop/Evolution-X/Evolution-X%20639/Evolution-X%20%20639%20Server/Evolution-X%20639%20Server/Evolution-X%20639%20Server/SocketsTest/TestConnection/bin/
network: Cache entry not found [url: file:/C:/Users/Dan/Desktop/Evolution-X/Evolution-X%20639/Evolution-X%20%20639%20Server/Evolution-X%20639%20Server/Evolution-X%20639%20Server/SocketsTest/TestConnection/bin/, version: null]
network: Cache entry not found [url: file:/C:/Users/Dan/Desktop/Evolution-X/Evolution-X%20639/Evolution-X%20%20639%20Server/Evolution-X%20639%20Server/Evolution-X%20639%20Server/SocketsTest/TestConnection/bin/, version: null]
basic: exception: java.lang.reflect.InvocationTargetException.
java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
at com.sun.deploy.uitoolkit.impl.awt.AWTAppletAdapter.runOnEDTAndWait(Unknown Source)
at com.sun.deploy.uitoolkit.impl.awt.AWTAppletAdapter.instantiateApplet(Unknown Source)
at sun.plugin2.applet.Plugin2Manager.initAppletAdapter(Unknown Source)
at sun.plugin2.applet.Plugin2Manager$AppletExecutionRunnable.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.reflect.InvocationTargetException
at com.sun.deploy.uitoolkit.impl.awt.OldPluginAWTUtil.invokeAndWait(Unknown Source)
... 5 more
Caused by: java.lang.ClassCastException: StatusCheckerMain cannot be cast to java.applet.Applet
at com.sun.deploy.uitoolkit.impl.awt.AWTAppletAdapter$1.run(Unknown Source)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$200(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
basic: Removed progress listener: sun.plugin.util.ProgressMonitorAdapter@844c3d
security: Reset deny session certificate store
basic: Dialog type is not candidate for embedding

最佳答案

applet 元素用于加载类型为JAppletApplet 的类,而代码是JFrame

所以从这样的事情开始。

import javax.swing.*;

/*
<applet code=DialerApplet width=250 height=70>
<param name="servername" value="Evolution-X" >
<param name="ip" value="127.0.0.1" >
<param name="port" value="43594" >
</applet>
*/
public class DialerApplet extends JApplet {
public void init() {
JTextArea ta = new JTextArea(20,5);
add(new JScrollPane(ta));

String eol = System.getProperty("line.separator");
StringBuilder sb = new StringBuilder();
String[] params = {"servername","ip","port"};
for (String param : params) {
sb.append( param );
sb.append( ": " );
sb.append( getParameter(param) );
sb.append( eol );
}
ta.setText(sb.toString());

validate();
}
}

working applet

更新

堆栈跟踪中的重要行是:

Caused by: java.lang.ClassCastException: 
StatusCheckerMain cannot be cast to java.applet.Applet

这很奇怪。

  • 根据问题的代码扩展了 applet。
  • 控制台输出支持缓存已刷新(删除可能已存储的旧类)。
  • 一个 JApplet extends Applet - 所以它可以被转换。

绝对确保将新 Jar 复制到运行代码的任何位置。还可以尝试开始一个新项目,看看您是否可以获得 发布的代码,以在您的机器上作为小程序启动。如果不是,则表明存在更根本的问题。

关于Java/HTML - 抛出 InvocationTargetException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13221509/

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