Initial Commit
[packages] / xemacs-packages / jde / doc / html / bsh-ug / bsh-ug-content.html
1 <HTML>
2 <HEAD>
3    <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
4    <META NAME="GENERATOR" CONTENT="Mozilla/4.05 [en] (Win95; I) [Netscape]">
5    <TITLE>BeanShell User's Guide</TITLE>
6 </HEAD>
7 <BODY>
8
9 <H1>BeanShell User's Guide</H1>
10
11 <H2>
12 <A NAME="Introduction"></A>Introduction</H2>
13 This guide explains how to use version 1.2.7 of the BeanShell Java interpreter with the
14 Java Development Environment for Emacs. This guide contains the following
15 sections:
16 <BR>&nbsp;
17 <BR>&nbsp;
18 <TABLE BORDER=0 WIDTH="100%" >
19 <TR>
20 <TD><A HREF="#About">About the BeanShell</A>&nbsp;</TD>
21
22 <TD>Brief overview of the BeanShell&nbsp;</TD>
23 </TR>
24
25 <TR>
26 <TD VALIGN=TOP><A HREF="#Using">Using the BeanShell</A>&nbsp;</TD>
27
28 <TD>Explains how to start and shut down the BeanShell, enter statements,
29 and show results.</TD>
30 </TR>
31
32 <TR>
33 <TD VALIGN=TOP><A HREF="#LanguageRef">The BeanShell Language</A>&nbsp;</TD>
34
35 <TD>Describes the Java constructs and scripting extensions supported by
36 the BeanShell</TD>
37 </TR>
38
39 <TR>
40 <TD VALIGN=TOP><A HREF="#ClassPathManagement">Class Loading and Class Path Management</A></TD>
41
42 <TD>Explains how to load classes and change the BeanShell class path dynamically.</TD>
43 </TR>
44
45 <TR>
46 <TD><A HREF="#CommandRef">BeanShell Commands</A>&nbsp;</TD>
47
48 <TD>Describes the commands supported by the BeanShell</TD>
49 </TR>
50 </TABLE>
51 &nbsp;
52 <H2>
53 <A NAME="About"></A>About the BeanShell</H2>
54 The BeanShell is a Java interpreter that evaluates Java statements directly
55 (without first compiling them). The&nbsp; BeanShell has been included in
56 the JDE with the permission of its author,&nbsp; <A HREF="mailto:pat@pat.net">Pat
57 Niemeyer</A>.
58
59 <P>The BeanShell can operate as a stand-alone application or as a part
60 of another application. When running as a stand-alone application, the
61 interpreter accepts input from the command line of the shell in which it
62 runs. The BeanShell distribution includes a shell for running the interpreter.
63 It can, however, be run from other shells, such as bash or the DOS command
64 window. The JDE includes a command for running the BeanShell in an Emacs
65 shell (comint) buffer. This enables you to interact with the interpreter
66 directly while developing Java programs.
67
68 <P>The JDE also&nbsp; uses the interpreter to implement some JDE commands.
69 The JDE invokes the interpreter via Lisp functions that pass Java statements
70 to the interpreter via the standard input of the interpreter process and
71 retrieve results via the standard output. This guide documents BeanShell
72 functionality that seems relevant to JDE users.&nbsp; See the <A HREF="http://www.ooi.com/beanshell/">BeanShell
73 home page</A> home page for additional information and to download the
74 latest version of the BeanShell.
75 <H2>
76 <A NAME="Using"></A>Using the BeanShell</H2>
77
78 <H3>
79 <A NAME="Starting"></A>Starting the BeanShell</H3>
80 To start the BeanShell, select <B><FONT SIZE=-1>Interpret</FONT></B> from
81 the <B><FONT SIZE=-1>JDE</FONT></B> menu or enter the command <TT>M-x bsh</TT>.
82 Emacs starts the BeanShell, if not already started, and displays its prompt
83 in an Emacs shell buffer window.
84
85 <P><IMG SRC="images/BeanShellBuffer.gif" HEIGHT=536 WIDTH=664>
86
87 <P>The JDE allows you to run only one instance of the BeanShell at a time.
88 If&nbsp; an instance is already running, the <TT>bsh</TT> command simply
89 displays the buffer containing the instance, without starting another instance.
90 Note that you can indirectly start a BeanShell instance when you invoke
91 commands implemented as hybrid Lisp/Java scripts, such as <TT>jde-wiz-override-method</TT>.
92 Such commands start a BeanShell instance if one is not already running.
93 <H3>
94 <A NAME="Evaluating"></A>Evaluating Statements</H3>
95 To evaluate a Java statement, type it at the BeanShell prompt and press
96 the <B><FONT SIZE=-1>Enter</FONT></B> key. The BeanShell evaluates the
97 statement. When it is done, it redisplays the the BeanShell command prompt.
98 You can then enter another statement.
99
100 <P>Statements must conform to Java syntax. For example, simple statements
101 must end in a semicolon. Compound statements, such as if-then constructs,
102 must end in a right brace. Statements may span multiple lines. To continue
103 a statement on a new line, press the <B><FONT SIZE=-1>Enter</FONT></B>
104 key. The BeanShell does not evaluate the statement until you have entered
105 the last line of the statement.
106
107 <P><IMG SRC="images/BshMultiLineEx.gif" HEIGHT=264 WIDTH=576>
108 <BR>&nbsp;
109 <H3>
110 <A NAME="Displaying"></A>Displaying Results</H3>
111 You can display the results of evaluating Java statements via the BeanShell
112 <TT>print</TT> and <TT>show</TT> commands. The print command accepts any
113 Java expression as an argument and displays the result of evaluating that
114 expression&nbsp; in the BeanShell window. For example,
115 <BLOCKQUOTE><TT>print(2+2);</TT></BLOCKQUOTE>
116 displays
117 <BLOCKQUOTE><TT>4</TT></BLOCKQUOTE>
118 in the BeanShell window. Note that <TT>print(expr)</TT> is equivalent to
119 <TT>System.out.println(expr)</TT> and you can use either method to print
120 a Java expression.
121
122 <P>The <TT>show();</TT> command toggles automatic display of the results
123 of evaluating statements as they are entered.
124 <H3>
125 <A NAME="Exiting"></A>Exiting the Shell</H3>
126 To exit the BeanShell, enter
127 <BLOCKQUOTE><TT>exit();</TT></BLOCKQUOTE>
128 at the command prompt.
129 <H2>
130 <A NAME="LanguageRef"></A>The BeanShell Language</H2>
131
132 <H3>
133 <A NAME="JavaSupport"></A>Java Language Support</H3>
134 The BeanShell language includes most of the constructs of the Java language.
135 Standard Java constructs supported by the BeanShell include&nbsp; variable
136 assignments, method calls, math expressions, for-loops, etc.. Here are
137 some examples:
138 <PRE>&nbsp;&nbsp;&nbsp; // Use a hashtable
139 &nbsp;&nbsp;&nbsp; Hashtable h = new Hashtable();
140 &nbsp;&nbsp;&nbsp; Date d = new Date();
141 &nbsp;&nbsp;&nbsp; h.put("today", d);
142
143 &nbsp;&nbsp;&nbsp; // Print the current clock value
144 &nbsp;&nbsp;&nbsp; print( System.currentTimeMillis() );
145
146 &nbsp;&nbsp;&nbsp; // Loop
147 &nbsp;&nbsp;&nbsp; for (int i=0; i&lt;5; i++)
148 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print(i);
149
150 &nbsp;&nbsp;&nbsp; // Pop up an AWT frame with a button in it
151 &nbsp;&nbsp;&nbsp; Button b = new Button("My Button");
152 &nbsp;&nbsp;&nbsp; Frame f = new Frame("My Frame");
153 &nbsp;&nbsp;&nbsp; f.add(b, "Center");
154 &nbsp;&nbsp;&nbsp; f.pack();
155 &nbsp;&nbsp;&nbsp; f.show();</PRE>
156 By default, the BeanShell imports the Java core classes at startup. You
157 can import additional classes, using standard Java import syntax, for example,
158 <BLOCKQUOTE><TT>import mypackage.*;</TT></BLOCKQUOTE>
159 or
160 <PRE>&nbsp;&nbsp;&nbsp; import mypackage.MyClass;</PRE>
161
162 <H3>
163 <A NAME="ScriptExtensions"></A>Scripting Extensions</H3>
164 The BeanShell defines a number of extensions to the Java language designed
165 to facilitate creation of scripts.&nbsp; The scripting extensions include
166 <UL>
167 <LI>
168 Script Variables</LI>
169
170 <LI>
171 Script Methods</LI>
172
173 <LI>
174 Implicit Objects</LI>
175
176 <LI>
177 Syntax for accessing Bean properties and Hashtable entries</LI>
178 </UL>
179
180 <H3>
181 <A NAME="ScriptVariables"></A>Script Variables</H3>
182 The BeanShell allows you to create a special type of variable named a script
183 variable. Unlike a standard Java variable, which can reference objects
184 only of a specified type, a script variable can be defined to reference
185 any type of object, including primitive types, such as <TT>int</TT> and
186 <TT>boolean</TT>. You create a script variable by declaring it with or
187 without a type specifier. If you include a type specifier, the variable
188 can reference only values of the specified type. If you do not specify
189 a type, the variable can reference values of any type. For example, the
190 following statement
191 <PRE>&nbsp;&nbsp;&nbsp; foo = new Button("Another Button");</PRE>
192 creates an untyped script variable named <TT>foo</TT> and assigns it a
193 Button object. You are&nbsp; free to subsequently assign <TT>foo</TT> to
194 any other type of object.
195 <H4>
196 Predefined Variables</H4>
197
198 <UL>
199 <LI>
200 <B>$_</B> - the value of the last expression evaluated.</LI>
201
202 <LI>
203 <B>bsh.Console bsh.console</B> - The primary console, if one exists.</LI>
204
205 <LI>
206 <B>java.awt.AppletContext bsh.appletcontext</B> - the applet context, if
207 one exists.</LI>
208
209 <LI>
210 <B>String bsh.cwd</B> - used by the cd() and dir() commands.</LI>
211
212 <LI>
213 <B>boolean bsh.show</B> - used by the show() command.</LI>
214
215 <LI>
216 <B>boolean bsh.interactive</B> - is this interpreter running in an interactive
217 mode or sourcing a file?</LI>
218
219 <LI>
220 <B>boolean bsh.evalOnly</B> - Does this interpreter have an input stream
221 or is it only serving as a bag for eval() operations.</LI>
222 </UL>
223
224 <H4>
225 Undefined variables</H4>
226 You can test to see if a variable is "undefined" with the value <TT>void</TT>,
227 e.g.:
228 <PRE>&nbsp;&nbsp;&nbsp; if ( foobar == void )
229 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // undefined</PRE>
230 You can return a defined variable to the undefined state using the unset()
231 command:
232 <BLOCKQUOTE><TT>a == void;&nbsp; // true</TT>
233 <BR><TT>a=5;</TT>
234 <BR><TT>unset("a"); // note the quotes</TT>
235 <BR><TT>a == void;&nbsp; // true</TT></BLOCKQUOTE>
236
237 <H3>
238 <A NAME="ScriptMethods"></A>Script Methods</H3>
239 BeanShell lets you define and use a special type of method called a script
240 method. Script methods differ from standard Java methods in the following
241 ways:
242 <UL>
243 <LI>
244 Script methods are methods of an implicit, typeless object</LI>
245
246 <LI>
247 Script methods can be defined to accept and return values of any type</LI>
248
249 <LI>
250 Script methods can define other script methods</LI>
251 </UL>
252 You use standard Java syntax to declare a script&nbsp; method that accepts
253 and returns specific types. For example, the following code
254 <PRE>&nbsp;&nbsp;&nbsp; int addTwoNumbers( int a, int b ) {
255 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return a + b;
256 &nbsp;&nbsp;&nbsp; }</PRE>
257 defines a BeanShell method called <TT>addTwoNumbers</TT> that accepts and
258 returns values of type <TT>int</TT>. The next example
259 <PRE>&nbsp;&nbsp;&nbsp; int a = addTwoNumbers( 5, 7 );</PRE>
260 uses the newly defined method to add two values of type <TT>int</TT>.
261
262 <P>You define an untyped script method by omitting type specifications.
263 For example, the following statement
264 <PRE>&nbsp;&nbsp;&nbsp; add( a, b ) {
265 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return a + b;
266 &nbsp;&nbsp;&nbsp; }</PRE>
267 defines a script method that accepts arguments of any type. When you invoke
268 an untyped script method, BeanShell interprets the method based on the
269 types of the arguments that you pass to the method. Consider, for example,
270 the following invocations of the untyped add method defined in the preceding
271 example:
272 <PRE>&nbsp;&nbsp;&nbsp; foo = add(1, 2);
273 &nbsp;&nbsp;&nbsp; print( foo ); // 3
274
275 &nbsp;&nbsp;&nbsp; foo = add("Oh", " baby");
276 &nbsp;&nbsp;&nbsp; print( foo ); // Oh baby</PRE>
277 The first invocation returns the result of adding, the second, of concatenating
278 the arguments.
279
280 <P>Methods with unspecified return types may return any type of object
281 or no object. A return statement is optional. If omitted, the method returns
282 the value of the last statement or expression in the method body.
283 <H4>
284 Method Namespace</H4>
285 The namespaces of script methods and variables are separate. Thus, you
286 can define a method and a variable having the same name.
287 <H4>
288 Nested Methods</H4>
289 Script methods may define methods, for example,
290 <PRE>&nbsp;&nbsp;&nbsp; foo() {
291 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; bar() {
292 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ...
293 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }
294 &nbsp;&nbsp;&nbsp; }</PRE>
295 Method may be nested in this way to an arbitrary depth. Within a nested
296 method, locally declared variables and methods&nbsp; shadow identically
297 named variables and methods declared in outer methods. Otherwise, variables
298 and methods are visible to an arbitrary depth of scope. Outer methods can
299 invoke methods defined by inner methods that return a <TT>this</TT> object.
300
301
302 <H3>
303 <A NAME="ImplicitObjects"></A>Implicit Objects</H3>
304 The methods and variables defined by a script method are considered to
305 be methods and fields of an implicit object. The reserved identifiers,
306 <TT>this</TT>, <TT>super</TT>, and <TT>global</TT>, refer, respectively,
307 to the current object, the calling object, and the global object. A method
308 can access any variable or method in these scopes by qualifying the variable's
309 name with the name of the appropriate implicit object.
310 <PRE>&nbsp;&nbsp;&nbsp; a = 42;
311 &nbsp;&nbsp;&nbsp; foo() {
312 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; a = 97;
313 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print( a );
314 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print( this.a );
315 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print( super.a );
316 &nbsp;&nbsp;&nbsp; }
317
318 &nbsp;&nbsp;&nbsp; foo();&nbsp; // prints 97, 97, 42</PRE>
319 A script method can return its implicit object, thereby allowing the invoking
320 script to access variables and methods defined by the method, using standard
321 Java&nbsp; "." notation. For example,
322 <PRE>&nbsp;&nbsp;&nbsp; foo() {
323 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int a = 42;
324 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; bar() {
325 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print("The bar is open!");
326 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }
327 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
328 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; bar();
329 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return this;
330 &nbsp;&nbsp;&nbsp; }
331
332 &nbsp;&nbsp;&nbsp; obj = foo();&nbsp;&nbsp;&nbsp;&nbsp; // prints "the bar is open!"
333 &nbsp;&nbsp;&nbsp; print ( obj.a )&nbsp; // 42
334 &nbsp;&nbsp;&nbsp; obj.bar();&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // prints "the bar is open!"</PRE>
335
336 <H4>
337 Using Implicit Objects as AWT Event Handlers</H4>
338 Implicit method objects can serve as AWT event handlers. To handle an AWT
339 event, a script method defines the appropriate event-handling method and
340 then registering its implicit (<TT>this</TT>) object with the object in
341 which the event originates. For example, the following script
342 <PRE>&nbsp;&nbsp;&nbsp; button = new java.awt.Button("foo!");
343
344 &nbsp;&nbsp;&nbsp; actionPerformed( event ) {
345 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print( event );
346 &nbsp;&nbsp;&nbsp; }
347
348 &nbsp;&nbsp;&nbsp; button.addActionListener( this );
349 &nbsp;&nbsp;&nbsp; frame( button );&nbsp; // show it</PRE>
350 defines an Action event handler and registers it with a button.
351
352 <P>Remember that you don't have to define all of your event handlers globally.
353 You can handle events in any bsh object scope. For example, the following
354 method creates a button that displays a message when pushed:
355 <PRE>&nbsp;&nbsp;&nbsp; messageButton( message ) {
356 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; b = new Button("Press Me");
357 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; b.addActionListener( this );
358 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; frame(b);
359 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
360 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; actionPerformed( e ) {
361 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print( message );
362 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }
363 &nbsp;&nbsp;&nbsp; }
364
365 &nbsp;&nbsp;&nbsp; messageButton("Hey you!");
366 &nbsp;&nbsp;&nbsp; messageButton("Another message...");</PRE>
367 The above will create two buttons and each will display its own message
368 when pushed. Each has a separate instance of the event handler object.
369 Note too that we could return a 'this' reference from the handler method
370 and use it in other contexts to register listeners...
371 <H4>
372 Using Implicit Objects as Threads</H4>
373 'This' type references also implement the standard Runnable interface,
374 so you can declare a "run()" method in your objects:
375 <BLOCKQUOTE>&nbsp;<TT>&nbsp;&nbsp; foo() {</TT>
376 <BR><TT>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; run() {</TT>
377 <BR><TT>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
378 // do work...</TT>
379 <BR><TT>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</TT>
380 <BR><TT>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return this;</TT>
381
382 <P><TT>&nbsp;&nbsp;&nbsp; }</TT>
383 <BR><TT>&nbsp;</TT>
384 <BR><TT>&nbsp;&nbsp;&nbsp; foo = foo();</TT>
385 <BR><TT>&nbsp;&nbsp;&nbsp; new Thread( foo ).start();</TT>
386 <BR>&nbsp;</BLOCKQUOTE>
387
388 <H4>
389 Implicit Object Members</H4>
390 Implicit objects have four "magic" members:
391 <BLOCKQUOTE>
392 <DIR>
393 <LI>
394 <TT>this.interpreter</TT> refers to the currently executing BeanShell Interpreter
395 object.</LI>
396
397 <LI>
398 <TT>this.namespace</TT> refers to the BeanShell NameSpace object of the
399 current context.</LI>
400
401 <LI>
402 <TT>this.variables</TT> refers to an array of strings listing the variables
403 defined in this namespace.</LI>
404
405 <LI>
406 <TT>this.methods</TT> refers to an array of strings listing the methods
407 defined in this namespace.</LI>
408 </DIR>
409 </BLOCKQUOTE>
410 These are mainly for internal use by BeanShell commands. Note that there
411 are certain special situations in which the <TT>this.interpreter</TT> reference
412 may not be available, such as in AWT event handlers.
413 <H3>
414 <A NAME="PropSyntax"></A>Extended Syntax for Accessing Bean Properties
415 and Hashtables Entries</H3>
416 You may use the following syntax
417 <BLOCKQUOTE><TT>x{name}</TT></BLOCKQUOTE>
418 to access properties of Java beans and Hashtable entries, where <TT>x</TT>
419 is a bean or Hashtable and <TT>name</TT> is a String that identifies a
420 bean property or hashtable entry, for example:
421 <PRE>&nbsp;&nbsp;&nbsp; b = new java.awt.Button();
422 &nbsp;&nbsp;&nbsp; b{"label"} = "my button";
423 &nbsp;&nbsp;&nbsp; // Equivalent to: b.setLabel("my button");
424
425 &nbsp;&nbsp;&nbsp; h = new Hashtable();
426 &nbsp;&nbsp;&nbsp; h{"foo"} = "bar";
427 &nbsp;&nbsp;&nbsp; // Equivalent to: h.put("foo", "bar");</PRE>
428
429
430 <h2><a name="ImplementingInterfaces">Implementing Interfaces</a></h2>
431
432 <p><strong>Note</strong> Implementing arbitrary interfaces requires BeanShell be running under a Java 1.3 environment or higher.</p>
433
434 <p>You can use the standard Java anonymous inner class syntax to implement an interface type with a script. For example:</p> 
435
436
437 <pre>
438   ActionListener scriptedListener = new ActionListener() {
439     actionPerformed( event ) { ... }
440   }
441 </pre>
442  
443
444 <p>You don't have to implement all of the methods defined by an interface.
445 The calling code throws an exception if it tries to invoke a method
446 that isn't defined. If you wish to override the behavior of a large
447 number of methods - say to produce a "dummy" adapter for logging - you
448 can implement a special method signature: <code>invoke(name, args)</code> in your
449 scripted object. The <code>invoke()</code> method is called to handle any undefined
450 method invocations: </p>
451
452 <pre>
453   ml = new MouseListener() {
454     mousePressed( event ) { ... }
455     // handle the rest
456     invoke( name, args ) { print("Method: "+name+" invoked!");
457   }
458 </pre>
459  
460
461 <h2><a name="ClassPathManagement">Class Loading and Class Path Management</a></h2>
462
463
464 <p>BeanShell is capable of some very fine grained and sophisticated class 
465 reloading and modifications to the class path.  BeanShell can even map
466 the entire class path to allow for automatic importing of classes.</p>
467
468
469 <h3><a name="Changing_the_Class_Path">Changing the Class Path</a></h3>
470
471 <strong>addClassPath( URL | path )</strong>
472
473 <p CLEAR="ALL"/>
474 Add the specified directory or archive to the classpath.  Archives may be
475 located by URL, allowing them to be loaded over the network.
476 <p CLEAR="ALL"/>
477
478 Examples:
479 <p CLEAR="ALL"/>
480 <p/><center><table border="1" cellpadding="5" width="100%"><tr><td bgcolor="#dfdfdc">
481   <pre>
482 addClassPath( "/home/pat/java/classes" );
483 addClassPath( "/home/pat/java/mystuff.jar" );
484 addClassPath( new URL("http://myserver/~pat/somebeans.jar") );
485 </pre>
486 </td></tr></table></center><p/>
487
488 Note that if you add class path that overlaps with the existing Java user 
489 classpath then the new path will effectively reload the classes in that
490 area.
491 <p CLEAR="ALL"/>
492
493 If you add a relative path to the classpath it is evaluated to an absolute
494 path; it does not "move with you".
495
496 <p/><center><table border="1" cellpadding="5" width="100%"><tr><td bgcolor="#dfdfdc"><pre>
497 cd("/tmp");
498 addClassPath("."); // /tmp
499 </pre></td></tr></table></center><p/>
500
501 <strong>setClassPath( URL [] )</strong>
502 <p CLEAR="ALL"/>
503 Change the entire classpath to the specified array of directories and/or 
504 archives.  
505 <p CLEAR="ALL"/>
506
507 This command has some important side effects.  It effectively causes all
508 classes to be reloaded (including any in the Java user class path at startup).
509 Please see "Class Reloading" below for further details.
510 <p CLEAR="ALL"/>
511
512 Note: setClassPath() cannot currently be used to make the classpath smaller 
513 than the Java user path at startup.
514 <p CLEAR="ALL"/>
515
516
517 <h3><a name="Auto-Importing_from_the_Classpath">Auto-Importing from the Classpath</a></h3>
518
519 As an alternative to explicitly importing class names you may use the
520 following statement to trigger automatic importing:
521
522 <p/><center><table border="1" cellpadding="5" width="100%"><tr><td bgcolor="#dfdfdc"><pre>
523 import *;
524 </pre></td></tr></table></center><p/>
525
526 There may be a significant delay while the class path is mapped.  This is why
527 auto-importing is not turned on by default.  When run interactively, Bsh will 
528 report the areas that it is mapping.
529 <p CLEAR="ALL"/>
530
531 It is only necessary to issue the auto-import command once.  Thereafter changes
532 in the classpath via the addClassPath() and setClassPath() commands will
533 remap as necessary.
534 <p CLEAR="ALL"/>
535
536 <em>
537 Note: As of BeanShell 1.1alpha new class files added to the classpath 
538 (from outside of BeanShell) after mapping will not be seen in imports.
539 </em>
540
541
542 <h3><a name="Reloading_Classes">Reloading Classes</a></h3>
543
544 BeanShell provides an easy to use mechanism for reloading classes from the 
545 classpath.  It is possible in BeanShell to reload arbitrary subsets of classes 
546 down to a single class file.  However There are subtle issues to be understood 
547 with respect to what it means to reload a class in the Java environment. 
548 Please see the discussion of class loading detail below.  But in a nutshell,
549 it is important that classes which work together be reloaded together at
550 the same time, unless you know what you are doing.
551 <p CLEAR="ALL"/>
552
553 <strong>reloadClasses( [ package name ] )</strong>
554 <p CLEAR="ALL"/>
555 The most course level of class reloading is accomplished by issuing the 
556 reloadClasses() command with no arguments.  
557
558 <p/><center><table border="1" cellpadding="5" width="100%"><tr><td bgcolor="#dfdfdc"><pre>
559 reloadClasses();
560 </pre></td></tr></table></center><p/>
561
562 This will effectively reload all
563 classes in the current classpath (including any changes you have made through
564 addClassPath()).
565 <p CLEAR="ALL"/>
566
567 <em>
568 Note: that reloading the full path is actually a light weight operation that 
569 simply replaces the class loader - normal style class loading is done as
570 classes are subsequently referenced.
571 </em>
572 <p CLEAR="ALL"/>
573
574 Be aware that any object instances which you have previously created may not 
575 function with new objects created by the new class loader.  Please see the
576 discussion of class loading details below.
577 <p CLEAR="ALL"/>
578
579 You can also reload all of the classes in a specified package:
580
581 <p/><center><table border="1" cellpadding="5" width="100%"><tr><td bgcolor="#dfdfdc"><pre>
582 reloadClasses("mypackage.*");
583 </pre></td></tr></table></center><p/>
584
585 This will reload only the classes in the specified package.  The classes will
586 be reloaded even if they are located in different places in the classpath (e.g.
587 if you have some of the package in one directory and some in another).
588 <p CLEAR="ALL"/>
589
590 As a special case for reloading unpackaged classes the following commands
591 are equivalent:
592 <p/><center><table border="1" cellpadding="5" width="100%"><tr><td bgcolor="#dfdfdc"><pre>
593 reloadClasses(".*") 
594 reloadClasses("&lt;unpackaged&gt;")
595 </pre></td></tr></table></center><p/>
596 <p CLEAR="ALL"/>
597
598 You can also reload just an individual class file:
599 <p/><center><table border="1" cellpadding="5" width="100%"><tr><td bgcolor="#dfdfdc"><pre>
600 reloadClasses("mypackage.MyClass") 
601 </pre></td></tr></table></center><p/>
602
603 <em>Note: As of alpha1.1 classes contained in archives (jar files) cannot be
604 reloaded. i.e. jar files cannot be swapped.</em>
605 <p CLEAR="ALL"/>
606
607 <h3>Mapping the path</h3>
608
609 Unlike the reloadClases() command which reloads the entire class path,
610 when you issue a command to reload a package or individual class name
611 BeanShell must map some portions of the classpath to find the location of 
612 those class files.  This operation can be time consuming, but it is only
613 done once.  If running in interactive mode feedback will be given on the
614 progress of the mapping.
615 <p CLEAR="ALL"/>
616
617 <h3><a name="Loading_Classes_Explicitly">Loading Classes Explicitly</a></h3>
618
619 In order to perform an explicit class lookup by name while taking into 
620 account any BeanShell class path modification you must use a replacement
621 for the standard Class.forName() method.
622 <p CLEAR="ALL"/>
623
624 The getClass() command will load a class by name, using the BeanShell
625 classpath.  Alternately, you can consult the class manager explicitly:
626
627 <p/><center><table border="1" cellpadding="5" width="100%"><tr><td bgcolor="#dfdfdc"><pre>
628 name="foo.bar.MyClass";
629 c = getClass( name );
630 c = BshClassManager.classForName( name );  // equivalent
631 </pre></td></tr></table></center><p/>
632
633 <h3><a name="Setting_the_Default_ClassLoader">Setting the Default ClassLoader</a></h3>
634
635 The bsh.Interpeter setClassLoader() and bsh.BshClassManager.setClassLoader()
636 methods can be used to set an external class loader which is consulted
637 for all basic class loading in BeanShell.  
638 <p CLEAR="ALL"/>
639
640 BeanShell will use the specified class loader at the same point where it 
641 would otherwise use the plain Class.forName().  If no explicit classpath 
642 management is done from the script (addClassPath(), setClassPath(), 
643 reloadClasses()) then BeanShell will only use the supplied classloader.  
644 If additional classpath management is done then BeanShell will perform that 
645 in addition to the supplied external classloader.  However BeanShell is not 
646 currently able to reload classes supplied through the external classloader.  
647 <p CLEAR="ALL"/>
648
649
650 <h3><a name="Class_Loading_in_Java">Class Loading in Java</a></h3>
651
652 A fundamental Java security proposition is that classes may only be loaded 
653 through a class loader once and that classes loaded through different class
654 loaders live in different name spaces.  By different name spaces I mean that
655 they are not considered to be of the same type, even if they came from the
656 very same class file.
657 <p CLEAR="ALL"/>
658 You can think of this in the following way:  When you load classes through
659 a new class loader imagine that every class name is prefixed with the 
660 identifier "FromClassLoaderXXX" and that all internal references to other
661 classes loaded through that class loader are similarly rewritten.  Now if
662 you attempt to pass a reference to a class instance loaded through another
663 class loader to one of your newly loaded objects, it will not recognize it
664 as the same type of class.
665 <p CLEAR="ALL"/>
666
667 BeanShell works with objects dynamically through the reflection API, so
668 your scripts will not have a problem recognizing reloaded class objects.
669 However any objects which have you already created might not like them.
670
671
672 <h3><a name="Class_Loading_in_BeanShell">Class Loading in BeanShell</a></h3>
673
674 The following is a discussion of the BeanShell class loader architecture,
675 which allows both course class path extension and fine grained individual
676 class reloading.
677 <p CLEAR="ALL"/>
678
679 <strong>Thriftiness</strong> - Abiding by the BeanShell thriftiness 
680 proposition: no class loading code is exercised unless directed by a
681 command.  BeanShell begins with no class loader and only adds class loading
682 in layers as necessary to achieve desired effects.
683 <p CLEAR="ALL"/>
684
685 The following diagram illustrates the two layer class loading scheme:
686 <p CLEAR="ALL"/>
687 <center>
688 <img src="images/bshclassloading.gif"/>
689 </center>
690 <p CLEAR="ALL"/>
691
692 A "base" class loader is used to handle course changes to the classpath 
693 including added path.  Unless directed by setClassPath() the base loader will
694 only add path and will not cover existing Java user class path.  This prevents
695 unnecessary class space changes for the existing classes.
696 <p CLEAR="ALL"/>
697
698 Packages of classes and individual classes are mapped in sets by class 
699 loaders capable of handling discrete files.  A mapping of reloaded classes
700 is maintained.  The discrete file class loaders will also use this mapping
701 to resolve names outside there space, so when any individual class is reloaded
702 it will see all previously reloaded classes as well.
703 <p CLEAR="ALL"/>
704
705 The BshClassManager knows about all class loader changes and broadcasts
706 notification of changes to registered listeners.  BeanShell namespaces use
707 this mechanism to dereference cached type information, however they do not
708 remove existing object instances.
709 <p CLEAR="ALL"/>
710
711 Type caching is extremely important to BeanShell performance.  So changing
712 the classloader, which necessitates clearing all type caches, should be 
713 considered an expensive operation. 
714
715
716 <H2>
717 <A NAME="CommandRef"></A>BeanShell Commands</H2>
718 The BeanShell provides a set of commands for displaying data, invoking
719 system utilities, and performing various other tasks. See the BeanShell
720 Command Reference for a description of the syntax and usage of each command.
721 The current crop of bsh commands follow. These are, for the most part,
722 just very short bsh scripts, supplied in the bsh.jar file. See <A HREF="#making">making
723 bsh commands</A> below for more details on adding to the "built-in" bsh
724 command set.
725 <BR>&nbsp;
726 <BR>
727 <HR>
728 <H3><A NAME="addClasspathCommand"></A>addClassPath</H3>
729 <code>void addClassPath( string | URL )</code>
730
731
732 <p>Add the specified directory or JAR file to the class path. e.g.</p> 
733
734 <pre>
735     addClassPath( "/home/pat/java/classes" );
736     addClassPath( "/home/pat/java/mystuff.jar" );
737     addClassPath( new URL("http://myserver/~pat/somebeans.jar") );
738 </pre>
739 <BR>
740 <HR>
741
742 <H3><A NAME="bgCommand"></A>bg</H3>
743 <TT>bg( String script )</TT>
744
745 <P>This is like <TT>run()</TT> except that it runs the command in its own
746 thread. Returns the thread object (for <TT>stop()</TT>ing, <TT>join()</TT>ing,
747 etc.)</p>
748 <BR>
749 <HR>
750
751 <H3><A NAME="bindCommand"></A>bind</H3>
752 <code>bind ( bsh .This ths , bsh .NameSpace namespace )</code>
753
754 <P>Bind a bsh object into a particular namespace and interpreter.</p>
755 <BR>
756 <HR>
757
758
759 <H3>
760 <A NAME="BrowseClassCommand"></A>browseClass</H3>
761 <code>void browseClass( String | Object | Class ) </code>
762
763 <p>Open the class browser to view the specified class. If the argument is a string it is considered to be a class name. If the argument is an object, the class of the object is used. If the arg is a class, the class is used. 
764 Note: To browse the String class you can't supply a String. You'd have to do: </p>
765
766 <pre>
767     browseClass( String.class
768 </pre>
769
770 <P>The browser enables you to browse the contents
771 of any packages packaged as <code>jar</code> files on the classpath defined
772 by <code>jde-global-classpath</code>.
773 <BR>&nbsp;
774 <BR>
775 <HR>
776
777 <H3> <A NAME="catCommand"></A>cat</H3>
778 <code>cat ( String filename )</code> <br>
779 <code>cat ( URL url )</code> <br>
780 <code>cat ( InputStream ins )</code> <br>
781 <code>cat ( Reader reader )</code> <br>
782
783 <P>Print the contents of filename, URL, or stream (like Unix cat)l
784 <BR>
785 <HR>
786
787 <H3><A NAME="cdCommand"></A>cd</H3>
788 <code>void cd(String path);</code>
789
790 <P>Change working directory for the <TT>dir()</TT> command (like Unix <TT>cd</TT>).</p>
791 <BR>
792 <BR>
793 <HR>
794
795 <H3><A NAME="classBrowserCommand"></A>classBrowser</H3>
796 <code>void classBrowser ( )</code>
797 <P>Open the class browser.</p>
798
799 <BR>
800 <BR>
801 <HR>
802
803 <H3><A NAME="clearCommand"></A>clear</H3>
804 <code>clear()</code>
805
806 <P>Clear all variables, methods, and imports from this namespace. If
807 this namespace is the root, it will be reset to the default
808 imports. See NameSpace.clear(); </p>
809 <BR>
810 <BR>
811 <HR>
812
813
814
815 <H3><A NAME="consoleCommand"></A>console</H3>
816 <TT>bsh.Console console()</TT>
817
818 <P>Create a console window attached to the current interpreter. Returns
819 the console Frame.
820 <BR>
821 <HR>
822
823 <H3><A NAME="dirCommand"></A>dir</H3>
824 <TT>void dir(String dirname)</TT>
825
826 <P>Display the contets of directory dirname. The format is similar to the
827 Unix ls -l command.</p>
828 <BR>
829 <HR>
830
831 <H3><A NAME="debugCommand"></A>debug</H3>
832 <TT>void debug()</TT>
833
834 <P>Toggle on and off debug mode... Note: debug output is verbose and gross.</p>
835 <BR>
836 <HR>
837
838 <H3><A NAME="EditorCommand"></A>editor</H3>
839
840 <code>Frame Frame editor()</code> 
841
842 <P>Create an editor window with an "eval" button. This is primarily useful
843 for typing multi-line commands and experimenting with methods when running
844 the BeanShell outside of the Emacs environment. Returns the editor Frame.
845 <BR>
846 <HR>
847
848 <H3><A NAME="errorCommand"></A>error</H3>
849
850 <code>void error(item)</code> 
851
852 <p>Print the item as an error to standard error.</p>
853
854 <BR>
855 <HR>
856
857 <H3><A NAME="evalCommand"></A>eval</H3>
858 <code>Object eval(String expression)</code>
859
860
861 <p>Evaluate the string in the current interpreter (see source()). Returns
862 the result of the evaluation or null.  Evaluate a string as if it were
863 written directly in the current scope, with side effects in the
864 current scope.
865 </p>
866
867 <p>e.g.</p>
868
869 <pre>    a=5;
870     eval("b=a*2");
871     print(b); // 10
872 </pre>
873
874 <p><code>eval()</code> acts just like invoked text except that any
875 exceptions generated by the code are captured in a <code>bsh.EvalError</code>. This
876 includes <code>ParseException</code> for syntactic errors and <code>TargetError</code> for
877 exceptions thrown by the evaluated code.</p>
878
879 <p>e.g.</p> 
880
881 <pre>
882     try {
883         eval("foo>>><>M>JK$LJLK$");
884     } catch ( EvalError e ) {
885         // ParseException caught here
886     }
887
888     try {
889         eval("(Integer)true");  // illegal cast
890     } catch ( EvalError e ) {
891         // TargetException caught here
892         print( e.getTarget() )  // prints ClassCastException
893     }
894 </pre>
895
896 <p>If you want <code>eval()</code> to throw target exceptions directly, without
897 wrapping them, you can simply redefine own eval like so:</p>
898
899 <pre>
900     myEval( String expression ) {
901         try {
902             return eval( expression );
903         } catch ( TargetError e ) {
904             throw e.getTarget();
905         }
906     }
907  </pre>
908
909 <p>Returns the value of the expression.</p> 
910
911 <p>Throws <code>bsh.EvalError</code> on error.</p> 
912
913 <BR>
914 <HR>
915
916 <H3><A NAME="execCommand"></A><B>exec</B></H3>
917
918 <code>exec(String process)</code>
919
920
921 <p> Start an external application using the Java Runtime
922 <code>exec()</code> method. Display any output to the standard
923 BeanShell output using <code>print()</code> </p>
924
925 <BR>
926 <HR>
927
928 <H3><A NAME="exitCommand"></A>exit</H3>
929 <TT>void exit()</TT>
930
931
932 <p>Conditionally exit the virtual machine. Call
933 <code>System.exit(0)</code> unless <code>bsh.system.shutdownOnExit ==
934 false</code>.</p>
935
936 <BR>
937 <HR>
938
939 <H3><A NAME="extendCommand"></A>extend</H3>
940 <TT>This extend(This object)</TT>
941
942 <p>Return a new object that is a child of the specified object. </p>
943
944 <p><strong>Note</strong> This command will likely change along with a
945 better inheritance mechanism for bsh in a future release.,</p>
946
947 <p><code>extend()</code> is like the <code>object()</code> command,
948 which creates a new bsh scripted object, except that the namespace of
949 the new object is a child of the parent object.</p>
950
951 <p>For example:</p>
952
953 <pre>
954     foo=object();
955     bar=extend(foo);
956
957     is equivalent to:
958       
959     foo() { 
960         bar() {
961             return this; 
962         }
963     }
964
965     foo=foo();
966     bar=foo.bar();
967
968     and also:
969      
970     oo=object();
971     ar=object();
972     ar.namespace.bind( foo.namespace );
973     
974 </pre>
975
976 <p>The last example above is exactly what the extend() command
977 does. In each case the bar object inherits variables from foo in the
978 usual way.</p>
979 <BR>
980 <HR>
981
982
983 <H3><A NAME="FrameCommand"></A>frame</H3>
984
985 <p><code>Frame | JFrame | JInternalFrame frame(Component component);</code></p>
986
987
988 <p>Display the component, centered and packed, in a Frame, JFrame, or
989 JInternalFrame. Returns the frame. If the GUI desktop is running then
990 a JInternaFrame will be used and automatically added to the
991 desktop. Otherwise if Swing is available a top level JFrame will be
992 created. Otherwise a plain AWT Frame will be created.</p>
993
994 <BR>
995 <HR>
996
997 <H3><A NAME="getClassCommand"></A>getClass</H3>
998 <p><code>Class getClass(String name)</code></p>
999
1000
1001 <p>Get a class through the current namespace utilizing the current
1002 imports, extended classloader, etc. </p>
1003
1004 <p>This is equivalent to the standard <code>Class.forName()</code>
1005 method for class loading. However, it takes advantage of the BeanShell
1006 class manager so that added classpath will be taken into account. You
1007 can also use <code>Class.forName()</code>. However if you have modified the
1008 classpath or reloaded classes from within your script the
1009 modifications will only appear if you use the <code>getClass()</code> command.
1010 </p>
1011 <BR>
1012 <HR>
1013
1014
1015 <H3><A NAME="getClassPathCommand"></A>getClassPath</H3>
1016 <p><code>URL[] getClassPath()</code></p>
1017
1018 <p>Get the current classpath including all user path, extended path,
1019 and the bootstrap JAR file if possible. </p>
1020
1021 <BR>
1022 <HR>
1023
1024
1025
1026 <H3><A NAME="getResourceCommand">getResource</A></H3>
1027 <p><code>URL getResource(String path)</code></p>
1028
1029 <p>The equivalent of calling <code>getResource()</code> on the
1030 interpreter class in the bsh package. Use absolute paths to get stuff
1031 in the classpath.</p>
1032 <BR>
1033 <HR>
1034
1035 <H3><A NAME="getSourceFileInfoCommand">getSourceFileInfo</A></H3>
1036 <p><code>getSourceFileInfo() </code></p>
1037
1038
1039 <p>Return the name of the file or source from which the current
1040 interpreter is reading. Note that if you use this within a method, the
1041 result will not be the file from which the method was sourced, but
1042 will be the file that the caller of the method is reading. Methods are
1043 sourced once but can be called many times. Each time the interpreter
1044 may be associated with a different file and it is that calling
1045 interpreter that you are asking for information.</p>
1046
1047 <p><strong>Note</strong> Although it may seems like this command would
1048 always return the <code>getSourceFileInfo.bsh</code> file, it does not
1049 since it is being executed after sourcing by the caller's
1050 interpreter. If one wanted to know the file from which a bsh method
1051 was sourced one would have to either capture that info when the file
1052 was sourced (by saving the state of the
1053 <code>getSourceFileInfo()</code> in a variable outside of the method
1054 or more generally we could add the info to the BshMethod class so that
1055 bsh methods remember from what source they were created.</p>
1056
1057 <BR>
1058 <HR>
1059
1060 <H3><A NAME="javapCommand">javap</A></H3>
1061
1062 <p><code>void javap(String | Object | Class)</code></p>
1063
1064 <p>Print the public fields and methods of the specified class (output
1065 similar to the JDK javap command). </p>
1066
1067 <p>If the argument is a string it is considered to be a class name. If
1068 the argument is an object, the class of the object is used. If the arg
1069 is a class, the class is used.</p>
1070 <BR>
1071 <HR>
1072
1073 <H3><A NAME="LoadCommand">load</A></H3>
1074
1075 <p><code>Object load(String filename)</code></p>
1076
1077 <p>Load a serialized Java object from filename. Returns the object.</p>
1078
1079 <BR>
1080 <HR>
1081
1082 <H3><A NAME="MakeWorkspaceCommand">makeWorkspace</A></H3>
1083
1084 <p><code>makeWorkspace(String name)</code></p>
1085
1086 <p>Open a new workspace (JConsole) in the GUI desktop. </p>
1087 <BR>
1088 <HR>
1089
1090 <h3><a  name="mvCommand">mv</a></h3>
1091 <p><code>mv (String fromFile, String toFile)</code></p> 
1092  
1093 <p>Rename a file (like Unix <code>mv</code>).</p>  
1094 <BR>
1095 <HR>
1096
1097 <h3><a  name="objectCommand">object</a></h3>
1098 <p><code>This object()</code></p>
1099  
1100 <p>Return an "empty" BeanShell object context which can be used to hold data items. e.g.</p>
1101
1102 <pre>
1103     myStuff = object();
1104     myStuff.foo = 42;
1105     myStuff.bar = "blah";
1106 </pre>
1107 <BR>
1108 <HR>
1109  
1110
1111
1112 <H3>
1113 <A NAME="pathToFileCommand">pathToFile</A></H3>
1114 <p><code>File pathToFile(String filename)</code></p>
1115
1116 <p>Create a File object corresponding to the specified file path name,
1117 taking into account the bsh current working directory (bsh.cwd)</p>
1118 <BR>
1119 <HR>
1120
1121 <H3>
1122 <A NAME="PrintCommand">print</A></H3>
1123 <p><code>void print(item);</code></p>
1124
1125 <p>Print the string value of the argument, which may be of any type. If beanshell is running interactively, the output will always go to the command line, otherwise it will go to System.out.</p>
1126
1127 <p>Most often the printed value of an object will simply be the Java toString() of the object. However if the argument is an array the contents of the array will be (recursively) listed in a verbose way.</p> 
1128
1129 <p>Note that you are always free to use <code>System.out.println()</code>
1130 instead of <code>print()</code>.</p> 
1131
1132 <BR>
1133 <HR>
1134 <H3><A NAME="pwdCommand">pwd</A></H3>
1135
1136 <DL>
1137 <DT>
1138 <TT>void pwd();</TT></DT>
1139 </DL>
1140
1141
1142 <P>Print the bsh working directory. This is the cwd obeyed by all the unix
1143 like bsh comands.
1144 <BR>
1145 <HR>
1146
1147 <h3><a  name="reloadClassesCommand">reloadClasses</a></h3>
1148 <p><code>void reloadClasses([package name])</code></p> 
1149  
1150 <p>Reload the specified class, package name, or all classes if no name is given. e.g. 
1151 </p>
1152 <pre>
1153     reloadClasses();
1154     reloadClasses("mypackage.*");
1155     reloadClasses(".*")  // reload unpackaged classes
1156     reloadClasses("mypackage.MyClass") 
1157 </pre>
1158
1159 <p>See "Class Path Management" </p>
1160 <BR>
1161 <HR>
1162  
1163
1164
1165 <H3>
1166 <A NAME="rmCommand">rm</A></H3>
1167 <p><code>void rm(String pathname);</code></p>
1168
1169 <P>Remove the file (like Unix rm)
1170 <BR>
1171 <HR>
1172
1173 <H3>
1174 <A NAME="runCommand">run</A></H3>
1175 <p><code>run(String filename, Object runArgument);</code></p>
1176 <p><code>run(String filename);</code></p> 
1177
1178 <p>Run a command in its own private global namespace and interpeter
1179 context (kind of like the unix <code>chroot</code> for the namespace).
1180 The root bsh system object is extended (with the <code>extend()</code>
1181 command) and made visible here, so that system info is effectively
1182 inherited. Because the root bsh object is extended, it is effectively
1183 read / copy on write, e.g. you can change directories in the child
1184 context, do imports, etc. and it will not affect the calling
1185 context.</p>
1186
1187 <p>Parameter <code>runArgument</code> an argument passed to the child
1188 context under the name runArgument, e.g. you might pass in the calling
1189 <code>this</code> context from which to draw variables, etc. </p>
1190 <BR>
1191 <HR>
1192
1193
1194
1195 <H3><A NAME="SaveCommand">save</A></H3>
1196 <p><code>void save(Component component, String filename);</code></p>
1197
1198 <p>Save a serializable Java object to filename.</p>
1199
1200 <p>Since the AWT Button class happens to be serializable, we could test
1201 drive the <code>save()</code> command.</p>
1202
1203 <PRE>
1204    save(foo, "myButton.ser");
1205 </PRE>
1206
1207 <p>If we did save our button, we could revive it later with the
1208 <code>load()</code> command.</p>
1209
1210 <pre>  bar = load("myButton.ser");
1211   frame(bar);
1212 </pre>
1213
1214 <BR>
1215 <HR>
1216
1217 <H3><A NAME="serverCommand">server</A></H3>
1218
1219 <p><code>void server(int port);</code></p>
1220
1221 <p>Create a <A HREF="file:///D|/jde-dev/doc/guide.html#server">Server Mode</A>
1222 server attached to the current interpreter, listening on the specified
1223 port.</p>
1224
1225 <BR>
1226 <HR>
1227
1228 <H3><A NAME="setAccessibilityCommand">setAccessibility</A></H3>
1229
1230 <p><code>setAccessibility(boolean b);</code></p>
1231
1232 <p>Setting accessibility on enables access to private and other
1233 non-public fields and methods.</p>
1234
1235 <BR>
1236 <HR>
1237
1238 <H3><A NAME="setClasspathCommand">setClassPath</A></H3>
1239
1240 <p><code>void setClassPath(URL[]);</code></p>
1241
1242 <p>Change the classpath to the specified array of directories and/or archives.</p>
1243
1244 See "Class Path Management" for details.
1245
1246
1247 <BR>
1248 <HR>
1249
1250
1251 <H3><A NAME="setFontCommand">setFont</A></H3>
1252 <p><code>Font setFont(Component comp, int ptsize)</code></p>
1253
1254 <p>Change the point size of the font on the specified component,
1255 to <code>ptsize</code>.</p>
1256 <BR>
1257 <HR>
1258
1259 <H3><A NAME="setNameSpaceCommand">setNameSpace</A></H3>
1260 <p><code>setNameSpace(ns)</code></p>
1261
1262
1263 <p>Set the namespace (context) of the current scope. 
1264 The following example illustrates swapping the current namespace. 
1265 </p>
1266
1267 <pre>
1268     fooState = object(); 
1269     barState = object(); 
1270     
1271     print(this.namespace);
1272     setNameSpace(fooState.namespace);
1273     print(this.namespace);
1274     a=5;
1275     setNameSpace(barState.namespace);
1276     print(this.namespace);
1277     a=6;
1278     
1279     setNameSpace(fooState.namespace);
1280     print(this.namespace);
1281     print(a);  // 5
1282     
1283     setNameSpace(barState.namespace);
1284     print(this.namespace);
1285     print(a); // 6
1286     
1287 </pre>
1288
1289 <p>You could use this to creates the effect of a static namespace for a method by explicitly setting the namespace upon entry.</p> 
1290
1291 <BR>
1292 <HR>
1293
1294 <H3><A NAME="setStrictJavaCommand">setStrictJava</A></H3>
1295 <p><code>void setStrictJava(boolean val)</code></p>
1296
1297
1298 <p> Enable or disable "Strict Java Mode". When strict Java mode is enabled BeanShell will: 
1299 </p>
1300
1301 <ol>
1302   <li><p>Require typed variable declarations, method arguments and return types.</p> 
1303   </li>
1304   <li>
1305     <p> Modify the scoping of variables to look for the variable
1306   declaration first in the parent namespace, as in a java method
1307   inside a java class. e.g. if you can write a method called
1308   incrementFoo() that will do the expected thing without referring to
1309   "super.foo".</p>
1310     <p>See "Strict Java Mode" for more details.</p> 
1311     </li>
1312 </ol>
1313
1314 <p><strong>Note</strong> Currently most standard BeanShell commands will not work in Strict Java mode simply because they have not been written with full types, etc. 
1315 </p> 
1316  
1317
1318
1319 <H3>
1320 <A NAME="ShowCommand"></A>show</H3>
1321 <TT>void show();</TT>
1322
1323 <P>Toggle on or off the display of the value of expressions evalauted on
1324 the command line. Show mode may be useful if you find yourself typing print()
1325 a lot.
1326 <BR>
1327 <HR>
1328
1329
1330 <H3>
1331 <A NAME="sourceCommand"></A>source</H3>
1332 <p><code>Object source(String filename)</code></p> 
1333 <p><code>Object source(URL url)</code></p> 
1334
1335 <p>Read filename into the interpreter and evaluate it in the current namespace.
1336 Like Bourne Shell "." command.
1337 </p>
1338
1339 <BR>
1340 <HR>
1341
1342 <H3>
1343 <A NAME="unsetCommand"></A>unset</H3>
1344 <TT>void unset(String name);</TT>
1345
1346 <P>"undefine" the variable specified by 'name' (So that it tests == void).
1347
1348 <p><strong>Note</strong> There will be a better way to do this in the future. This is currently equivalent to doing</p>
1349
1350 <pre>
1351   namespace.setVariable(name, null); 
1352 </pre>
1353 <BR>
1354 <HR>
1355
1356 <H3>
1357 <A NAME="whichCommand">which</A></H3>
1358
1359 <p><code>which( classIdentifier | string | class )</code></p> 
1360  
1361 <p>Use classpath mapping to determine the source of the specified class file. (Like the Unix which command for executables).  
1362 </p>
1363
1364 <BR>
1365 <HR>
1366
1367 <H2>
1368 <A NAME="MakeCommands"></A>Making BeanShell Commands</H2>
1369 Adding to the set of "prefab" commands supplied with bsh can be about as
1370 easy as writing any other bsh methods. You simply have to place your bsh
1371 scripts in a bsh/commands/ directory in the classpath (or inside the JAR
1372 file).
1373
1374 <P>&nbsp;
1375 <BR>&nbsp;
1376 </BODY>
1377 </HTML>