I have a folder with two files inside; the java .class file and the .html file. In my html file I call the .class file as an applet but it sais an error on the website its published to saying it can't find the .class file. This puzzles me since they are in the same directory and I triple checked for spelling errors.
Here is my .html file...
<html>
<head>
<title>Applet</title>
</head>
<body>
Program<br />
<applet code="testing.class" width="300" height="300"/>
</body>
</html>
and here is my .class file...
import java.awt.Color;
import java.awt.Graphics;
public class testing extends java.applet.Applet{
public void init(){
}
public void paint(Graphics g){
g.drawOval(0,0,250,100);
g.setColor(Color.RED);
g.drawString("My Applet",10,50);
}
}
My .class file is "testing.class" and my html file is "testingpage.html"
Here is the Full Entire error
load: class testing.class not found.
java.lang.ClassNotFoundException: testing.class
at sun.plugin2.applet.Applet2ClassLoader.findClass(Applet2ClassLoader.java:252)
at sun.plugin2.applet.Plugin2ClassLoader.loadClass0(Plugin2ClassLoader.java:249)
at sun.plugin2.applet.Plugin2ClassLoader.loadClass(Plugin2ClassLoader.java:179)
at sun.plugin2.applet.Plugin2ClassLoader.loadClass(Plugin2ClassLoader.java:160)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
at sun.plugin2.applet.Plugin2ClassLoader.loadCode(Plugin2ClassLoader.java:690)
at sun.plugin2.applet.Plugin2Manager.createApplet(Plugin2Manager.java:3045)
at sun.plugin2.applet.Plugin2Manager$AppletExecutionRunnable.run(Plugin2Manager.java:1497)
at java.lang.Thread.run(Thread.java:680)
Exception: java.lang.ClassNotFoundException: testing.class
load: class testing.class not found.
java.lang.ClassNotFoundException: testing.class
at sun.plugin2.applet.Applet2ClassLoader.findClass(Applet2ClassLoader.java:252)
at sun.plugin2.applet.Plugin2ClassLoader.loadClass0(Plugin2ClassLoader.java:249)
at sun.plugin2.applet.Plugin2ClassLoader.loadClass(Plugin2ClassLoader.java:179)
at sun.plugin2.applet.Plugin2ClassLoader.loadClass(Plugin2ClassLoader.java:160)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
at sun.plugin2.applet.Plugin2ClassLoader.loadCode(Plugin2ClassLoader.java:690)
at sun.plugin2.applet.Plugin2Manager.createApplet(Plugin2Manager.java:3045)
at sun.plugin2.applet.Plugin2Manager$AppletExecutionRunnable.run(Plugin2Manager.java:1497)
at java.lang.Thread.run(Thread.java:680)
Exception: java.lang.ClassNotFoundException: testing.class
The applet tag should be:
<applet code=testing.class width="300" height="300" />
Notice the change in the code attribute. Compare to the example code listed in the relevant Java Tutorial:
<applet code=Applet1.class width="200" height="200">
Your browser does not support the <code>applet</code> tag.
</applet>
The following is working for me.
http://puu.sh/PebS
TestingApplet.java
import java.applet.Applet;
import java.awt.Color;
import java.awt.Graphics;
public final class TestingApplet extends Applet {
public void paint(final Graphics g){
g.drawOval(0, 0, 250, 100);
g.setColor(Color.RED);
g.drawString("My Applet", 10, 50);
}
}
testing-applet.html
<html>
<head>
<title>Applet</title>
</head>
<body>
Program <br />
<applet code=TestingApplet.class width="300" height="300" />
</body>
</html>
If this is not working, I have two questions for you...
Did you save the Java as a .java file and compile it to produce the correct .class file?
Did you verify your browser is not caching an old incorrect version of the .html file?
Are your .class and .html files in the same directory?
I am pretty sure that you're not openning the correct HTML file. Use your File Browser (Finder I guess ;) ) and look for the file in the Netbeans project directory. It should be in /build/classes/.html
Related
My aim is to import my class file in my index.jsp and reading over the internet I learned I'm supposed to use JavaBeans.
So here is my class file placed in WEB-INF/classes/jBean/Bucket.class -
package jBean;
import java.sql.* ;
import java.math.* ;
public class Bucket implements java.io.Serializable{
public static void main(String[] args){
Bucket obj = new Bucket();
obj.DBConn();
}
public static void DBConn(){
try{
Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/studentfeedback","root","");
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery("select * from student");
while(rs.next())
System.out.println(rs.getInt("Roll_No")+" "+rs.getString("Name")+" "+rs.getString("Pass"));
con.close();
}
catch(Exception e){
System.out.println(e);
}
}
And here is my index.jsp where I want to import the class file -
<%# page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>$Title$</title>
</head>
<body>
<jsp:useBean id="jBean" class="jBean.Bucket" scope="session" />
</body>
</html>
Doing this I get this error message on executing index.jsp file
java.lang.ClassNotFoundException: org.apache.jsp.index_jsp
So, my query is how shall I import the class file, Bucket.java?
And is there any other way to import the file to index.jsp other than using javBeans?
Please help me with this as I can't find what I'm looking for. Maybe cause I might not be asking the right thing.
All I want to do is import the class file Bucket.java to the index.jsp
May God help the helping soul.
Did you observed your project folder structure.
The classes referenced by the jsp must be in the classpath.
And the classpath includes WEB-INF/classes.
The location of the jsp is of no importance.
question regarding jsp page import and <jsp:useBean id
So basically, I've mentioned the HTML code in the java file, But um the applet wont execute for some reason, Help me
import java.awt.*;
import java.applet.*;
/*
<applet code = "demo.java" width=400 height=200>
<param name="txt" value ="Hey">
</applet>
*/
class demo extends Applet {
public void paint(Graphics g)
{
String string = getParameter("txt");
g.drawString(string, 29, 40);
start();
}
}
<applet code = "demo.java" width=400 height=200>
<param name="txt" value ="Hey">
</applet>
That code parameter is incorrect. It should be the fully qualified class name. Or..
<applet code = "demo" width=400 height=200>
<param name="txt" value ="Hey">
</applet>
To compile & launch it in applet viewer from the command line, do something like:
prompt> javac demo.java
prompt> appletviewer demo.java // (see Note)
Note: Yes I do mean the .java extension. AppletViewer can launch an applet from that comment embedded in the source code. See the Applet info. page (at To compile and launch:) for another example.
Questions/Comments
Debugging
Ensure the Java Console is configured to show for applets & JWS apps. If there is no output at the default level, raise it and try again.
Copy/paste all error & exception output the console provides.
Code
The applet code itself would be better to declare a String txt that is declared as a class attribute and initialized in the init() method, like this txt = getParameter("txt");. the paint(Graphics) method might be called many times.
Any time a paint(..) method is overridden, it should immediately call super.paint(..) (for the BG color, if nothing else).
Questions
Why code an applet? If it is due to spec. by teacher, please refer them to Why CS teachers should stop teaching Java applets.
Why AWT rather than Swing? See my answer on Swing extras over AWT for many good reasons to abandon using AWT components.
you should give the class name not java file name.Go through applet tutorials for good understanding.
Try this,
import java.awt.*;
import java.applet.*;
public class demo extends Applet {
public void run(){
repaint();
}
public void paint(Graphics g)
{
String string = getParameter("txt");
g.drawString(string, 29, 40);
}
}
/*
<html>
<applet code = "demo.java" width=400 height=200>
<param name="txt" value ="Hey">
</applet>
</html>
*/
My class's code:
package overviewPack;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class ButtonScreen extends JApplet implements ActionListener{
JButton middle = new JButton();
Container screen = getContentPane();
public void init(){
setVisible(true);
middle.addActionListener(this);
screen.add(middle);
}
public void actionPerformed(ActionEvent event) {
if (event.getSource() == middle){
System.out.println("hey");
}
}
}
When I attempt to run it using html, I recieve a noclassdefFound error, with the stacktrace as ButtonScreen(Wrong name: overviewPack ButtonScreen)
Here is my html code: (I use brackets so that the code will turn up in chat as the code and not the finished product).
<HEAD>
<TITLE>
A Simple Program </TITLE>
</HEAD>
<BODY>
Here is the output of my program:
<APPLET CODE="overviewPack.ButtonScreen.class" codebase = "bin" WIDTH=150 HEIGHT=25>
</APPLET>
</BODY>
</HTML>
I have tried many different formats for the html, and looked at many other people's similar, sometimes exactly the same errors, but none of the solutions proposed to other people have worked. I have also looked around on the rest of the net for a solution, but I have found none.
This error happens with all my applets, even this extremely simple applet I did above.
The html file is in the same folder as the class
The HTML file is in the same folder as the class
That is no good. You need to understand how the parameters in the applet element work.
<APPLET CODE="overviewPack.ButtonScreen.class" codebase="bin" WIDTH=150 HEIGHT=25>
Let us say that the HTML is located at: our.com/applets/applet1.html.
codebase = "bin" would mean the classpath starts with our.com/applets/bin/.
overviewPack.ButtonScreen.class would therefore need to be found at:
our.com/applets/bin/overviewPack/ButtonScreen.class
Note that the package overviewPack has become an inherent part of the correct path to the class file. That is where the 'wrong name' is originating from. The JRE seems to be searching the directory of the HTML, locating the class in the same directory, then loading it to discover it is in the wrong path.
Code Attribute
<APPLET CODE="overviewPack.ButtonScreen.class" codebase="bin" WIDTH=150 HEIGHT=25>
Note the required value is the Fully Qualified Name of the class file. That consists of the package(s) name, followed by the class name, each separated by a dot. E.G.
overviewPack.ButtonScreen
As opposed to
overviewPack.ButtonScreen.class // combination of FQN with file type
or
overviewPack/ButtonScreen.class // relative file path on server
So the opening APPLET element should best be:
<APPLET CODE="overviewPack.ButtonScreen" codebase="bin" WIDTH=150 HEIGHT=25>
Sometimes there is a problem with the .class file extension at the end of the code= attribute. Some doc I've seen says that the code= attribute has the classname in which case having the .class at the end is wrong. The classname is: overviewPack.ButtonScreen and the filename is:
ButtonScreen.class
I am experiencing a little problem reading parameters from an html page to an applet.
my code (necesssary for question):
on html page:
<PARAM NAME = "name" VALUE = "Nicholus">
in applet (init):
String strName = getParameter("name");
The applet just decides to look at me instead of getting the name value..
a few google search shows im not the only one, except I haddn't yet found the solution, so I decided to post it here in case it was already resolved.
Compare your work with the following working code or post your code if problem still not solved.
Here is the ParamDemo.java code
import java.awt.*;
import java.applet.*;
public class ParamDemo extends Applet{
String strName;
public void start()
{
strName=getParameter("name");
if(strName==null)
strName="Not Found";
}
public void paint(Graphics g)
{
g.drawString("Name :"+strName,10,20);
}
}
Then the Applet.html
<html>
<body>
<applet code="ParamDemo" width="300" height="300">
<param name='name' value="Nicholas">
</applet>
</body>
</html>
Snapshot
[Ps:You need to compile your .java before using in HTML page and make sure that there is no problem related to leter-case of parameter-names as java is case-sensitive.Also the .class and .html files must be in same root folder).
I got it solved, my negligence... my html had many applet codes, the params were not printed within the applet declaration of the one that needs the params. thanks for contributing.
I have a strange and fustrating problem. I loaded all my .class files into a JAR file and placed it on my local web server. The problem is when I navigated to the page with my JAR I got a big ClassNotFoundExeption. I am 100% certian this class file is in my JAR. I also know the JAR file is in the same directory as my XHTML file
Here is the XHTML source
<html>
<head>
</head>
<body bgcolor="000000">
<center>
<applet
archive = "program.jar"
code = "inigui4.class"
width = "500"
height = "300"
>
<param name="cache_option" value="no">
</applet>
</center>
</body>
</html>
Very strange!
For some reason when I create a hello world applet I get the same problem (could it be with my LightTPD server?
Java source follows
import java.awt.*;
import java.applet.*;
public class inigui4 extends Applet {
public void init() {
}
public void paint(Graphics g) {
g.drawString("Welcome to Java!!", 50, 60 );
}
}
Even stranger.
Now I can't load any applets on the web because I get this error. I will need to contact Oracle in the morning.
It can be caused by multiple reasons, but the most probable one is that you haven´t specified the package of the class you´re using. Look at the answer of this post.
If you are using packages you have have to prefix inigui4.class by its relative path into the jar
I think the problem is here:
<applet
archive = "program.jar"
code = "inigui4.class"
width = "500"
height = "300"
>
Here, you specify inigui4.class - but in your Java code:
public class inigui_rb extends Applet {
...
}
This is inigui_rb. Try:
<applet
archive = "program.jar"
code = "inigui_rb.class"
width = "500"
height = "300"
>
The ClassNotFoundException should go away.
Also please adhere to the Java coding conventions when coding! In this case:
Class Names Begin With Capitals!
ClassNamesDoNotContainUnderscoresInsteadTheyAreCamelCased!