Export Applet Java with referenced libraries - java

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.

Related

Applet html embedding and I can't seem to locate the .class from my git repo

I am trying embed a java applet into my webpage. Unfortunately, I can't put my .class file straight into the server so, I was going to read the .class file from a git repository and pointing to it with the archive attribute in my html code.
This is the html code:
<html>
<body>
<applet code="DodgemApplet.class" width="640" height="480" archive="https://github.com/smithg017/repo.git">
</applet>
</body>
</html>
I am still pretty new to html so can someone show me the right way I should be going to get my java applet embedded into my website? Thanks!
Well, there was a hidden property I didn't know about and after much digging I found the property codebase.
I put this:
codebase="https://github.com/smithg017/repo/blob/master/"
right after
code = "DodgemApplet.class"
code is simply the name. by default it will look in the same directory as the html file. In my case, in the Desktop folder. If your class file is elsewhere, reference the folder your class file is in under the codebase property.

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.

Java lwjgl applet noclassdeffound

I'm trying to use my java applet on a html document but It just keeps giving the same errors over and over again.
I'm using eclipse. When i debug/run the applet it all works fine, but when I export it to a jar file and try to use it with a html document it gives this error:
java.lang.NoClassDefFoundError: org/newdawn/slick/opengl/Texture
Here is my java code:
http://pastebin.com/B3R6nj1a
This is what the .jar file contains:
-lib
-jars
lwjgl.jar
lwjgl_util.jar
slick-util.jar
-natives-win
*all the dlls*
-META-INF
-*files*
-res
grass.png
wood.png
.classpath
.project
Camera.class
Main$1.class
Main$2.class
Main.class
I do have everything right in the build path from my project.
(So added the three external jars. And added native-win to lwjgl.jar)
This is my html code:
<html>
<head>
</head>
<body>
<applet archive='3dtest.jar' Code='Main' width = "640" height = "480"></applet>
</body>
</html>
I've also tried to change "Code='Main' " to "Code='Main.class'" also didn't work.
Does anybody has any idea why I'm getting the error?
Thanks in advance.
-Tim
EDIT:
.classpath file:
http://pastebin.com/i7y4XYaf
This is what the .jar file contains:
-lib
-jars
lwjgl.jar
...
Those Jars should not be there. They should be separate Jars on the site referenced something like this:
<html>
<head>
</head>
<body>
<applet
archive='3dtest.jar,lwjgl.jar,lwjgl_util.jar,slick-util.jar,all_dlls.jar..'
code='Main'
width="640"
height="480">
</applet>
</body>
</html>
That is presuming the HTML is in the same directory as all the Jars.
You need to post relevant code, not everything and hope someone will pick through it all.
java.lang.NoClassDefFoundError: org/newdawn/slick/opengl/Texture means that class is not present in your classpath.
You need to make sure your applet is packaged properly when deploying it. See http://docs.oracle.com/javase/tutorial/deployment/applet/deployingApplet.html

ClassNotFoundException when loading Applet in Java Web Application

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.

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