Java Imports
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 importing classes in beanshell script.
/*
Default imports
* javax.swing.event
* javax.swing
* java.awt.event
* java.awt
* java.net
* java.util
* java.io
* java.lang
*/
import com.bethecoder.tutorials.bean_shell.common.Student;
std = new Student ( "Sriram" , 2 , "Cricket" ) ;
print ( std ) ;
package com.bethecoder.tutorials.bean_shell.common;
public class Student {
private String name;
private int age;
private String hobby;
public Student () {
}
public Student ( String name, int age, String hobby ) {
super () ;
this .name = name;
this .age = age;
this .hobby = hobby;
}
public String getName () {
return name;
}
public void setName ( String name ) {
this .name = name;
}
public int getAge () {
return age;
}
public void setAge ( int age ) {
this .age = age;
}
public String getHobby () {
return hobby;
}
public void setHobby ( String hobby ) {
this .hobby = hobby;
}
public boolean isKid () {
return age < 5 ;
}
public String toString () {
return "Student[name = " + name + ", age = " + age + ", hobby = " + hobby + "]" ;
}
}
package com.bethecoder.tutorials.bean_shell;
import java.io.InputStreamReader;
import bsh.EvalError;
import bsh.Interpreter;
public class ImportTest {
/**
* @param args
* @throws EvalError
*/
public static void main ( String [] args ) throws EvalError {
Interpreter interpreter = new Interpreter () ;
InputStreamReader reader = new InputStreamReader (
ImportTest. class .getClassLoader ()
.getResourceAsStream ( "import.bsh" )) ;
interpreter.eval ( reader ) ;
}
}
It gives the following output,
Student[name = Sriram, age = 2, hobby = Cricket]