JarLauncher.java :  » Groupware » lucane » org » lucane » jarlauncher » Java Open Source

Java Open Source » Groupware » lucane 
lucane » org » lucane » jarlauncher » JarLauncher.java
/*
 * Lucane - a collaborative platform
 * Copyright (C) 2004  Vincent Fiack <vfiack@mail15.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
package org.lucane.jarlauncher;

import java.io.File;
import java.io.FilenameFilter;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;

public class JarLauncher
{
  public static void main(String[] args) 
  throws Throwable
  {
  //check args
    if(args.length < 2)
    {
      System.err.println("usage: JarLauncher <lib-path> <main-class> [params]");
      System.exit(1);
    }
  
    //read args
    String path = args[0];
    String mainClass = args[1];
    String[] newArgs = new String[args.length-2];
    for(int i=0;i<newArgs.length;i++)
      newArgs[i] = args[i+2];
      
    //get jars names
    File lib = new File(path);
    File[] jars = lib.listFiles(new FilenameFilter() {
    public boolean accept(File dir, String name) {
      return name.toLowerCase().endsWith(".jar");
    }
  });
    
    //create url array
    URL[] urls = new URL[jars.length];
    for(int i=0;i<jars.length;i++)
    {
    try {
      String fullPath = "jar:file:///" + jars[i].getAbsolutePath().replace('\\', '/') + "!/";
      urls[i] = new URL(fullPath);
    } catch (MalformedURLException e) {
      System.err.println("Unable to load jar : " + jars[i].getName());
    }
    }
    
    //create classloader
    ClassLoader loader = new URLClassLoader(urls);
    
    try {
    //load main class
    Class c = Class.forName(mainClass, true, loader);
    Method m = c.getDeclaredMethod("main", new Class[]{String[].class});
    m.invoke(null, new Object[]{newArgs});
  } catch (ClassNotFoundException e) {
    System.err.println("Unable to find class : " + mainClass);
  } catch (SecurityException e) {
    System.err.println("Unable to load main method in : " + mainClass);
  } catch (NoSuchMethodException e) {
    System.err.println("No main method in : " + mainClass);
  } catch (IllegalArgumentException e) {
    System.err.println("Wrong main method in : " + mainClass);
  } catch (IllegalAccessException e) {
    System.err.println("Wrong main method in : " + mainClass);
  } catch (InvocationTargetException e) {
    throw e.getCause();
  }    
  }
}

java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.