From:  "Mike" <Mail.Micke@gmail.com>
Date:  13 Jul 2006 23:53:49 Hong Kong Time
Newsgroup:  news.mozilla.org/netscape.public.mozilla.jseng
Subject:  

Re: convert java object to js

NNTP-Posting-Host:  193.62.199.144

Hi,
not sure if this is the answer, but in [1] they use something like this
to call custom java classes :

var test = new Packages.FirstTry(12);

Note the Packages part, haven't tried this myself though :)

- Mike

Matthias wrote:
> Hello everyone,
>
> I'm new to rhino (and js ;-)) and have a(n) (understanding) problem
> converting java objects to js. Here is my code:
>
> import org.mozilla.javascript.*;
> import java.io.*;
>
> public class FirstTry extends ScriptableObject{
>
> 	private SecondTry str;
>
> 	public String getClassName() {
> 		return "FirstTry";
> 	}
>
> 	public void jsConstructor(int test) {
> 		this.str = new SecondTry();
> 	}
>
> 	public SecondTry jsGet_SecondTry() {return str;}
>
> 	public FirstTry() {}
> }
>
>
>
> import org.mozilla.javascript.*;
>
> public class SecondTry extends ScriptableObject {
>
> 	public String member = "Hello World";
>
> 	public String getClassName() {
> 		return "SecondTry";
> 	}
>
> 	public SecondTry() {
> 	}
>
> 	public void jsConstructor() {}
>
> 	public String jsGet_member () {return member;}
> }
>
>
> import java.io.File;
> import java.io.FileReader;
>
> import org.mozilla.javascript.*;
>
> public class Test {
>
> 	/**
> 	 * @param args
> 	 */
> 	public static void main(String[] args) {
> 		String filename = "firstTry.js";
> 		Context cx = Context.enter();
> 		try {
> 			Scriptable scope = cx.initStandardObjects();
> 			File file = new File(filename);
> 			FileReader reader = new FileReader(file);
> 			ScriptableObject.defineClass(scope, FirstTry.class);
> 			ScriptableObject.defineClass(scope, SecondTry.class);
> 			Object result = cx.evaluateReader(scope, reader, "me", 1, null);
> 			System.out.println(cx.toString(result));
> 		} catch (Exception e) {
> 			System.err.println(e.toString());
> 		}
> 	}
> }
>
> Here is the code of my js- file:
>
> var test = new FirstTry(12);
> test.SecondTry.member;
>
> Executing Test simply gives me "undefined".
>
> When I change the content of my js- file to
>
> var test = new SecondTry();
> test.member;
>
> it gives me "Hello World" as expected.
>
> So I wonder, what I missed to map java object out of java to js.
> 
> Regards,
> Matthias