How to Specify Correctly Codebase and Archive in Java Applet

How to specify correctly codebase and archive in Java applet?

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:

  1. Place myjar.jar in the http://myurl.com/classes folder;
  2. 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>

Specifying attributes in the applet tag

..how do you specify a relative URI in the archive attribute and how do you specify an absolute URI?

E.G.

Relative

// icon.gif can be found in the docs directory that is a sibling to this directory
"../docs/icon.gif"
// icon.gif can be found in the docs directory that is a child to this directory
"./docs/icon.gif"

Absolute

// a complete path to a web resource
"http://pscode.org/media/stromlo2.jpg"
// an absolute path to a local resource
"file:///WINDOWS/XYfolder/some.jar"

Is the Archive attribute specified above as relative or absolute URI?

It is an absolute reference.


Note that an applet should not typically be loading Jars off the computer of the end-user (unless they were cached locally by the JRE - but that is all 'invisible' to the app.). That is most likely the real problem here.

How to specify classes from another projects to an applet?

The simplest thing is to bundle all the required classes in a jar file and then specify this as the archive.

Like this:


http://docs.oracle.com/javase/7/docs/api/java/applet/Applet.html
André

Java applet: How to tell the browser (Firefox) where to look for libraries (jars)

Applet is executed in clients machine, so having libs in CLASSPATH won't help.

Libs should be in "archive" attribute:

<applet code="Applet.class" archive="myApplet.jar,lib1.jar,lib2.jar" width="600" height="600" title="MyApplet">

java.lang.ClassNotFoundException: Applet using external jar

Note that anything in the WEB-INF folder is only readable to the back-end servlet, not by anything running in the browser including the applet[1].

If the two jars in the lib folder are only used by the applet, you can move the folder up to webcontent level. If your backend also uses them, you'll have to either copy them or create a servlet to serve them to the front-end.

[1]What is WEB-INF used for in a Java web application?

run applet in web application

the problem is that the applet engine can't find your MyApplet class at the codebase you have defined.

This can be caused because you have you class at your /WEB-INF/classes directory. This directory is protected by the servlet engine, for it not to be accesed from external resources (as can be an applet tag at a JSP/HTML page.

There are a few ways to solve this. The easiest one is to pack your MyApplet class un a jar file (let's call it myapplet.jar), and save it at an accesible directory (i.e. the jsp folder of your web application).
As an example, supose you have the following folders for the web application:

/MyWebApp/jsp
/MyWebApp/applet
/MyWebApp/WEB-INF

The client browsers can access the content of jsp and applet folders.

Then, save your myapplet.jar at the applet folder, and set your applet tag configuration like this (suposing that you web context is MyWebApp):

<applet codebase="/MyWebApp/applet" archive="myapplet.jar" 
code="MyApplet.class" width="600" height="500">
</applet>

Here you can find more info about the applet tag: http://docs.oracle.com/javase/tutorial/deployment/applet/index.html

Creating a Java applet that will display in a web browser

..have followed the steps exactly as described.

No you didn't. Their applet is in the default package, while yours is in openDis.applet package.

So:

<applet code = "HelloWorld.class" width = 150 height = 25>
</applet>

Should be:

<applet code = "openDis.applet.HelloWorld" width = 150 height = 25>
</applet>

And the structure needs to be:

  • dir (directory)
    • applet.html
    • openDis (directory)
      • applet (directory)
        • HelloWorld.class


Related Topics



Leave a reply



Submit