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

Calling Java CLI programs through Rhino

4 views
Skip to first unread message

aschearer

unread,
Oct 25, 2009, 1:51:16 AM10/25/09
to
Hey, I've heard a number of things about this project before but only
recently dipped my toe in. So far I'm really pleased by what I'm
finding -- keep up the good work! One question I had, though, was
whether it is possible to blend Java CLI programs with Rhino's shell.
I would very much like to combine the scripting power provided by the
shell with the functionality present in programs I've written. In this
case, I am thinking a little of Microsoft's Powershell as a model. As
far as I can tell I am not able to simply invoke a program and pass
arguments. Is that correct? How could I go about adding that kind of
support -- maybe it's impossible since I suppose "program -f arg"
isn't exactly Javascript...

The dream scenario might work as follows. Write programs in Java to
perform CRUD operations, use Rhino shell to script using CLI programs
and data objects they create. Today I can get close to that by using
reflection and a launcher but this solution is fairly cumbersome.

Any help or insight would be appreciated, thanks!

David Parks

unread,
Oct 25, 2009, 2:52:33 PM10/25/09
to dev-tech-js-...@lists.mozilla.org
I don't personally play with the shell, but I don't expect you would have
too much of a problem extending it to add your own Javascript methods. Take
a look at the embedding tutorial to get an idea of how you can add your own
Javascript objects. In extending the shell I suspect you'll be doing
something like this.

http://www.mozilla.org/rhino/tutorial.html

Let me know if I'm off base with my thoughts here.

Dave

_______________________________________________
dev-tech-js-engine-rhino mailing list
dev-tech-js-...@lists.mozilla.org
https://lists.mozilla.org/listinfo/dev-tech-js-engine-rhino

Eric Lee

unread,
Oct 28, 2009, 3:47:10 PM10/28/09
to David Parks, dev-tech-js-...@lists.mozilla.org
Hi,

I think I am in the same boat as aschearer. I am just beginning at looking
at embeddable scripting languages. I see that it is possible to create New
java objects through Rhino and work with these objects. But is it possible
for Rhino script to interact with an existing running Java application?

For example,
I have a Java Application with a package with a class which has a private
variable int "count", a main function and another function called
jFunction(int num) { count += num; }
I run this application with Rhino embeded and then I would like to use Rhino
shell to call the jFunction from the running instance of the application
with the following script

function jFunctionCall(num) {
jFunction(num) // call the java function of the running application
}

jFunction(num)

calling this function would then add num to the count variable in the Java
object

Is this possible? I may be totally lost here at what Rhino can do. Please
let me know if you'd like me to clarify further..or if I am not making any
sense at all.
I guess the question comes down to, can I use Rhino as a command prompt /
command line interface to control my application.

Thank you very much for any help
Eric


On Sun, Oct 25, 2009 at 11:52 AM, David Parks <davidp...@yahoo.com>wrote:

> I don't personally play with the shell, but I don't expect you would have
> too much of a problem extending it to add your own Javascript methods. Take
> a look at the embedding tutorial to get an idea of how you can add your own
> Javascript objects. In extending the shell I suspect you'll be doing
> something like this.
>
> http://www.mozilla.org/rhino/tutorial.html
>
> Let me know if I'm off base with my thoughts here.
>
> Dave
>
>
> -----Original Message-----
> From:
> dev-tech-js-engine-rhino-bounces+davidparks21=yaho...@lists.mozilla.org
> [mailto:dev-tech-js-engine-rhino-bounces+davidparks21<dev-tech-js-engine-rhino-bounces%2Bdavidparks21>
> =yaho...@lists.mozill
> a.org] On Behalf Of aschearer
> Sent: Sunday, October 25, 2009 12:51 PM
> To: dev-tech-js-...@lists.mozilla.org
> Subject: Calling Java CLI programs through Rhino
>

Daryl Stultz

unread,
Oct 28, 2009, 4:17:32 PM10/28/09
to Rhino JS USer List
On Wed, Oct 28, 2009 at 3:47 PM, Eric Lee <eri...@gmail.com> wrote:

> Hi,
>
> I think I am in the same boat as aschearer. I am just beginning at looking
> at embeddable scripting languages. I see that it is possible to create New
> java objects through Rhino and work with these objects. But is it possible
> for Rhino script to interact with an existing running Java application?
>

Sure. I don't have any experience with the shell, but I think the idea is
the same: when you run a script in a "context" (or is it "scope"?) you
decide what to expose to the script. So you can expose the method jFunction
or you can add a property that is "the application" and just call whatever
public methods it has.

What I do is add "extension points" to my app where at various points I run
a script designed to extend the logic at that point and pass in parameters
to the function and have it modify the parameters, return new data, etc.

--
Daryl Stultz
_____________________________________
6 Degrees Software and Consulting, Inc.
http://www.6degrees.com
mailto:da...@6degrees.com

Patrick Dobbs

unread,
Oct 28, 2009, 8:26:29 PM10/28/09
to
> Is this possible? I may be totally lost here at what Rhino can do. Please
> let me know if you'd like me to clarify further..or if I am not making any
> sense at all.
> I guess the question comes down to, can I use Rhino as a command prompt /
> command line interface to control my application.

Yes, I think you can. Based on my fumblings there seems to be an easy
way and a hard way.

For the easy way, you initialize and run your application from within
the rhino shell. You can do this directly from the shell, but that gets
boring because you generally need quite a few lines to bootstrap your
application. The solution here is to write a script, then call this
script from rhino with the "-i" command line argument so the script runs
and you get dropped back into the shell. See
http://www.mozilla.org/rhino/scriptjava.html

The other approach is to start your application normally and then have
some code in your application which embeds rhino.

For example, if your application is a servlet based webapp you can write
a servlet which takes javascript code as an HTTP POST value and executes
it passing in whatever variables/objects you want to expose. Naturally
there are all sorts of security issues with this example, but that's a
different issue. http://www.mozilla.org/rhino/tutorial.html

To get a proper interactive CLI interface from an embedded rhino setup
is, I think, more difficult since you've got to figure out how to do the
IO.

A good way to begin is to start up the shell, setting your classpath to
include your application classes, and then start interacting with your
own classes to get a feel for what you can do.

For command line experimenting it's worth while enabling readline to get
tab completion and better history as per Norris Boyd's blog post:
http://blog.norrisboyd.com/2009/03/rhino-17-r2-released.html

Hope that helps.

0 new messages