Applet and libraries
My applet doesn’t see the external libraries. Everything works using the appletviewer, but not using the browser. I’ve put in my “test_applet” folder the jar (TreC-Vis.jar) containing the applet classes, four jar libraries used by TreC-Vis and the html file with the following applet tag开发者_如何学运维:
<applet code="gui.Gui" archive="TreC-Vis.jar,postgresql-8.4-701.jdbc4.jar,postgis_1.5.0.jar,jfreechart-1.0.13.jar,jcommon-1.0.16.jar" width="1024" height="768"> </applet>
Java console gives me a java.io.FileNotFoundException for each of the four jar libraries. I specify that I exported TreC-Vis.jar from the corresponding Eclipse project, in which I put these libraries in a “lib” folder at the same level of the “src” package.
What’s wrong with the applet tag I wrote? Reading the tutorial here
http://download.oracle.com/javase/tutorial/deployment/jar/downman.html
I’ve been considering the possibility to put everything, applet and libraries, in one jar as a solution, but I would need some example of the “custom code” mentioned in the Note. Thanks in advance.
My applet doesn’t see the external libraries. ..They are just native libraries, .. .class files ..
OK. If then, you mean 'natives' as in files of type .dll, .so etc., that is problematic for an applet in that they cannot use the natives unless they are already installed in the appropriate directory of the user system.
Having said that, recent developments allow us to deploy an embedded applet using Java webstart (JWS). JWS makes use of natives easy. Simply put them in the root of a Jar file and add them to a nativelib element in the (XML based) launch file (file type .jnlp).
Even better, JWS can separate the downloads into resources for different operating systems, so Windows gets the .dll natives, while *nix get the .so natives.
JWS offers many more useful features, but the important thing here is that they can make natives available to applets.
Use of native libs in an applet, requires the applet to be trusted.
jar cfm MyApplet.jar MyManifest.txt MyPackage1 MyPackage2 MyPackage3
This was the line I was looking for. This way I've put in my manifest the classpath of the external libraries.
Here is my code and how I have used native libraries in that. It does work in Windowes but it doesn't work in Linux and I receive
access denied("java.lang.RuntimePermission""loadLibrary.hello")
Here is my JNLP:
<?xml version="1.0" encoding="UTF-8"?>
<jnlp spec="1.0+" codebase="" href="">
<resources>
<!-- Application Resources -->
<j2se version="1.7+"
href="http://java.sun.com/products/autodl/j2se"/>
<jar href="applet.jar" main="true" />
<nativelib download="eager" href="libhello.jar"/>
</resources>
<applet-desc
name="Math Applet"
main-class="NativeHelloApplet"
width="10"
height="1">
</applet-desc>
<update check="background"/>
</jnlp>
My Applet:
import java.security.*;
import javax.swing.*;
public class NativeHelloApplet extends JApplet
{
public native String displayHelloWorld();
public native int initPKE (int[] retVal);
public NativeHelloApplet() {
}
public void init()
{
// privileged code goes here, for example:
System.loadLibrary("hello");
getContentPane().add(new JLabel("Test"));
getContentPane().add(new JLabel(displayHelloWorld()));
}
}
My native .c code :
#include <jni.h>
#include "NativeHelloApplet.h"
#include <stdio.h>
JNIEXPORT jstring JNICALL
Java_NativeHelloApplet_displayHelloWorld(JNIEnv *env, jobject obj)
{
return (*env)->NewStringUTF(env,"Hello world!\n");
}
My HTML page:
<Html>
<Head>
<Title>Java Example</Title>
</Head>
<Body>
This is my page<br>
Below you see an applet<br>
<br>
<script language="javascript" type="text/javascript" src="deployJava.js"></script>
<script>
var attributes = {
id: "sswSignApplet",
code: "NativeHelloApplet",
width: 300,
height: 60
};
var parameters = {jnlp_href:"launch.jnlp"}; <!-- Applet Parameters -->
var version = "1.6"; <!-- Required Java Version -->
deployJava.runApplet(attributes, parameters, version);
</script>
</Body>
</Html>
libhello.jar contains the shared object of my native code and is located on the same folder as html and jnlp.
It works in windows when I put hello.jar(containing hello.dll) in resource section but in Linux I received the mentioned error.
精彩评论