[英]ClassNotFoundException Java JApplet
I've been searching all over Stackoverflow and found out the problem seems to be, the path to the class is not correct. 我一直在搜索Stackoverflow,发现问题似乎是,该类的路径不正确。 (I tried to tweak around my code, but it still gives me ClassNotFoundException) The purpose of this code is to, let a user click on the list, then it will redirect them to a website.This is also pratice training from thenewboston. (我试图调整我的代码,但它仍然给我ClassNotFoundException)这个代码的目的是,让用户点击列表,然后它将它们重定向到一个网站。这也是来自thenewboston的实践培训。
Java: Java的:
package webApplet;
import java.applet.AppletContext;
import java.awt.BorderLayout;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import javax.swing.JApplet;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
public class Applet extends JApplet {
HashMap webInfo;
List<String> name;
JList list;
public void init() {
webInfo = new HashMap();
name = new ArrayList();
populate();
add(new JLabel("please click on a website"), BorderLayout.NORTH);
list = new JList(name.toArray());
list.addListSelectionListener(
new ListSelectionListener() {
public void valueChanged(ListSelectionEvent event) {
Object object = list.getSelectedValue();
URL url = (URL) webInfo.get(object);
AppletContext browser = getAppletContext();
browser.showDocument(url);
}
});
add(new JScrollPane(list), BorderLayout.CENTER);
}
public void populate() {
URL url;
String title;
String address;
int counter = 0;
title = getParameter("title" + counter);
while (title != null) {
try {
address = getParameter("address" + counter);
url = new URL(address);
webInfo.put(title, url);
name.add(title);
} catch (MalformedURLException ex) {
System.out.println("hi");
}
}
counter++;
title = getParameter("title" + counter);
}
}
html: HTML:
<html>
<body>
<applet code= "webApplet.Applet.class" width = "500" height = "250">
<param name= "title0" value = "thenewBoston.org">
<param name= "address0" value = "http://thenewboston.org">
<param name= "title1" value = "Awesome forum!">
<param name= "address1" value = "http://tnbforum.com/">
</applet>
</body>
</html>
Error: 错误:
Java Plug-in 10.25.2.16
Using JRE version 1.7.0_25-b16 Java HotSpot(TM) Client VM
User home directory = C:\Users\tin
c: clear console window
f: finalize objects on finalization queue
g: garbage collect
h: display this help message
l: dump classloader list
m: print memory usage
o: trigger logging
q: hide console
r: reload policy configuration
s: dump system and deployment properties
t: dump thread list
v: dump thread stack
x: clear classloader cache
0-5: set trace level to <n>
under the error it has this : "webApplet.Applet.class" 在错误下它有这个:“webApplet.Applet.class”
This is the flow of my files : 这是我的文件流:
website ---->
source packages----->
webApplet---->
- Applet.java
- bucky.html
the problem is you have your html next to java file not class file. 问题是你有你的HTML文件旁边的HTML不是类文件。 find the directory of the class file and put the html file in there. 找到类文件的目录并将html文件放在那里。
class file has extension Applet.class not Applet.java 类文件有扩展名Applet.class而不是Applet.java
so in your case, your class file may be under webApplet/bin/classes 所以在你的情况下,你的类文件可能在webApplet / bin / classes下
It the Applet.class
ends up in the same place as Applet.java
(please choose better class names BTW, even TestApplet01
makes it explicit that we are not referring to java.applet.Applet
), then the bucky.html
as seen needs to be in the parent directory. 它的Applet.class
在同一个地方结束Applet.java
(请选择更好的类名BTW,甚至TestApplet01
使得它明确的是,我们不是指java.applet.Applet
),那么bucky.html
所看到需求在父目录中。
website ---->
source packages----->
- bucky.html
webApplet---->
- Applet.java
- Applet.class
As to the HTML. 至于HTML。 Change: 更改:
<applet code= "webApplet.Applet.class" width = "500" height = "250">
<param name= "title0" value = "thenewBoston.org">
<param name= "address0" value = "http://thenewboston.org">
<param name= "title1" value = "Awesome forum!">
<param name= "address1" value = "http://tnbforum.com/">
</applet>
to.. 至..
<applet code= "webApplet.Applet" width = "500" height = "250">
...
</applet>
The code
attribute should be the fully qualified name of the class. code
属性应该是类的完全限定名。 While webApplet/Applet.class
might be an href to a class, and webApplet.Applet
is the FQN, webApplet.Applet.class
is just ..wrong. 虽然webApplet/Applet.class
可能是类的href,而webApplet.Applet
是FQN,但webApplet.Applet.class
只是..wrong。 Tolerated, but wrong. 容忍,但错了。
Problem is that the Applet.class file is not in classpath. 问题是Applet.class文件不在类路径中。 If you are not using any IDE, then you may need to manually compile the Applet.java file: 如果您没有使用任何IDE,则可能需要手动编译Applet.java文件:
// Assuming the Applet.java is in current directory and Java is setup fully.
prompt> javac Applet.java
This will create the Applet.class file as webApplet/Applet.class. 这将创建Applet.class文件作为webApplet / Applet.class。 here webApplet is a folder. 这里webApplet是一个文件夹。
Jar the complete webApplet/Applet.class file as 将完整的webApplet / Applet.class文件包含在内
jar -cvf applet.jar webApplet/Applet.class
Add the jar path into the CLASSPATH environment variable value of the system. 将jar路径添加到系统的CLASSPATH环境变量值中。 See this on how to do that: http://www3.ntu.edu.sg/home/ehchua/programming/howto/Environment_Variables.html 请参阅以下内容: http : //www3.ntu.edu.sg/home/ehchua/programming/howto/Environment_Variables.html
If you are using IDE like eclipse, it may help you to do that. 如果您使用像eclipse这样的IDE,它可能会帮助您做到这一点。 In any way, the main thing is to make sure that the jar is in CLASSPATH, so that JVM could recognize the jar. 无论如何,主要的是确保jar在CLASSPATH中,以便JVM可以识别jar。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.