KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sapia > magnet > domain > java > JavaTask


1 package org.sapia.magnet.domain.java;
2
3 // Import of Sun's JDK classes
4
// ---------------------------
5
import java.lang.reflect.Method JavaDoc;
6 import java.lang.reflect.InvocationTargetException JavaDoc;
7
8
9 /**
10  *
11  *
12  * @author Jean-Cedric Desrochers
13  *
14  * <dl>
15  * <dt><b>Copyright:</b><dd>Copyright &#169; 2002-2003 <a HREF="http://www.sapia-oss.org">Sapia Open Source Software</a>. All Rights Reserved.</dd></dt>
16  * <dt><b>License:</b><dd>Read the license.txt file of the jar or visit the
17  * <a HREF="http://www.sapia-oss.org/license.html" target="sapia-license">license page</a> at the Sapia OSS web site</dd></dt>
18  * </dl>
19  */

20 public class JavaTask implements Runnable JavaDoc {
21
22   /////////////////////////////////////////////////////////////////////////////////////////
23
///////////////////////////////// INSTANCE ATTRIBUTES /////////////////////////////////
24
/////////////////////////////////////////////////////////////////////////////////////////
25

26   /** The main class of this java task. */
27   private Class JavaDoc _theMainClass;
28
29   /** The arguments of this java task. */
30   private String JavaDoc[] _theArguments;
31
32   /////////////////////////////////////////////////////////////////////////////////////////
33
//////////////////////////////////// CONSTRUCTORS /////////////////////////////////////
34
/////////////////////////////////////////////////////////////////////////////////////////
35

36   /**
37    * Creates a new JavaTask instance with the passed in arguments.
38    */

39   public JavaTask(Class JavaDoc aMainClas, String JavaDoc[] someArguments) {
40     _theMainClass = aMainClas;
41     _theArguments = someArguments;
42   }
43
44   /**
45    * Run method of the Runnable interface that calls the main method of the main class.
46    */

47   public void run() {
48     try {
49       Method JavaDoc aMainMethod = _theMainClass.getDeclaredMethod("main", new Class JavaDoc[] { String JavaDoc[].class });
50       aMainMethod.invoke(_theMainClass, new Object JavaDoc[] { _theArguments });
51     } catch (NoSuchMethodException JavaDoc nsme) {
52       throw new RuntimeException JavaDoc("main method not found on class " + _theMainClass.getName() +
53                                  " - " + nsme.getMessage());
54     } catch (InvocationTargetException JavaDoc ite) {
55       throw new RuntimeException JavaDoc("Error calling main method on class " + _theMainClass.getName() +
56                                  " - " + ite.getTargetException().getMessage());
57     } catch (IllegalAccessException JavaDoc iae) {
58       throw new RuntimeException JavaDoc("main method was found but is not accessible on class " + _theMainClass.getName() +
59                                  " - " + iae.getMessage());
60     }
61   }
62 }
63
64
Popular Tags