tl  tr
  Home | Tutorials | Articles | Videos | Products | Tools | Search
Interviews | Open Source | Tag Cloud | Follow Us | Bookmark | Contact   
 Java Scripting > BeanShell > Scripted Objects

Scripted Objects 

BeanShell is a small, free, embeddable Java source interpreter with object scripting language features, written in Java. BeanShell is a natural scripting language for Java. This requires the library bsh-2.0b4.jar to be in classpath. The following example shows creating a scripted object in beanshell script.

File Name  :  
/BEAN_SHELL001/config/scripted_objects.bsh 
   
mathlib() {

  //Property
  PI = 3.14;

  //Method
  sum(a, b) {
    return a + b;
  }
  
  return this;
}

lib = mathlib();
print("PI value : " + lib.PI)
print("Sum : " + lib.sum(24));
   

File Name  :  
com/bethecoder/tutorials/bean_shell/ScriptedObjectTest.java 
Author  :  Sudhakar KV
Email  :  [email protected]
   
package com.bethecoder.tutorials.bean_shell;

import java.io.InputStreamReader;

import bsh.EvalError;
import bsh.Interpreter;

public class ScriptedObjectTest {

  /**
   @param args
   @throws EvalError 
   */
  public static void main(String[] argsthrows EvalError {
    Interpreter interpreter = new Interpreter();  

    InputStreamReader reader = new InputStreamReader(
        ScriptedObjectTest.class.getClassLoader()
        .getResourceAsStream("scripted_objects.bsh"));
    interpreter.eval(reader);
  }

}
   

It gives the following output,
PI value : 3.14
Sum : 6



 
  


  
bl  br