<section>
<name filename="quickstart">Quick Start</name>

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/>

<h2>
Download and Run BeanShell
</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/>

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/>

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).

<example>
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:

<em>unix:</em>     export CLASSPATH=$CLASSPATH:bsh-xx.jar
<em>windows:</em>  set classpath=%classpath%;bsh-xx.jar
</example>

<tip>
You can modify the classpath from within BeanShell using the addClassPath()
and setClassPath() commands.
</tip>

You can then run BeanShell in either a GUI or command line mode:

<example>
    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
</example>
<p/>

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>
The BeanShell GUI
</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/>

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/>

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/>

<h2>
Java Statements and Expressions
</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/>

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/>

Here are some examples:
<example>
foo = "Foo";    
four = (2 + 2)*2/2;
print( foo + " = " + four );  // print() is a BeanShell command

// Do a loop
for (i=0; i&lt;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);
</example>
<p/>

<h2>
Useful BeanShell Commands
</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/>

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/>
See the complete list of <a href="bshcommands.html">BeanShell Commands</a>
for more information.
<p/>

<tip>
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.
</tip>

<h2>
Scripted Methods
</h2>

You can declare and use methods in BeanShell just as you would in a Java class.

<example>
int addTwoNumbers( int a, int b ) {
    return a + b;
}

sum = addTwoNumbers( 5, 7 );  // 12
</example>


Bsh methods may also allow dynamic (loose) argument and return types.  

<example>
add( a, b ) {
    return a + b;
}

foo = add(1, 2);            // 3
foo = add("Oh", " baby");   // "Oh baby"
</example>

<h2>Implementing Interfaces</h2>

<em>Note: implementing arbitrary interfaces requires BeanShell be run
under a Java 1.3 or higher environment.</em>
<p/>

You can use the standard Java anonymous inner class syntax to implement an
interface type with a script.  For example:

<example>
ActionListener scriptedListener = new ActionListener() {
    actionPerformed( event ) { ... }
}
</example>

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:

<example>
ml = new MouseListener() {
    mousePressed( event ) { ... }
    // handle the rest
    invoke( name, args ) { print("Method: "+name+" invoked!");
}
</example>

<h2>
Scripted Objects
</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:

<example>
foo() {
    print("foo");
    x=5;

    bar() {
        print("bar");
    }

    return this;
}

myfoo = foo();    // prints "foo"
print( myfoo.x ); // prints "5"
myfoo.bar();      // prints "bar"
</example>

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/>

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/>

<h2>Calling BeanShell From Your Application</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.

<example>
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");
</example>

<tip>In the above example the Interpreter's eval() method also returned the 
value of bar as the result of the evaluation.</tip>

<h2>Conclusion</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/

</section>

