ClassNotFoundException when loading Applet in Java Web Application - java

I'm trying to deploy Java Web Application (Spring, Hibernate, Maven, Tomcat, WinXp) with a very simple applet, but when I open jsp page with this applet I see ClassNotFountException Error.
The structure of my project (deployed):
myApp
|--META-INF
|--WEB-INF
|--classses
|--ru
|--mydomain
|--applet
|--FileChooserApplet.class
|--views
|--main.jsp
|--index.html
|--resources
FileChooserApplet.class:
package ru.mydomain.applet;
import java.applet.Applet;
public class FileChooserApplet extends Applet {
#Override
public void paint(java.awt.Graphics g) {
g.drawString("Weather is good!", 70, 70);
}
}
main.jsp:
...
<body>
<APPLET code="ru.mydomain.applet.FileChooserApplet.class"
codebase="../classes" width=350 height=200></APPLET>
...
</body>
..
I tried to change codebase attribute to:
"classes"
"/classes"
""
delete this attribute
But,
if i add the same code for applet to index.html and double-click on this file (URL in browser starts with file:///C:/projects/myApp/target/myApp/...) then applet works.

AFAIK the applet will not have access to class files in WEB-INF/classes. These classes can only be accessed by server side resources such as servlets (as opposed to downloadable/static content).
You can jar all the class required for the applet to work and place the JAR file in the views folder. Your applet tag will look like
<APPLET code="ru.mydomain.applet.FileChooserApplet.class"
archive="mynewjar.jar"
width=350 height=200>
</APPLET>
A single JAR file is a cleaner way to do deployments.

Related

Include java applet class to my web page

How can I include my java applet class into my web page using JSP on a netbeans web application project.
I don't want it as a jar. When I use a jar it works correctly.
I have a package named "com.example" which is the applet named Scan.java
Also have a web page named main.jsp which I added the code below.
I am using this code to include the class but
<APPLET CODE="Scan.class" WIDTH=150 HEIGHT=250>
On the browser the applet doens't open and it says "ClassNotFoundException"
How to fix that?
The class "Scan" should be on the same directory than your HTML/JSP page, or you could specify the relative path (from the JSP) to reach your Scan.class with the attribute CODEBASE.
<APPLET CODE="Scan.class" CODEBASE="../classes/com/example" WIDTH=150 HEIGHT=250>

How do I turn an applet I made in Eclipse into something that will run in a web browser?

How do I go from the Eclipse project to making a file that will run the applet in a browser? From what I understand, I have to make it into a .jar file and then make an html file with the applet tag, like follows:
<html>
<body>
<applet name="TerisApplet.java" code = "TetrisApplet.jar">
</applet>
</body>
</html>
I do this and I run into nothing but trouble. Right now I am receiving a ClassNotFoundException. What am I doing wrong?
If someone can walk me through step by step from getting the Java Applet from Eclipse into an applet running over a browser, that would be awesome. This is for my own learning experience btw and not for school. I'm pretty good with Java I think but fairly new to applets.
1) the code should be
<html>
<body>
<applet code="name.class"
width="500"
height="250"/>
</body>
</html>
2) you must add your .class file to the folder in which your html file is located
for this just search your name.class file and ther would be two files one with a $ sign , copy them both to the folder which contains your .html file
In "name.class","name" means your class name
and you can take width and height as you want this is just an example.

First servlet, but not able to open it

Actually i try to create my first servlet and open it in a browser. But i alway get a 404 error.
My webapps folder from tomcat directory looks like:
docs
examples
host-manageR
manager
ROOT
test
index.htmL
servlet
bin
test.class
src
test.java
.classpath
.project
WEB-INF
classes
my index.html:
<html>
<body>
<div style="width:500px;height:300px;margin:auto;background-color:#EEEEEE;padding:0px 10px 0px 10px;margin-top:50px;">
<h1>Send Text</h1>
<form action="servlet/bin/test" method="get" style="font-size:10px;">
<textarea rows="10" cols="59" name="input"></textarea>
<input type="submit" style="margin-top:20px;"></input>
</form>
</div>
</body>
and this is my test class:
import java.io.*;
import javax.servlet.*;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
#WebServlet(urlPatterns={"/servlet/bin/test"})
public class test extends HttpServlet{
public void doGet(HttpServletRequest req,HttpServletResponse res) throws ServletException,IOException{
PrintWriter out = res.getWriter();
res.setContentType("text/html");
out.println("this is First servlet Example ");
}
}
I know it does nothing with the input from the html form but at this point I just want to open it and see the text inside^^... But when i open:
http://localhost:8080/test/index.html
, fill in the form and countinue or when i open:
http://localhost:8080/test/servlet/bin/test
directly, i always get a 404 error with everything i tried. I would prefer a solution without a xml but i think the problem is not there.
There are several problems in this code.
You're not respecting Java naming conventions. Classes should always start with an uppercase letter. They should never be in the default package.
You're deploying your whole development environment (sources, etc.), instead of generating a correct war file or exploded war file, and to deply that. The structure of your web-app should be
test
any static file you want
WEB-INF
classes
.class files, organized in directories strictly matching the package hierarchy
lib
jar files needed by your application
You're confused about what
#WebServlet(urlPatterns={"/servlet/bin/test"})
does. The url patterns is the set of URLs that you would like to use to access your servlet. The value doesn't have anything to do with the actual location of the servlet class on the disk. If you want the URL of your servlet to be /hello, then you simply use
#WebServlet(urlPatterns={"/hello"})
or, even simpler:
#WebServlet("/hello")
Once deployed, the complete URL of the servlet will thus be:
http://localhost:8080/test/hello
/test being the context path your webapp which is, by default, the name of the war file of exploded war directory you put under tomcat/webapps.
Try to follow these tutorials in Java-Brains for JSP & Servlets its very simple and explain every things from scratch :)
I think after all the comments the problem is just that your
web archive is not even registered within the server.
Try to package everthing under test as a zip file and rename it to test.war.
A war file is nothing more as a zip file with ending "war" for web archive
which indicates that server should resolve the archive as a web application.
if you extract this archive it should look like this.
index.html
WEB-INF
web.xml
|- classes
|- test.class
|- lib
Deploy this file using the management console of tomcat and manually deploy it to your server.
Additionally have a look about Webarchives "war".
If the servlet is registered within the server correctly than you reach it using
the specified pattern which you declared within #WebServlet Annotation.
The context path applies to the name of you war archive.
http://localhost:8080/<name of the warfile>/<urlPattern>
http://localhost:8080/test/servlet/bin/test // if the given example still applies

Export Applet Java with referenced libraries

I have coded an applet requiring 1 library jar file (prowser-0.2.0). I have tested it on eclipse (3.6) and it works but when i put it on my html website, i have got the following error. I have impoted pbrowser library from project properties => Java Build Path => Libraries => Add external Jar.
This code works in runnable jar and as applet in Eclipse.
Error from Java Console :
"Exception in thread "thread applet-myapplet.class-4" java.lang.NoClassDefFoundError: Could not initialize class com.zenkey.net.prowser.Prowser
at myapplet.init(myapplet.java:8)
at sun.plugin2.applet.Plugin2Manager$AppletExecutionRunnable.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)"
Applet code :
import java.applet.Applet;
import com.zenkey.net.prowser.*;
public class myapplet extends Applet {
public void init() {
Prowser prowser = new Prowser();
Tab tab = prowser.createTab();
System.out.println(tab.go("http://www.google.com").getPageSource());
}
}
Html code :
<html>
<head>
<title> hello world </title>
</head>
<body>
This is the applet:<P>
<applet code="myapplet.class" archive="hello.jar,prowser-0.2.0.jar" width="150" height="50">
</applet>
</body>
</html>
Really Thanks for help !
Are hello.jar and prowser-0.2.0.jar in the same directory as the HTML file in your web server that serves the HTML? The applet seems to find hello.jar as your error message indicates. The prowser-0.2.0.jar needs to be added to the same directory as a separate file, not being packed inside hello.jar itself (as Eclipse allows you to do if you select "export as runnable jar").
Then I'd also check the manifest file of hello.jar to see whether it contains unusual Class-Path entries for the prowser Jar. It should not contain any relative or absolute path information, just the file name itself.

How to specify correctly codebase and archive in Java applet?

I use firefox version > 3.5 (3.5.,3.6.,4.*) and I try to specify archive and codebase property correctly but it doesn't work. My main class for applet is situated in the archive and some necessary classes that are loaded during runtime are situated in the codebase. If I specify only the archive then the applet is loaded but the classes from codebase are missing. If I specify the archive and the codebase then the applet can't be loaded. It looks like applet try to load main class from codebase folder and doesn't look into the archive file.
<html>
<body>
<applet width=600 height=300 code="MyClass.class"
type="application/x-java-applet;jpi-version=6"
archive="http://myurl.com/archive/myjar.jar"
codebase="http://myurl.com/classes">
no applet
</applet>
</body>
</html>
Main class is situated in http://myurl.com/archive/myjar.jar and runtime classes are situated in http://myurl.com/classes.
Attribute codebase specifies the base URL of the applet - the directory that contains the applet's code. It is used while searching jar files in archive attribute, in such a way that all jars in archive attribute are searched relative to codebase.
So. When you use archive="http://myurl.com/archive/myjar.jar" and codebase="http://myurl.com/classes" together it means: find "http://myurl.com/archive/myjar.jar" in "http://myurl.com/classes" folder.
I.e. the full search path is "http://myurl.com/classes/http://myurl.com/archive/myjar.jar". And of course it can't be found!
Also, classes, whose jar-files aren't specified in the archive attribute, can't be found without codebase attribute. I.e. if there is no codebase then there is no way to find your classes in "http://myurl.com/classes" folder.
You can find more details in the Deploying With the Applet Tag tutorial.
I suggest the following solution:
Place myjar.jar in the http://myurl.com/classes folder;
Assuming your MyClass.class is in default package, and in the "http://myurl.com/archive/myjar.jar", the following code should work:
<html>
<body>
<applet width=600 height=300 code="MyClass"
type="application/x-java-applet;jpi-version=6"
archive="myjar.jar"
codebase="http://myurl.com/classes">
no applet
</applet>
</body>
</html>

Categories

Resources