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,

[1758 byte] By [attilaracza] at [2007-10-3 3:40:47]
# 1

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>

harmmeijera at 2007-7-14 21:36:28 > top of Java-index,Desktop,Core GUI APIs...
# 2

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

attilaracza at 2007-7-14 21:36:28 > top of Java-index,Desktop,Core GUI APIs...
# 3

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

harmmeijera at 2007-7-14 21:36:28 > top of Java-index,Desktop,Core GUI APIs...
# 4

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());

attilaracza at 2007-7-14 21:36:28 > top of Java-index,Desktop,Core GUI APIs...
# 5

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?

harmmeijera at 2007-7-14 21:36:28 > top of Java-index,Desktop,Core GUI APIs...
# 6
- I have put the jar into [jre_home]lib/ext- I have tried to set -classpath, -cp, -Djava.class.pathUnfortunatelly it did not work. Neither with JRE 1.4.2 on Linux.
attilaracza at 2007-7-14 21:36:28 > top of Java-index,Desktop,Core GUI APIs...
# 7
Do you see the java console? If so what is the [jre home] dir?Can't test this because I don't have a linux here but after installing, updating and installing several versions of jre and jdk I've made the mistake of copying files to the wrong [jre home] sometimes.
harmmeijera at 2007-7-14 21:36:28 > top of Java-index,Desktop,Core GUI APIs...
# 8
java.home = /usr/java/jre1.5.0_08I have installed JRE from RPM package.
attilaracza at 2007-7-14 21:36:28 > top of Java-index,Desktop,Core GUI APIs...