Access public enums of Java Classes from JavaScript

1,691 views
Skip to first unread message

P

unread,
Apr 12, 2013, 8:21:59 AM4/12/13
to mozill...@googlegroups.com
Hi,

Trying to access a public enum in a Java class from JavaScript like Packages.com.mycompany.MyClass.MyEnum, but unfortunatly it fails with the following error:
org.mozilla.javascript.EvaluatorException: Java class "com.mycompany.MyClass" has no public instance field or method named "MyEnum". (internal_anon#1)

Now, I'm using Rhino 1.7R2 and unfortunatly I cannot upgrade. But I wonder if upgrading would help. I tried searching bugzilla to find anything on the topic and also the Google Groups, but no avail.

Only thing I found was this discussion: http://web.archiveorange.com/archive/v/ZPEIkiAkiEnYl4oabuNZ, but it doesn't state if the bug report was ever filed and if something was done with it.

Note: I'm trying to dynamically just access any public enum on any public Java class

Hannes Wallnöfer

unread,
Apr 13, 2013, 1:58:34 AM4/13/13
to mozilla-rhino
I think there is a resolved bugzilla issue for this. It works with current Rhino:

js> java.util.concurrent.TimeUnit.SECONDS.toMillis(123)
123000

Hannes



2013/4/12 P <pgba...@gmail.com>

--
You received this message because you are subscribed to the Google Groups "mozilla-rhino" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mozilla-rhin...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

P

unread,
Apr 16, 2013, 5:44:34 AM4/16/13
to mozill...@googlegroups.com
Hi Hannes,

Tnx for the response. I've tested your code snippet in Rhino 1.7R2 and there is works as well. So I went and had a look at why it doesn't work for the enum I'm trying to access. This is the simplified version of the code with the enum I'm trying to access from scripting:

package com.mycompany.mypackage;

public class TestEnum
{
public TestEnum()
{

}

public enum testEnum
{
ENUM_TEST_VALUE
}
}

And the JavaScript code I'm using:
var test  = new Packages.com.mycompany.mypackage.TestEnum()
log(test.testEnum.TEST_VALUE)

This setup is a bit different to the setup of the java code you used as example and unfortunately this setup doesn't work in Rhino 1.7R2

Can you confirm it works in the latest Rhino?

P.

Hannes Wallnöfer

unread,
Apr 18, 2013, 11:59:26 AM4/18/13
to mozilla-rhino
It doesn't work with a TestEnum instance, but it works just using the class name, accessing the enum like a static member:

Packages.com.mycompany.mypackage.TestEnum.testEnum.TEST_VALUE

Hannes

2013/4/16 P <pgba...@gmail.com>

P

unread,
Apr 18, 2013, 2:55:45 PM4/18/13
to mozill...@googlegroups.com
Hi Hannes,

That is one of the many variations I already tried, but that gives me the following error:
org.mozilla.javascript.EvaluatorException: Java class "com.mycompany.mypackage.TestEnum" has no public instance field or method named "testEnum". (internal_anon#1)

P.

Mrudula Devi Naraparaju

unread,
Jun 9, 2016, 1:16:27 AM6/9/16
to mozilla-rhino
Hi,
        i have tried the solution but getting Packages in not defined in javascript at the time runnig my application.could any one please help me in solving this issue
Reply all
Reply to author
Forward
0 new messages