Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

error to call static method of thirdparty class

705 views
Skip to first unread message

viktor stolbin

unread,
Aug 28, 2009, 6:47:29 AM8/28/09
to
Hello everybody
I'm embedding rhino in my desktop application and faced with problem.
In script I use hibernate Restrinctions.eq() and it gets an error when
trying to call static "eq()". I wrote a test class and a test script:
Test class

1. public class TestClass {
2.
3. public static String variable = "variable";
4.
5. public static String test(){
6. return "test";
7. }
8.
9. }

Test script

1.
2. var test1 = java.lang.Math.cos(0);
3. var test2 =
org.eclipse.jface.viewers.StructuredSelection.EMPTY;
4. var test3 = ru.griffo.core.ui.utils.TestClass.variable;

It gets error:

org.mozilla.javascript.EcmaError: ReferenceError: "ru" is not
defined. (<cmd>#1)

Class TestClass is in the same package my script util is. When I
changed script to:

1.
2. hibernate = Packages.org.hibernate.criterion;
3. jface = Packages.org.eclipse.jface.viewers;
4. test = Packages.ru.griffo.core.ui.utils;
5.
6. var test1 = java.lang.Math.cos(0);
7. var test2 = jface.StructuredSelection.EMPTY;
8. var test3 = test.TestClass.variable;
9. var test4 = test.TestClass.test();

it gets the same error I get with hibernate Restriqtions.eq():


org.mozilla.javascript.EcmaError: TypeError: Cannot call property
test in object [JavaPackage ru.griffo.core.ui.utils.TestClass]. It is
not a function, it is "object". (<cmd>#1)

I tried to use Importers and get this error again.

Script util class

1. private org.mozilla.javascript.Context scriptContext;
2. private Scriptable scriptScope;
3.
4. private void initServices() {
5. scriptContext = org.mozilla.javascript.Context.enter();
6. scriptScope = new ImporterTopLevel(scriptContext);
7.
8. registerToScriptEngine("out", System.out);
9. registerToScriptEngine("boTool", boTool);
10. registerToScriptEngine("convertTool", convertTool);
11. registerToScriptEngine("entityDAO", entityDAO);
12. }

Please, help me to solve this problem

Norris Boyd

unread,
Aug 28, 2009, 3:17:22 PM8/28/09
to
On Aug 28, 6:47 am, viktor stolbin <viktor.stol...@gmail.com> wrote:
> Hello everybody
> I'm embedding rhino in my desktop application and faced with problem.
> In script I use hibernate Restrinctions.eq() and it gets an error when
> trying to call static "eq()". I wrote a test class and a test script:
> Test class
>
>    1. public class TestClass {
>    2.
>    3.     public static String variable = "variable";
>    4.
>    5.     public static String test(){
>    6.         return "test";
>    7.     }
>    8.
>    9. }
>
> Test script
>
>    1.
>    2. var test1 = java.lang.Math.cos(0);
>    3. var test2 =
> org.eclipse.jface.viewers.StructuredSelection.EMPTY;
>    4. var test3 = ru.griffo.core.ui.utils.TestClass.variable;
>
> It gets error:
>
>     org.mozilla.javascript.EcmaError: ReferenceError: "ru" is not
> defined. (<cmd>#1)

Unlike "com", "java", etc., "ru" is not defined by default in Rhino.
You have to prefix it with "Packages" as you've done below.


>
> Class TestClass is in the same package my script util is. When I
> changed script to:
>
>    1.
>    2. hibernate = Packages.org.hibernate.criterion;
>    3. jface = Packages.org.eclipse.jface.viewers;
>    4. test = Packages.ru.griffo.core.ui.utils;
>    5.
>    6. var test1 = java.lang.Math.cos(0);
>    7. var test2 = jface.StructuredSelection.EMPTY;
>    8. var test3 = test.TestClass.variable;
>    9. var test4 = test.TestClass.test();
>
> it gets the same error I get with hibernate Restriqtions.eq():
>
>     org.mozilla.javascript.EcmaError: TypeError: Cannot call property
> test in object [JavaPackage ru.griffo.core.ui.utils.TestClass]. It is
> not a function, it is "object". (<cmd>#1)

The fact that it thinks that ru.griffo.core.ui.utils.TestClass is a
package rather than a class tells me that there's some kind of
classpath problem. Is the TestClass in the classpath? Perhaps you're
using the -jar option to "java" so that it ignores the classpath?

viktor stolbin

unread,
Sep 1, 2009, 10:18:08 AM9/1/09
to

Thank you for respond!
I check classpath, to be more exact, build-paths of eclipse projects,
and didn't find out anything unusual: package exports and plugin
dependencies. I have following design:
plugin project "core" (includes and exports js.jar)
plugin project "core.ui" (includes and exports TestClass and
ScroptUtils, depends on "ru.griffo.core")
plugin project "userdomain" (runs scripts, depends on both above)

Any suggestions will be helpfull.

viktor stolbin

unread,
Sep 2, 2009, 6:11:57 AM9/2/09
to

I addition, I tried to change script and find out more detais of it's
executing

Script Text:
hibernate = Packages.org.hibernate.criterion;
jface = Packages.org.eclipse.jface.viewers;
test = Packages.ru.griffo.core.ui.utils;

var test1 = java.lang.Math.cos(0);
out.println(test1);
var test2 = jface.StructuredSelection.EMPTY;
out.println(test2);
var test3 = test.TestClass.variable;
out.println(test3);

The result was:
1.0
[JavaPackage org.eclipse.jface.viewers.StructuredSelection.EMPTY]
[JavaPackage ru.griffo.core.ui.utils.TestClass.variable]

Does it mean that rhino hadn't recognize StructuredSelection and
TestClass classes?
Before using rhino 1.7R2 I used embedded one from Java 6 SE, but it
didn't fullfill my expectations about varargs, thought it works
perfectly with any thirdparty classes with no extra efforts.

Norris Boyd

unread,
Sep 2, 2009, 2:13:36 PM9/2/09
to

Yes. As a double check one thing you could try is calling
java.lang.Class.forName('ru.griffo.core.ui.utils.TestClass') as a way
to see if the class is indeed in the classpath.

0 new messages