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
Related
I don't understand why i cannot include a class into my jsp file.
So, the file i want to use is:
public_html/admin/dashboard.jsp
i have this code:
<%#page import="frontEnd.General"%>
<%
String getBasePath = General.getBasePath(request);
%>
the class i want to include is located:
public_html/WEB-INF/classes/frontEnd/General.class
but when i do this i recieve the error:
HTTP Status 500 - Unable to compile class for JSP
Can you help me with this?
Thanks!
another relevant example:
i have a page, page.jsp in route that has this code on the top:
<%#page import="frontEnd.General"%>
<%
String getBasePath = General.getBasePath(request);
%>
if i access like this:
www.mydomain.com/page.jsp - works, the class is found
if i move the file into a directory (let's say "admin") and try to access the file like:
www.mydomain.com/admin/page.jsp - i recieve the error that the class is not found
Check the package structure of your server. Most probably it doesnt find the class in your structure hence it throw compilation error.
Try importing other classes and check whether problem still comes.
I'm supporting an older JSP system that may be using non-standard way of including classes and functions. Instead of writing classes files and compiling them into jar files, the classes are written directly in "child" JSPs and then included in a "parent" JSP.
The problem I'm having is that in JDeveloper, even though the IDE recognize the files in the include statement, I get error messages that MyClass and ChildFunction() are not defined. When I drop the 2 JSPs on the server, the page loads and runs with the expected results. As a result, when I work on the real code, I have a 1000+ line JSP with over a hundred syntax errors, so I can't tell if my code is even syntactically correct until I drop it on a server. I'm hoping its something simple (/stupid) like a classloader error or a bad configuration, but I haven't been able to fix it. Any help is greatly appreciated.
I've included two stub files below:
MyClass.jsp FILE:
<%# page import="java.lang.*,java.net.*,java.text.*,javax.servlet.http.*" %>
<%!
class MyClass() {
public MyClass() { super(); }
private int it=0;
public void setIt (int it) {this.it = it; }
public int getIt () { return it; }
}
public String ChildFunction(){
return " ChildFunction ";
}
%>
Parent.jsp FILE:
<%# page contentType="text/html;charset=WINDOWS-1252"%>
<%# page language="java" session="true" import="java.lang.*,java.net.*,java.text.*,javax.servlet.http.*" %>
<HTML>
<HEAD><TITLE>Test page</TITLE></HEAD>
<BODY>
<%# include file="./MyClass.jsp" %>
<br>Result of calling Child Function:<% out.print(ChildFunction()); %>
<br>Result of declaring MyClass:
<%
MyClass mc = new MyClass();
out.print(mc.getIt());
%>
</BODY>
</HTML>
Before someone suggest that I put the code in class files and use EAR/WAR files to deploy, that's not an option. This is an existing legacy system that I need to be able to support.
Don't put functions or subclasses in JSPs expecting to use them in others. (That is what they are after all, subclasses under that particular JSP.) It is very bad design. Create classes in actual .java files, compile them, and then call them from the JSPs. Even that is not particularly recommended (see How to avoid Java code in JSP files), but its better, a lot better.
If you have access to the filesystem, you don't need to put classes in a EAR/WAR. Just put them on the filesystem under webapps/{theApp}/WEB-INF/classes/...
You can even dispense with an IDE altogether, and just save all your .java files under webapps/{theApp}/WEB-INF/classes/... open a console, cd to that directory, and javac them. There is certainly no need to put class files in jars.
You do, of course, have to restart the application afterwards.
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>
*/
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!