Can't access package/class from JavaScript on Linux with JRE 1.5
I made a simple test class:
package accessfromjs;
publicclass AccessFromJS{
privateint i = 0;
public AccessFromJS(){
}
publicvoid dump(){
System.out.println("i: " + i);
}
}
and put into/tmp/AccessFromJS.jar.
I want to call it from JavaScript using:
<script type="text/javascript">
function AccessFromJS(){
var acc =new Packages.accessfromjs.AccessFromJS();
acc.dump();
}
</script>
...<form>
<input type="button" value="Access From JavaScript" onclick="AccessFromJS()">
</form>
I set "-classpath=/tmp/AccessFromJS.jar" in Java Control Panel > Java > Java Applet Runtime Settings as runtime parameter.
The only result I get is a JavaScript error saying:"Packages.accessfromjs.AccessFromJS is not a constructor".
On the other hand it works on Windows with Firefox fine and displays "i: 0" in the Java Console.
Do you have any idea what is wrong on Linux? It seems to ignore the -classpath setting...
Thanks,
Javascript should look something like this:
document.getElementById("idOfTheApplet").yourPublicFunction();
Make sure to set the scriptable parameter to true
For multiple browser support you have both OBJECT and EMBED tag
each element should have a unique id that's why the sample below
uses try and catch to call the Applet's function.
Here is an example:
<object
id='idOfTheAppletIE'
classid = "clsid:8AD9C840-044E-11D1-B3E9-00805F499D93"
codebase = "http://java.sun.com/update/1.5.0/jinstall-1_5-windows-i586.cab"
>
<param name = "code" value = "MyApplet">
<param name = "type" value = "application/x-java-applet;">
<param name = "scriptable" value = "true">
<comment>
<embed
type = "application/x-java-applet;"
id='idOfTheAppletNotIE'
scriptable = true
pluginspage = "http://java.sun.com/products/plugin/index.html#download">
<noembed>
</noembed>
</embed>
</comment>
</object>
<script>
try{
document.getElementById("idOfTheAppletIE").yourPublicFunction();
}catch(e){
document.getElementById("idOfTheAppletNotIE").yourPublicFunction();
}
</script>
Actually my question was something else.
My class is not an applet just a simple class. I want to access it from JavaScript in JRE 1.5 from Mozilla (under Linux) as it is described under:
http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Guide:LiveConnect_Overview:JavaScript_to_Java_Communication#The_Packages_Object
As I wrote it runs fine on Windows with JRE 1.5 and Firfox...
Funny, never seen that. Works for me too on win.
Does this work on Linux?
<script>
var myString = new Packages.java.lang.String("Hello world")
alert(myString.length()); // Java function
alert("hello".length);// JScript function
</script>
Must say that setting a classpath variable never did anything for my applets
on win2k. Does it help putting your jars in the [jre home]/lib/ext
It works under Linux. The "Packages." prefix is needed if the class is not in java, sun or netscape packages. So var myString = new java.lang.String("Hello world");
should also work. Just as
var thread = java.lang.Thread.currentThread();
alert(thread.getId());
So if
var myString = new Packages.java.lang.String("Hello world");
or
var myString = new java.lang.String("Hello world");
alert(myString.length());
works
and
var myObject = new Packages.MyPackage.MyClass();
It might be that Java cannot find the jar. Did putting the jar in the
[jre home]/lib/ext directory work?