View Javadoc

1   package org.codehaus.mojo.macker;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *      http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.io.FileInputStream;
23  import java.io.InputStreamReader;
24  import java.io.Reader;
25  import java.lang.reflect.InvocationTargetException;
26  import java.lang.reflect.Method;
27  import java.util.ArrayList;
28  import java.util.List;
29  
30  import org.apache.commons.io.IOUtils;
31  
32  /**
33   * Wrapper for calling a Java main method with a number of arguments.
34   * @author <a href="http://www.code-cop.org/">Peter Kofler</a>
35   */
36  public class CommandLineFile
37  {
38  
39      public static void main( String[] args )
40          throws Exception
41      {
42          if ( args.length == 0 || args.length > 2 )
43          {
44              System.err.println( "Usage: CommandLineFile <main class> [command line arguments file]" );
45              System.exit( 1 );
46          }
47  
48          String className = args[0];
49          Class clazz = Class.forName( className );
50          Method main = clazz.getMethod( "main", new Class[] { String[].class } );
51  
52          List/*<String>*/lines = new ArrayList/*<String>*/();
53          if ( args.length == 2 )
54          {
55              Reader in = new InputStreamReader( new FileInputStream( args[1] ), "UTF-8" );
56              try
57              {
58                  lines = IOUtils.readLines( in );
59              }
60              finally
61              {
62                  in.close();
63              }
64          }
65  
66          try
67          {
68              main.invoke( null, new Object[] { lines.toArray( new String[lines.size()] ) } );
69          }
70          catch ( InvocationTargetException ex )
71          {
72              Throwable cause = ex.getTargetException();
73              if ( cause instanceof Error )
74              {
75                  throw (Error) cause;
76              }
77              throw (Exception) cause;
78          }
79      }
80  }