Java applet with init() in a package? - java

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">

Related

Jar Applet in HTML

I've seen topics like this alraedy, but its not working for me.
So I have a game that succesfully made into an applet and it has many classes that are in a package called Build9. I want to put it on my website.
My file structure is the html file in the root "Desktop/My Name" and I have a media folder that has the RomanFury.jar.
I've tried every combination of changing around the code and archive in the HTML tag. With the classic:
<applet code="Main" archive="RomanFury.jar" width="1280" height="720">
My game.
</applet>
I get an error that says it cannot find Main. If I put media/RomanFury.jar or put media/ infront of main the same kind of error is given.
Can someone tell me the correct html tag?
My .class files are in a folder called "Build9" in RomanFury.jar.
Based on the information provided, this
<applet code="Main" archive="RomanFury.jar" width="1280" height="720">
should be
<applet code="Build9.Main" archive="RomanFury.jar" width="1280" height="720">
because your game has many classes that are in a package called Build9 (and your .class files are in a folder called "Build9" in RomanFury.jar).

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

Applet on Website seems to hate Packages

So I made an Applet (not JApplet) and proceeded to upload it to my website. I put all of my .class files into a package, exported the project from eclipse to a .jar, and uploaded that to the public_html folder of my website.
In my HTML code, I put
<applet ARCHIVE="BallShooter.jar" CODE="BallShooter" width=500 height=500> </applet>
However, it seems that I kept on getting the error "ClassNotFoundException"
If I reupload the .jar WITHOUT the package, it worked fine. Could somebody please explain to me how to fix this?
For those who are wondering, this is the structure of the things in the website
/public_html/myAppletJar/myPackage/a.class
/public_html/myAppletJar/myPackage/b.class
/public_html/myAppletJar/a.png
/public_html/myAppletJar/b.png
Check all these points:
if the applet class is BallShooter, and is in the package kikiotsuka, then its source code must start with the line package kikiotsuka;. The full name of the class is thus kikiotsuka.BallShooter.
in the jar file, you should thus have a directory named kikiotsuka at the root, and this directory must contain a file named BallShooter.class
since the name of the class is kikiotsuka.BallShooter, that's what the code attribute of the applet HTML element must contain: code="kikiotsuka.BallShooter" width=...

How to load a Java applet class from a subfolder with the embed tag?

I have a Java Applet as a single class file (No JAR file) in some sub directory and I want to embed it on a HTML page which is in a different directory. And I want to use the embed tag for it. But I can't get it working. This is my code so far:
<embed type="application/x-java-applet;version=1.6"
width="512" height="512"
code="subfolder/MyApplet.class" />
According to the Apache log file the Class file is loaded but it can't be started. Java says there is no class with name subfolder.MyApplet. So it treats the sub directory as a package name. A codebase parameter (No idea if this is valid for embed) doesn't make a difference. Maybe there is some other badly documented parameter to specifiy a code base directory from which to load the classes?
Please note that my question is not about how to embed a Java applet in general. I know how to get it running with the deprecated applet tag or the object tag and by packaging the class into a JAR file. My question is how to get it working in exactly this specific situation:
No JAR file, only single class file.
Class file is not in the same directory as the HTML page.
Using embed tag.
Consider using the 'codebase' tag when using the 'embed' tag. For example,
<embed type="application/x-java-applet;version=1.6"
width="512" height="512"
code="MyApplet.class"
codebase="subfolder/"/>
You should be able to use relative and absolute paths in the 'codebase' tag.
Hope that helps...
Try following:
<embed type="application/x-java-applet;version=1.6"
width="512" height="512"
codebase="subfolder/"
code="MyApplet.class" />
Refer to this documentation.

How to use an applet with packages in a jar

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

Categories