KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jline > ConsoleRunner


1 /**
2  * jline - Java console input library
3  * Copyright (c) 2002,2003 Marc Prud'hommeaux mwp1@cornell.edu
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  */

19 package jline;
20
21 import java.io.*;
22 import java.text.*;
23 import java.util.*;
24
25
26 /**
27  * <p>
28  * A pass-through application that sets the system input stream to a
29  * {@link ConsoleReader} and invokes the specified main method.
30  * </p>
31  *
32  * @author <a HREF="mailto:mwp1@cornell.edu">Marc Prud'hommeaux</a>
33  */

34 public class ConsoleRunner
35 {
36     public static void main (String JavaDoc[] args)
37         throws Exception JavaDoc
38     {
39         List argList = new ArrayList (Arrays.asList (args));
40         if (argList.size () == 0)
41         {
42             usage ();
43             return;
44         }
45
46         // invoke the main() method
47
String JavaDoc mainClass = (String JavaDoc)argList.remove (0);
48
49         // setup the inpout stream
50
ConsoleReader reader = new ConsoleReader ();
51         reader.setHistory (new History (new File (
52             System.getProperty ("user.home"), ".jline-" + mainClass
53                 + ".history")));
54
55         String JavaDoc completors = System.getProperty (ConsoleRunner.class.getName ()
56             + ".completors", "");
57         List completorList = new ArrayList ();
58         for (StringTokenizer tok = new StringTokenizer (completors, ",");
59             tok.hasMoreTokens (); )
60         {
61             completorList.add ((Completor)Class.forName (tok.nextToken ())
62                 .newInstance ());
63         }
64
65         if (completorList.size () > 0)
66             reader.addCompletor (new ArgumentCompletor (completorList));
67
68         ConsoleReaderInputStream.setIn (reader);
69         try
70         {
71             Class.forName (mainClass)
72                 .getMethod ("main", new Class JavaDoc[] { String JavaDoc[].class})
73                 .invoke (null, new Object JavaDoc[] { argList.toArray (new String JavaDoc[0])});
74         }
75         finally
76         {
77             // just in case this main method is called from another program
78
ConsoleReaderInputStream.restoreIn ();
79         }
80     }
81
82
83     private static void usage ()
84     {
85         throw new IllegalArgumentException JavaDoc ("Usage: java "
86             + ConsoleRunner.class.getName ()
87             + " <target class name> [args]");
88     }
89 }
90
91
Popular Tags