3

I would like to run class imported from different beanshell file. But I've no idea how instantiate class from main beanshell file. Is this possible?

Class which I import:

class HelloW {
public void run(){
    print("Hello World");
}
}

Main beanshell file which should run and instantiate class:

Interpreter i = new Interpreter();
i.source("HelloW.bsh");
PotOfTea
  • 213
  • 3
  • 12

1 Answers1

5

The BeanShell documentation is pretty good in this area, so you should read through that first. In your case there are few issues. That said, there are Scripted Objects. Also, the .bsh file you start with needs to execute the scripted object. Taking your example this code should work:

Hello() {
 run(){
     print("Hello World");
 }

 return this;
}

myHello = Hello();
myHello.run(); // Hello World

*UPDATED answer for version BeanShell 2.0b1 and later which support scripted classes *:

I created two beanshell files and placed them in a directory "scripts".

The first "executor.bsh" is what you are calling the "parent" script, I believe.

// executor.bsh

addClassPath(".");
importCommands("scripts");

source(); // This runs the script which defines the class(es)

x = new HelloWorld();
x.start();

The second file contains the scripted class. Note that I am using a Scripted Command and according to BeanShell documentation, the file name must be the same as the command name.

// source.bsh

source() {
    public class HelloWorld extends Thread {
        count = 5;
        public void run() {
            for(i=0; i<count; i++)
                print("Hello World!");
        }

    }
}

I invoked executor.bsh in a java class with:

Interpreter i = new Interpreter();
i.source("scripts/executor.bsh");

// Object val = null;
    // val = i.source("scripts/executor.bsh");
// System.out.println("Class:" + val.getClass().getCanonicalName());
// Method m = val.getClass().getMethod("start", null);
// m.invoke(val, null);

Note that I left some commented code which also shows me executing the scripted class from Java, using Reflection. And this is the result:

Hello World!
Hello World!
Hello World!
Hello World!
Hello World!
GaryMcM
  • 425
  • 3
  • 9
  • Sorry, but you misunderstood my question, this class Hello is in separated bsh file, and I call it using "Interpreter". – PotOfTea Jan 09 '13 at 12:37
  • Hmmm. I expected you to put the code I gave in the HelloW.bsh and call it the way you have in the question. If you do it will instantiate the scripted object and call the run method. The file you give Interpreter has to be script of some kind and perform an action. Calling the `source()` method is the same as calling `eval()` except the script comes from a file, versus a string. – GaryMcM Jan 09 '13 at 17:39
  • OK thank you I got it, I assume there is no way run methods or instantiate class in parent bsh file (Basically import file with classes)? – PotOfTea Jan 10 '13 at 09:23
  • I just did a little checking, and if you are using the Beta 2.0b1 or b2 you should be able to define scripted classes. The product I work on is stuck on an older version. I have updated my answer to show you how I did what I think you're looking for. I hope it helps. – GaryMcM Jan 10 '13 at 13:12