I've been searching all over Stackoverflow and found out the problem seems to be, the path to the class is not correct. (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.
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>
<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"
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. find the directory of the class file and put the html file in there.
class file has extension Applet.class not Applet.java
so in your case, your class file may be under 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.
website ---->
source packages----->
- bucky.html
webApplet---->
- Applet.java
- Applet.class
As to the 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. While webApplet/Applet.class
might be an href to a class, and webApplet.Applet
is the FQN, webApplet.Applet.class
is just ..wrong. Tolerated, but wrong.
Problem is that the Applet.class file is not in classpath. If you are not using any IDE, then you may need to manually compile the Applet.java file:
// 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. here webApplet is a folder.
Jar the complete webApplet/Applet.class file as
jar -cvf applet.jar webApplet/Applet.class
Add the jar path into the CLASSPATH environment variable value of the system. See this on how to do that: 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. In any way, the main thing is to make sure that the jar is in CLASSPATH, so that JVM could recognize the jar.
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.