2

I have a CMS server that provides a client library. I'd like to be able to drive the CMS interactively from the command line.

The basic approach would be:

  1. Create a connection to the CMS
  2. Add the CMS connection object to the REPL context
  3. Connect the REPL to stdout/stderr/stdin
  4. Kick off a daemon thread for to keep the REPL running.

I was hoping that I could perhaps leverage Groovy to do this but haven't managed to get it working.

Is there a library that provides REPL support?
Can you provide a simple example?

johnstok
  • 96,212
  • 12
  • 54
  • 76

4 Answers4

2

If you don't mind using Scala as your language, you can use the Scala REPL to explore java libraries. You can do this in a number of ways, either with

$ scala -classpath yourjarfileshere.jar

or if you're using maven:

mvn scala:console

If all you're doing is playing (not scripting or anything), then this is a possible way to go.

If you wish to embed your repl, and you're still willing to use Scala, you can look at the answer to these questions: Drop into interpreter during arbitrary scala code location and Launch Scala REPL programatically?

Groovy also has a repl, groovysh, which you can use to explore.

Community
  • 1
  • 1
Matthew Farwell
  • 60,889
  • 18
  • 128
  • 171
  • Thanks. I want to embed the REPL and distribute it to a few people. I'm aware I can just start a REPL for an existing language (Scala, Groovy, etc.) and add the JAR. I've looked at the GroovySh class but didn't manage to get it working :-( – johnstok Sep 06 '11 at 09:34
2

I got this working with Groovy.

Example

public static void main(final String[] args) {

    Binding binding = new Binding();
    // Configure your bindings here.

    Groovysh shell = new Groovysh(binding, new IO());
    shell.run(args);
}

Known Issues

However, it won't work when the app is started from Eclipse (ie using the Eclipse 'console' view). To work around this you must update the Eclipse launch configuration to pass the following VM argument:
-Djline.terminal=jline.UnsupportedTerminal.

More information

outro56
  • 62
  • 7
johnstok
  • 96,212
  • 12
  • 54
  • 76
1

Beanshell can be run as repl in your own thread/main within your application:

public static void main(String[] args) throws Exception{

    Reader inreader = new InputStreamReader(System.in);
    Interpreter i = new Interpreter(inreader, System.out, System.err, true);
    try {
        BufferedReader in = new BufferedReader(inreader);
        String str;
        while ((str = in.readLine()) != null) {
            i.eval(str);
        }
        in.close();
    } catch (Exception e) {
    }
}

that example runs in eclipse fine, you type at it in the console window of eclipse then it will talk back to you fine.

simbo1905
  • 6,321
  • 5
  • 58
  • 86
  • adding your own objects to the interpreter is easy. you can do `i.set("foo", "bar")` then if you were to run the program and type in `print(foo);` it will output `bar` to the console. this means you can add your services into the interpreter then speak to them over System.in e.g. if you use spring to configure and run your app you could pull out some beans from the application context and set them into the interpreter. – simbo1905 Aug 24 '12 at 13:04
  • the example code in this answer can be improved by wrapping the i.eval(str) in a try/catch which logs what the interpreter is complaining about. The outer try/catch shown in the code should log also but is likely to only see IO exceptions or thread interrupt exceptions when you are shutting down. – simbo1905 Sep 08 '12 at 22:21
  • the example code in this answer can be improved by executing import statements for your java packages before entering the repl. It is very tedious to have to type out long import statements interactively before you can jump into your logic. – simbo1905 Sep 08 '12 at 22:22
1

Wikipedia page for REPL mentions BeanShell. Would that work?

Petteri H
  • 11,779
  • 12
  • 64
  • 94