File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Other Languages and the fly likes Interactive shell scripts (console style) in application Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Languages » Other Languages
Bookmark "Interactive shell scripts (console style) in application" Watch "Interactive shell scripts (console style) in application" New topic

Interactive shell scripts (console style) in application

Aaron Roberts
Ranch Hand

Joined: Sep 10, 2002
Posts: 174
I want to have a shell/console in an application. The idea is to make a very simple image application that just applies a few filters to an image. So I would have an image viewing area and an entry area for the shell commands.

In the shell/console area I'd enter commands which would execute methods on java classes. A psuedo syntax might be -

image = new Image("original.png");
Blur.blur(image, 10)
Crop.crop(image, 50, 150)

The idea in the above is that Blur.blur is a static method on a class. Thats a bit of design ignorance, since I don't know enough about scripting with java at all.

Another usage example could be -

load image1 "original.png"
img = blur(image1, 15)
img = crop(img, 50, 150)
save img "newimage.png"

So you can see from both usage examples, the ability to do dynamic inputs is the key part. The syntax isn't so important.

Can someone provide some input on how I might approach this? What might be a pro or con for one method over another?

I don't want to write my own parser for doing this. From what I understand I don't have to with some of the options out there.

I came across this great article on some scripting options out there -

Choosing A Java Scripting Language


Aaron R>
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15302

My first thought is to simply embed some interpreter into a small java application. Something like this. I'm not sure if Groovy supports something similar or not. I've got a question into the mailing list and I'll let you know as soon as I hear something back.

I'm interested to see what others have to say about this as well.

GenRocket - Experts at Building Test Data
Ulf Dittmer

Joined: Mar 22, 2005
Posts: 42965
Like Gregg I thought that this sounds like embedding a scripting engine. JavaScript has a Java-like syntax, so the Rhino engine might fit the bill, or BeanShell if you want to get even closer at Java. Both can be integrated via BSF (which has been the de-facto standard for integrating scripting languages in Java for a while), so you wouldn't even have to change the integration code if you switched script engines.
Patrick Finnegan
Ranch Hand

Joined: Mar 05, 2002
Posts: 179
You are spoiled for choice.

Jacl was originally created by Sun in 1994 so it's quite mature. It's also lightweight and easy to learn.
Aaron Roberts
Ranch Hand

Joined: Sep 10, 2002
Posts: 174
If I understood the purpose of BSF, its to provide connectivity between a language and java. This means to create a working language of my own, I'd need -

Java code I wanted to interact with via my script language
The Java 'glue' code that connected BSF and the above

I wasn't sure if that would actually give me the shell type console I desired.

I think I've settled on BeanShell (bsh), since it retains so much of the java syntax. Can anyone comment on how difficult it would be to make the image app I described? Using beanshell, I'd need -

My application code (this would open a JFrame with an image at the top and the text entry area at the bottom.)

Would the app code be able to use beanshell and instantiate a console?

Aaron R>
I agree. Here's the link:
subject: Interactive shell scripts (console style) in application
It's not a secret anymore!