-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathcallscript.html
63 lines (56 loc) · 1.58 KB
/
callscript.html
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
<html>
<head><title>BeanShell Example - Calling scripts from Java</title></head>
<body bgcolor="#ffffff">
<a href="../home.html"><img border=0 src="../images/homebutton.gif"><br>Home</a>
<p>
<h1>Calling scripts from Java</h1>
Create an interpreter and source a script file, returning a value:
<p>
<center>
<table width=100% cellpadding=5>
<tr><td bgcolor="#afafac">
<font size=+1><a href="CallScript1.java">CallScript1.java</a> - Invoke a script from Java</font>
</td></tr>
<tr><td bgcolor="#cfcfcc">
<pre>
import bsh.*;
public class CallScript1 {
public static void main( String [] args ) throws EvalError {
Object result = new bsh.Interpreter().source("myscript.bsh");
}
}
</pre>
</td></tr>
</table>
</center>
<p>
<p>
This time we'll handle the two types of EvalError that can be thrown:
<p>
<center>
<table width=100% cellpadding=5>
<tr><td bgcolor="#afafac">
<font size=+1><a href="CallScript2.java">CallScript2.java</a> - Invoke a script from Java, handling errors</font>
</td></tr>
<tr><td bgcolor="#cfcfcc">
<pre>
import bsh.*;
public class CallScript2 {
public static void main( String [] args ) throws Exception {
try {
Object obj = new bsh.Interpreter().source("myscript.bsh");
} catch ( TargetError e ) {
System.out.println(
"The script or code called by the script threw an exception: "
+ e.getTarget() );
} catch ( EvalError e2 ) {
System.out.println(
"There was an error in evaluating the script:" + e2 );
}
}
}
</pre>
</td></tr>
</table>
</center>
<p>