1 package net.sf.tomp.xtcl.command;
2
3 import net.sf.tomp.xtcl.Context;
4
5 /***
6 * Executes an executable Java class (i.e. with main method)
7 *
8 * @author tomp
9 */
10 public class JavaCommand extends AbstractCommand {
11
12 /*** the Java class name */
13 protected String className;
14
15 /*** the Java class arguments */
16 protected String[] args;
17
18 /***
19 * sets the args
20 *
21 * @param r the Java class args
22 */
23 public void setArgs(String[] r) {
24 args = r;
25 }
26
27 /***
28 * the executed Java class name
29 *
30 * @param r the executed Java class name
31 */
32 public void setClassName(String r) {
33 className = r;
34 }
35
36 /***
37 * Executes the commmand, i.e. the Java class with args
38 *
39 */
40 public int execute(Context c) throws Exception {
41 int retCode = 0;
42
43 try {
44 Class toBeExecuted = Class.forName(className);
45 java.lang.reflect.Method main = toBeExecuted.getMethod("main",
46 new Class[] { new String[0].getClass() });
47
48 main.invoke(null, new Object[] { args });
49
50 } catch (Exception e) {
51 e.printStackTrace();
52 retCode = 1;
53 }
54
55 return done(c, retCode);
56 }
57
58 /***
59 * @return "JAVA " + className + "(" + listArray(args) + ")";
60 */
61 public String toString() {
62 return "JAVA " + className + "(" + listArray(args) + ")";
63 }
64 }
65
66
67
68
69
70
71
72
73
74
75