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

Help with package import

2,535 views
Skip to first unread message

whate...@gmail.com

unread,
Oct 26, 2007, 11:29:18 AM10/26/07
to
I have rhino version 1.6.7, and it's all working - cool. One thing I
can't figure out how to do is import a package.

I have a class, say com.foo.bar.Baz. I can create one of these, no
problems:

js%> var s = Packages.com.foo.bar.Baz()

I don't want to scatter all these full package references
(Packages.com.foo.bar) through code - I want to do:

js%> var s = Baz()

Help!?

On the docs (http://www.mozilla.org/rhino/ScriptingJava.html) is says
"if your scripts access a lot of different Java classes it can get
awkward to use the full package name of the class every time. Rhino
provides a top-level function importPackage that serves the same
purpose as Java's import declaration."

js%> importPackage(com.foo.bar)

But I get an error:

Exception thrown interpreting script:org.mozilla.javascript.EcmaError:
ReferenceError: "importPackage" is not defined. (<cmd>#2)

Is there a way to do this?

Any help is greatly appreciated.

Christophe Grand

unread,
Oct 26, 2007, 11:49:59 AM10/26/07
to
To import a single class:
Baz = Packages.com.foo.bar.Baz;
var s = Baz();

you can also write:
b = Packages.com.foo.bar;
var s = b.Baz();

To use importPackage your root scope must be created with new
ImporterTopLevel(cx).

whate...@gmail.com a écrit :

whate...@gmail.com

unread,
Oct 26, 2007, 11:58:20 AM10/26/07
to
Oh, duh - I see what's going on. :)

Thanks a ton - the second idiom you outline (myPackage =
Packages.com.foo.bar) is *exactly* what I want to do.

Cheers,

parki...


On Oct 26, 11:49 am, Christophe Grand <christo...@cgrand.net> wrote:
> To import a single class:
> Baz = Packages.com.foo.bar.Baz;
> var s = Baz();
>
> you can also write:
> b = Packages.com.foo.bar;
> var s = b.Baz();
>
> To use importPackage your root scope must be created with new
> ImporterTopLevel(cx).
>

> whatever...@gmail.com a écrit :

0 new messages