1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315
|
<?xml version="1.0" encoding="UTF-8"?>
<html><head><title>Quick Start</title></head><body bgcolor="ffffff"><table cellspacing="10"><tr><td align="center"><a href="http://www.beanshell.org/"><img src="../images/homebutton.gif"/><br/>Home</a></td><td><a href="intro.html#Introduction"><img src="../images/backbutton.gif"/><br/>Back
</a></td><td align="center"><a href="contents.html"><img src="../images/upbutton.gif"/><br/>Contents</a></td><td align="center"><a href="syntax.html#Basic_Syntax"><img src="../images/forwardbutton.gif"/><br/>Next
</a></td></tr></table><h1>Quick Start</h1>
Welcome to BeanShell.
This is a crash course to get you going. We'll leave out many
important options and details. Please see the rest of the user's guide
for more information.
<p CLEAR="ALL"/>
<h2><a name="Download_and_Run_BeanShell">
Download and Run BeanShell
</a></h2>
Download the latest JAR file from http://www.beanshell.org
and start up BeanShell either in the graphical desktop mode or on
the command line.
<p CLEAR="ALL"/>
If you just want to start playing around you may be able to launch the
BeanShell desktop by simply double clicking on the BeanShell JAR file. More
generally however you'll want to add the jar to your classpath so that you
can work with your own classes and applications easily.
<p CLEAR="ALL"/>
To do this you can either drop the BeanShell JAR file into your Java
extensions folder or add it to your classpath. (Important: If you put
BeanShell in the extensions folder and wish to use it with BSF applications
like Jakarta Ant you must install the bsf.jar in the same location).
<p/><center><table border="1" cellpadding="5" width="100%"><tr><td bgcolor="#dfdfdc"><pre>
To install as an extension place the bsh.jar file in your
$JAVA_HOME/jre/lib/ext folder. (OSX users: place the bsh.jar in
/Library/Java/Extensions or ~/Library/Java/Extensions for individual users.)
Or add BeanShell to your classpath like this:
unix: export CLASSPATH=$CLASSPATH:bsh-xx.jar
windows: set classpath=%classpath%;bsh-xx.jar
</pre></td></tr></table></center><p/>
<p/><center><table cellpadding="5" border="1" width="100%"><tr><td><strong>Tip:</strong><br CLEAR="ALL"/>
You can modify the classpath from within BeanShell using the addClassPath()
and setClassPath() commands.
</td></tr></table></center><p/>
You can then run BeanShell in either a GUI or command line mode:
<p/><center><table border="1" cellpadding="5" width="100%"><tr><td bgcolor="#dfdfdc"><pre>
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
</pre></td></tr></table></center><p/>
<p CLEAR="ALL"/>
It's also possible to call BeanShell from within your own Java applications,
to reach it in a remote server mode for debugging, to use it as a servlet,
or even in an applet. See "BeanShell Modes of Operation" for more details.
<h2><a name="The_BeanShell_GUI">
The BeanShell GUI
</a></h2>
The BeanShell GUI desktop is meant to allow some experimentation with the
features of BeanShell. It is not intended to be a replacement for a full
featured IDE. Please check out the
<a href="http://www.jedit.org/">jEdit editor</a> for an example of a full
featured development environment based in part on BeanShell scripting
capabilities.
<p CLEAR="ALL"/>
Upon starting the BeanShell in GUI mode a console window will open.
By right clicking on the desktop background you can open additional
console windows and other tools such as a simple class browser.
<p CLEAR="ALL"/>
Each console window runs a separate instance of the BeanShell interpreter.
The graphical console supports basic command history, line editing,
cut and paste, and even class and variable name completion. From the console
you can open a simple editor window. In it you can write
scripts and use the 'eval' option to evaluate the text in the attached
console's workspace or a new workspace.
<p CLEAR="ALL"/>
<h2><a name="Java_Statements_and_Expressions">
Java Statements and Expressions
</a></h2>
BeanShell understands standard Java statements, expressions, and
method declarations.
Statements and expressions are all of the normal things that you'd say
inside a Java method such as variable declarations and assignments,
method calls, loops, and conditionals.
<p CLEAR="ALL"/>
You can use these exactly as they would appear in Java,
however in BeanShell you also have the option of working with "loosely typed"
variables. That is, you can simply omit the types of variables that you use
(both primitives and objects). BeanShell will only
signal an error if you attempt to misuse the actual type of the variable.
<p CLEAR="ALL"/>
Here are some examples:
<p/><center><table border="1" cellpadding="5" width="100%"><tr><td bgcolor="#dfdfdc"><pre>
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);
</pre></td></tr></table></center><p/>
<p CLEAR="ALL"/>
<h2><a name="Useful_BeanShell_Commands">
Useful BeanShell Commands
</a></h2>
In the previous example we used a convenient "built-in" BeanShell command
called print(), to display values. print() does pretty
much the same thing as System.out.println() except that it insures that the
output always goes to the command line. print() also displays some types
of objects (such as arrays) more verbosely than Java would.
Another related command is show(), which toggles on and off automatic
display of the result of every line you type.
<p CLEAR="ALL"/>
Here are a few other examples of BeanShell commands:
<ul>
<li> <strong>source(), run()</strong>
- Read a bsh script into this interpreter, or run it
in a new interpreter</li>
<li> <strong>frame()</strong>
- Display a GUI component in a Frame or JFrame.</li>
<li> <strong>load(), save()</strong>
- Load or save serializable objects to a file.</li>
<li> <strong>cd(), cat(), dir(), pwd(), etc.</strong>
- Unix-like shell commands</li>
<li> <strong>exec()</strong> - Run a native application </li>
<li><strong>javap()</strong> - Print the methods and fields of an object,
similar to the output of the Java javap command.</li>
<li> <strong>setAccessibility()</strong>
- Turn on unrestricted access to private and protected components.</li>
</ul>
<p CLEAR="ALL"/>
See the complete list of <a href="bshcommands.html">BeanShell Commands</a>
for more information.
<p CLEAR="ALL"/>
<p/><center><table cellpadding="5" border="1" width="100%"><tr><td><strong>Tip:</strong><br CLEAR="ALL"/>
BeanShell commands are not really "built-in" but are simply BeanShell scripts
that are automatically loaded from the classpath. You can add your
own scripts to the classpath to extend the basic command set.
</td></tr></table></center><p/>
<h2><a name="Scripted_Methods">
Scripted Methods
</a></h2>
You can declare and use methods in BeanShell just as you would in a Java class.
<p/><center><table border="1" cellpadding="5" width="100%"><tr><td bgcolor="#dfdfdc"><pre>
int addTwoNumbers( int a, int b ) {
return a + b;
}
sum = addTwoNumbers( 5, 7 ); // 12
</pre></td></tr></table></center><p/>
Bsh methods may also allow dynamic (loose) argument and return types.
<p/><center><table border="1" cellpadding="5" width="100%"><tr><td bgcolor="#dfdfdc"><pre>
add( a, b ) {
return a + b;
}
foo = add(1, 2); // 3
foo = add("Oh", " baby"); // "Oh baby"
</pre></td></tr></table></center><p/>
<h2><a name="Implementing_Interfaces">Implementing Interfaces</a></h2>
<em>Note: implementing arbitrary interfaces requires BeanShell be run
under a Java 1.3 or higher environment.</em>
<p CLEAR="ALL"/>
You can use the standard Java anonymous inner class syntax to implement an
interface type with a script. For example:
<p/><center><table border="1" cellpadding="5" width="100%"><tr><td bgcolor="#dfdfdc"><pre>
ActionListener scriptedListener = new ActionListener() {
actionPerformed( event ) { ... }
}
</pre></td></tr></table></center><p/>
You don't have to script all of the methods of an interface. You can opt to
script only those that you intend to call if you want to.
The calling code will simply throw an exception if it tries to
invoke a method that isn't defined.
If you wish to override the behavior of a large number of methods - say to
produce a "dummy" adapter for logging - you can implement a special method
signature: invoke(name, args) in your scripted object. The invoke() method
is called to handle any undefined method invocations:
<p/><center><table border="1" cellpadding="5" width="100%"><tr><td bgcolor="#dfdfdc"><pre>
ml = new MouseListener() {
mousePressed( event ) { ... }
// handle the rest
invoke( name, args ) { print("Method: "+name+" invoked!");
}
</pre></td></tr></table></center><p/>
<h2><a name="Scripted_Objects">
Scripted Objects
</a></h2>
In BeanShell, as in JavaScript and Perl, method "closures" allow you to create
scripted objects. You can turn the results of a method call into an object
reference by having the method return the special value <strong>this</strong>.
You can then use the reference to refer to any variables set during the method
call. Useful objects need methods of course, so in BeanShell scripted methods
may also contain methods at any level. For example:
<p/><center><table border="1" cellpadding="5" width="100%"><tr><td bgcolor="#dfdfdc"><pre>
foo() {
print("foo");
x=5;
bar() {
print("bar");
}
return this;
}
myfoo = foo(); // prints "foo"
print( myfoo.x ); // prints "5"
myfoo.bar(); // prints "bar"
</pre></td></tr></table></center><p/>
If this "closure" thing seems strange to don't worry. It's just an
evolutionary step that languages acquired along the path to Objects.
Please see the user's manual for a more thorough explanation.
<p CLEAR="ALL"/>
Within your scripts, BeanShell scripted objects
(i.e. any <em>'this'</em> type reference like myFoo in the previous example)
can automatically implement any Java interface type. When Java code calls
methods on the interface the corresponding scripted methods will be invoked to
handle them. BeanShell will automatically "cast" your scripted object
when you attempt to pass it as an argument to a method that takes an interface
type.
For passing script references outside of BeanShell, you can perform an
explicit cast where necessary. Please see the user manual for full details.
<p CLEAR="ALL"/>
<h2><a name="Calling_BeanShell_From_Your_Application">Calling BeanShell From Your Application</a></h2>
You can evaluate text and run scripts from within your application by
creating an instance of the BeanShell interpreter and using the eval()
or source() commands. You may pass in variable references to objects you
wish to use in scripts via the set() method and retrieve results with the
get() method.
<p/><center><table border="1" cellpadding="5" width="100%"><tr><td bgcolor="#dfdfdc"><pre>
import bsh.Interpreter;
Interpreter i = new Interpreter(); // Construct an interpreter
i.set("foo", 5); // Set variables
i.set("date", new Date() );
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");
</pre></td></tr></table></center><p/>
<p/><center><table cellpadding="5" border="1" width="100%"><tr><td><strong>Tip:</strong><br CLEAR="ALL"/>In the above example the Interpreter's eval() method also returned the
value of bar as the result of the evaluation.</td></tr></table></center><p/>
<h2><a name="Conclusion">Conclusion</a></h2>
We hope this brief introduction gets you started. Please see the full user
manual for more details. Please consult the mailing list archives for
more useful information. http://www.beanshell.org/
<table cellspacing="10"><tr><td align="center"><a href="http://www.beanshell.org/"><img src="../images/homebutton.gif"/><br/>Home</a></td><td><a href="intro.html#Introduction"><img src="../images/backbutton.gif"/><br/>Back
</a></td><td align="center"><a href="contents.html"><img src="../images/upbutton.gif"/><br/>Contents</a></td><td align="center"><a href="syntax.html#Basic_Syntax"><img src="../images/forwardbutton.gif"/><br/>Next
</a></td></tr></table></body></html>
|