Class not found exception Applet.jar in html file - java

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

Related

JApplet not working - page becomes unresponsive when plugin enabled

I am trying to run an Applet in a browser. I have three libraries, commons-net.jar, Jsch.jar (referenced) and MyApplet.jar, in the same folder on my local machine.
I created the following HTML file try.html:
<html>
<head>
<title>
HelloApplet by Aditya Jain
</title>
</head>
<body>
<Applet Code = 'com.cisco.fastforward.main/Main_Applet.class'
Archive = 'MyApplet.jar,commons-net-3.3.jar,jsch-0.1.51.jar'
Width =900 height = 900>
</Applet>
</body>
</html>
When I load the HTML file, it initially says "plug-in blocked". After I enable the plugin and reload, the page becomes unresponsive and I don't get any console output.
Update - The browser i was using was Chrome and Safari , When i switched to Firefox 25.0 , it worked after a few permissions.
I dont know why this happened.
Any reasons?(latest Chrome and Safari used)
Sign your jar. Set your system path to java path in cmd. Then run jarsigner, create a keystore and sign your jar. Make sure you have a proper manifest in your jar as well for permissions purposes. Also set java security to low and add your local html file to the java security exception list. This is the only way to make an applet work right now.

How does HTML java applet finds its .jar?

I have this code:
document.write("<APPLET CODE='com/synergex/My.class' ARCHIVE='toc2.jar' ");
When my "toc2.jar" is in the same folder as the HTML - all works fine.
But when I put "toc2.jar" in another folder ("C:\MyJars"), I get ClassNotFoundException.
How can I "tell" the browser where to look for jars?
I tried to add "C:\MyJars" or "C:\MyJars\toc2.jar" to my CLASSPATH env variable, but it did not work.
You might be able to redirect the code base using an HTML base element.
For example, given the following BASE declaration and A declaration:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<HTML>
<HEAD>
<TITLE>Our Products</TITLE>
<BASE href="http://www.aviary.com/products/intro.html">
</HEAD>
<BODY>
<P>Have you seen our Bird Cages?
</BODY>
</HTML>
the relative URI "../cages/birds.gif" would resolve to:
http://www.aviary.com/cages/birds.gif
If not, there are only 2 options.
Leave the Jar in the same directory as the HTML.
Presuming that part of the script is the only thing you cannot change, add codebase='../path/' immediately after it, to tell the JRE to wherever the Jar is located.
As an aside:
CODE='com/synergex/My.class'
Should be:
CODE='com.synergex.My'

Embedding multi-class applet with .jar

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

How to reach css and image files from the html page loaded by javafx.scene.web.WebEngine#loadContent?

I have a String HTML content which is loaded into webEngine by loadContent() method. I have also some css and image files used in this page. Although I put these file into the same package of java class, the loaded page cannot find them. Looked for API docs and web, but could not find any appropiate similar solutions. How I load these files?
You can place your string html content in a file in the same package as the Java class and use the engine.load(String url) method instead:
engine.load(getClass().getResource("mypage.html").toExternalForm());
When you do this, all relative links in the html page will resolve to resources (e.g. css and image files) in your Java package.
Beware that if you are loading a resource that is located in a jar file, that the jar: protocol does not understand relative links with parent specifiers. E.g., <img src="../images/image.png"/> will not work, but <img src="/images/image.png"/> or <img src="images/image.png"/> will as long (as you put the image in the appropriate location in the jar file). The file: protocol does not have such restrictions and .. relative links will work fine when the resources are loaded using it.
If the html string is dynamically generated by your java code rather than static, then Sergey's solution is probably best.
You need to set your local paths in html string for loadContent next way:
view.getEngine().loadContent(
"<img src='" + getClass().getResource("1.jpg") + "' />");
I just found out that using the <base> tag in the HTML also does the trick:
<html>
<head>
<title>The slash at the end of the href is important!</title>
<base href="file:///absolute/path/to/your/docroot/" />
</head>
<body>
<img src="image.png"/>
</body>
</html>
If you load the above code via engine.loadContent(String) then image.png will be loaded from /absolute/path/to/your/docroot/image.png.
This method is easier if you need to load multiple resources since you only have to specify the absolute path at a single place.
This has been tested with WebView of Java 8u25.
Try this
<link href="file:css\default.css" rel="stylesheet" type="text/css" />

Java applet won't load

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.

Categories