I am building a small applet in java. It works fine (really!) when I run it in eclipse as an applet. Problems start when I export it as a jar and than try to load it through an html page.
Here's the html code:
<body>
<applet archive="myJar.jar" width=650 height=850>
</applet>
</body>
Now when I run other jar files in this way they work fine, for example if you view the source page of this niffty demo
<body>
<applet code="org.niffty.Niffty.class" archive="niffty.jar" width=650 height=850>
<param name="file" value="prelude.nif">
</applet>
</body>
Thank you!
You're missing the code attribute in your applet tag. Without it, the browser doesn't know which class in your JAR file is the applet it should display.
You've specified the archive, but no code attribute so the applet runner doesn't know which class to use as the entry point. You have to either specify code or specify the object attribute to give a serialized applet instance - but code is much more likely to be appropriate for you.
See the documentation for the applet tag for more information.
Related
I am trying to run an .jar file with a class in it that extends Applet in an html file. The .jar is called DocScrabble.jar, html is DocScrabble.html, and .class file is ScrabbleSolver.class. ScrabbleSolver.class also references a file called EnglishWords.txt, and I included that in the default package in which I placed ScrabbleSolver.class when I exported the file to a .jar. DocScrabble.jar and DocScrabble.html are located in the same directory. The applet works fine in eclipse, so I am assuming that my html file is the problem. THere is the html code.
<!DOCTYPE html>
<html>
<head>
<title>Doc Scrabble></title>
</head>
<body>
<APPLET CODE="ScrabbleSolver.class"
ARCIHVE="DocScrabble.jar"
WIDTH="400"
HEIGHT="200"
</Applet>
</body>
</html>
When i try to run the html, it gives me an error that says ClassNotFoundException ScrabbleSolver.class. Could someone please tell me what's wrong? I'm relatively new to programming.
It is highly advisable to check HTML using a validation service or DTD.
I typically rely on the W3C HTML validation service to check HTML.
Note that the applet element was last valid in HTML 3.2. It was deprecated in HTML 4.01. Without declaring any version, that mark-up would be presumed to be HTML 5.
The best way to deploy an applet is to use the Deployment Toolkit Script. On the other hand mistakes in spelling the attribute names in the script would not be picked up by an HTML validation service, since it concentrates on the HTML, rather than JavaScript embedded in the HTML.
For that reason it is a good idea to see the applet work when loaded using 'pure HTML' first.
Is your ScrabbleSolver class in a package? if so then it should be packagename.ScrabbleSolver.class
See Also: http://download.java.net/jdk8/docs/technotes/guides/jweb/applet/using_tags.html
I have a multi-class applet that has been exported using Eclipse as a .jar file. The jar file is called chess.jar, and the class I compiled and ran from in Eclipse is called Chess.java, and the binaries are Chess.class. The following code is the HTML I am trying to use to embed this applet into my website.
<APPLET CODE=Chess.class
ARCHIVE="chess.jar"
WIDTH=700
HEIGHT=700
CODEBASE="mywebsitewherethefilesarebeinghosted.com/"
ALT="Your browser understands the <APPLET> tag but isn't running the applet, for some reason.">
Your browser is ignoring the <APPLET> tag!
</APPLET>
I get the error ClassNotFoundException Chess.class
I have done applets before that have only one class successfully here , and I am using almost exactly the same HTML except for the CODEBASE and I have added the ARCHIVE tag.
As an applet, it has no main class. I am not very familiar with the MANIFEST.MF file, and I'm not sure if I need to utilize it for this purpose. My Chess.class calls some other classes like Pawn.class and Knight.class and they are all in chess.jar. Any help would be greatly appreciated.
EDIT
I created a local HTML file that runs the .jar locally and it runs perfectly.
<html>
<applet code=Chess.class
archive="chess.jar"
width=700 height=700>
</applet>
</html>
I think it is a simple matter of the contents of your HTML. I see you have a second start tag that I think would be confusing the browser. The browser would think the first one does not have an end tag and that may be the reason it is being ignored. Remove that and try again.
Even if this isn't the problem it's bad practice to leave tags open like that. Your error message should also be a bit more meaningful for poor users who don't know what an applet tag is.
It seems that CODEBASE follows / as directory separator unlike . in case of CODE attribute
so try replacing your CODEBASE value with proper directory structure separated by /
check example here and here also
This is web code for applet
<applet code=test.class name=test archive="thisDemo.jar">
</applet>
I write a class, and the class access the local data, so I use key to sign the applet. But I need to use other library, like Apache jar.
How to use the the jar in applet?
I can use the Apache jar in eclipse, but can't work in applet when web call the applet.
This works for me:
<applet code=test.class name=test archive="thisDemo.jar,apache.jar" width="800" height="600">
</applet>
and putting apache.jar next to thisDemo.jar
You might need to sign every single dependency jar:
https://stackoverflow.com/questions/14229863/must-sign-all-jars-of-applet
I have an applet (Applet, not JApplet) that has a lot of classes organized into packages, including the applet itself. I have looked everywhere for how to use that jar as an applet. It is not runnable and has a manifest file like such:
Manifest-Version: 1.0
Class-Path: AppletSource.jar
I put it in an html (Game.html), as such:
<applet code="Game/Game.class" archive="Game.jar" width=800 height=600>
Your browser needs JAVA!!!
</applet>
As you can see the class is called Game.class, Package Game and the jar Game.jar.
The manifest is in Game.jar/META-INF
When I use the appletviewer Game.html I get an error (java.security.AccessControlException: access denied) and if I open the .html I get a ClassNotFoundException: Game.Game.class. What can I do?
try
<applet code="Game.Game" archive="Game.jar" width=800 height=600>
Your browser needs JAVA!!!
</applet>
Also check that the package name is really Game and not game.
The format for the applet code attribute is from oracle doc "The value appletFile can be of the form classname.class or of the form packagename.classname.class.".
This file is relative to the base URL of the applet. It cannot be absolute.
Also try adding the jar in the same directory as the html.
For some further information see this doc
http://docs.oracle.com/javase/1.4.2/docs/guide/misc/applet.html
I'm wondering if you can put the main class (or the class with the init method, whatever) inside a package and still have it run in a browser? Most of the time I put my applets into the (default package) but my applet here is in a package, so can I still embed it into the webpage?
I've googled it with little results. Say I've got MyApplet.class in a directory called app in the jar file called MyApp.jar.
I've tried these with no success:
<applet archive="MyApp.jar" code="MyApplet.class">
<applet archive="MyApp.jar" code="app/MyApplet.class">
<applet archive="MyApp.jar" code="/app/MyApplet.class">
<applet archive="MyApp.jar/app/" code="MyApplet.class">
<applet archive="MyApp.jar" codebase="app/" code="MyApplet.class">
Each of these gives me a ClassNotFoundException.
Thanks in advance.
The archive attribute should contain the file name of the jar, and it should be placed in the same directory as the web page.
The class file in the code attribute should contain the fully qualified class name separated by forward slashes to indicate the directory structure.
Therefore, in your list of trials attempted, trial 2 should succeed, provided that MyApp.jar is actually present along side the html page. Additionally, MyApp.jar should contain the 'app' directory in the root, which should contain the MyApplet classfile. Don't forget to have MyApplet class itself in the app package.
You could take a look at this page for reference.
Well you list the package in a dotted form and you don't put the '.class' on the end.
<applet code="packagefolder1.packagefolder2.MyApplet" archive="folder1/folder2/MyApp.jar">
</applet>
I'm not sure, but have you tried like this?:
<applet archive="MyApp.jar" code="MyApplet">