|
Quick Start
Welcome to beanshell. This is a quick course. We will save some important options and details. For more information, see other sections of this user's guide.
Download and run beanshell
Download the latest JAR file from the http://www.beanshell.org. You can start beanshell in graphic desktop mode and command line mode.
If you only want to play beanshell, you can double-click the beanshell JAR file to start the beanshell desktop. However, if you want beanshell to work with your class and application, you must add the beanshell JAR file to classpath.
You can drag the beanshell JAR file to the ext directory of java_home or directly add it to classpath.
- For Windows users, place bsh. jar in the java_home/JRE/lib/EXT folder. For OSX users, place bsh. jar in/library/Java/extensions.
Or add the beanshell to your classpath directory, for example:
UNIX: Export classpath = $ classpath: bsh-xx.jar
Windows: Set classpath %; bsh-xx.jar
Then you can run beanshell in Gui or command line mode:
- Java bsh. Console // run the graphical desktop
Or
Java bsh. interpreter // run as text-only on the command line
Or
Java bsh. interpreter filename [ARGs] // run script file
You can run beanshell in your application, as the remote server mode of debug and Servlet, or as an applet. For more information, see "beanshell modes of operation.
Beanshell Gui
After you start beanshell in Gui mode, a desktop window is opened. Right-click on the background of the desktop. You can open another console window and other tools, such as a simple browser.
Each Console window runs an independent beanshell interpreter. The graphical console supports basic historical commands, line editing, cutting and pasting, and even Automatic completion of classes and variable names. You can open a simple editing window on the console. In it, you can write scripts and use the 'eval' option to evaluate the expression value.
Java statements and expressions
Beanshell can understand standard Java statements, expressions, and method declarations. The statement and expression content can be: Variable, declaration, assignment, method call, loop, condition, and so on.
You must strictly use them in Java programs, but in beanshell, you can use them in loosely typed mode. That is to say, you can be lazy when writing a script without declaring the variable type (both the original data type and object can be used ). If you try to use the wrong variable type, beanshell will give an error.
Here are some examples:
- Foo = "foo ";
Four = (2 + 2) * 2/2;
Print (FOO + "=" + four); // print () is a beanshell command
// Do a loop
For (I = 0; I <5; I ++)
Print (I );
// Pop up a frame with a button in it
Button = new jbutton ("My button ");
Frame = new jframe ("My frame ");
Frame. getcontentpane (). Add (button, "center ");
Frame. Pack ();
Frame. setvisible (true );
Useful beanshell commands
In the preceding example, we used a convenient command "Print ()" built in beanshell to display the variable value. Print () is very similar to Ava's system. Out. println () unless it ensures that the output is always a command line. Print () can also display some object types (such as arrays), but more detailed than Java. Another related command is show (), which is used to enable or disable displaying the results of each line you entered.
Here are some other beanshell commands:
Source (), run ()-read a bsh script or run it in another interpreter.
Frame ()-displays a frame or jframe GUI component.
Load (), save ()-load and save a serialized object to a file.
CD (), CAT (), Dir (), PWD (), etc.-Unix-like shell commands.
Exec ()-run a local program.
Javap ()-prints the methods and fields of an object, similar to Java's javap command.
Setaccessibility ()-enable unrestricted access to private and protected components.
For more information, see the detailed list of beanshell commands.
Tip:
Beanshell commands are not really "Built-in", but are automatically loaded from classpath as script methods. You can extend the basic command set and add it to classpath for custom scripts.
Script Method
You can advertise and use methods in bsh, just like in Java classes.
- Int addtwonumbers (int A, int B ){
Return A + B;
}
Sum = addtwonumbers (5, 7); // 12
The bsh method can have dynamic (loose) parameters and return types.
- Add (a, B ){
Return A + B;
}
Foo = add (1, 2); // 3
Foo = add ("Oh", "baby"); // "Oh baby"
Interface implementation
Note: To enable beanshell to implement any interface, jdk1.3 and above must be supported.
You can use the standard Java internal class syntax in the script to implement the interface. For example:
- Actionlistener scriptedlistener = new actionlistener (){
Actionreceivmed (event ){...}
}
You don't need to implement all the interface methods, but just implement the methods you need. If an unimplemented method is called in the code, an exception is thrown. If you want to overload the behavior of a large number of methods-for example, to generate a "dumb" adapter for logs-you can implement a special method in the script object: invoke (name, argS ). The invoke () method is used to handle calls to any unspecified methods:
- ML = new mouselistener (){
Mousepressed (event ){...}
// Handle the rest
Invoke (name, argS) {print ("method:" + name + "invoked! ");
}
Script object
In beanshell, like in JavaScript and Perl, the script object is composed of a closed method body. By returning a special value "this" at the end of the method, you can call this object just like using the method. When calling this method, you can give it any value. Generally, methods must be included in an object. To some extent, beanshell script methods can contain methods to form a script object. For example:
- Foo (){
Print ("foo ");
X = 5;
Bar (){
Print ("bar ");
}
Return this;
}
Myfoo = Foo (); // prints "foo"
Print (myfoo. X); // prints "5"
Myfoo. Bar (); // prints "bar"
If these codes are unfamiliar to you, don't worry. Please give a more thorough explanation in the user manual.
In your script, the beanshell script object (referred to as "this" in the previous example) can automatically implement any Java interface type. When the Java code calls the method in the script method corresponding to the communication. When you try to pass the script object as a parameter to the Java method, beanshell automatically shapes it as the corresponding type. If you want to pass an object outside beanshell, You can explicitly create a shape (CAST) as needed. Please refer to the details in the user manual.
Call beanshell from your application
By creating a beanshell interpreter and using the eval () or source () commands, you can evaluate the value of the text expression and run the script in your application. If you want to use an object in your script, you can use the Set () method to pass the object variable to beanshell and get the result through the get () method.
- Import bsh. interpreter;
Interpreter I = new interpreter (); // construct an interpreter
I. Set ("foo", 5); // set variables
I. Set ("date", new date ());
Date = (date) I. Get ("date"); // retrieve a variable
// Eval a statement and get the result
I. eval ("bar = Foo * 10 ");
System. Out. println (I. Get ("bar "));
// Source an external script file
I. Source ("somefile. bsh ");
|