All Downloads are FREE. Search and download functionalities are using the official Maven repository.

es.ucm.fdi.gaia.jcolibri.util.Launcher Maven / Gradle / Ivy

Go to download

jCOLIBRI is a java framework for the development of Case-Based Reasoning systems.

There is a newer version: 3.2
Show newest version
/**
 * Launcher.java
 * jCOLIBRI2 framework. 
 * @author Juan A. Recio-Garc�a.
 * GAIA - Group for Artificial Intelligence Applications
 * http://gaia.fdi.ucm.es
 * 10/01/2007
 */
package es.ucm.fdi.gaia.jcolibri.util;

import java.io.File;
import java.io.IOException;
import java.lang.reflect.Method;
import java.net.URL;
import java.net.URLClassLoader;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.apache.logging.log4j.LogManager;
import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;


/**
 * This class launches a jCOLIBRI application loading dinamically its libraries. 
 * It has a method that parses the ".classpath" file of the eclipse project to find the libraries that a project needs.
 * Then it loads the libraries and executes an application.
 * This eases the process of invoking a jCOLIBRI application.
 * 
 * @author Juan A. Recio Garcia
 * @version 2.0
 */
public class Launcher
{

    /**
     * Main method used to launch any application loading the libraries from a eclipse classpath file.
     * The first argument must be the class to launch and the second (optional) is the name of the eclipse classpath file.
     * If this second argument is not specified it tries with ".classpath". 
     */
    public static void main(String[] args)
    {
    	if(args.length == 0)
    	{
    		System.out.println("Usage: jcolibri.util.Launcher MainClass [eclipse_classpath_file]");
    		System.exit(0);
    	}

        try {
        	String classpathfile = ".classpath";
        	if(args.length==2)
        		classpathfile = args[1];
        	
        	System.out.println("Loading class path from file: "+classpathfile);
        			
        	URL[] libraries = getClassPath(classpathfile);
        	for(int i=0; i mainClass = Class.forName(args[0]);
        	LogManager.getLogger(Launcher.class).info("Executing class: "+args[0]);
        	Method mainMethod = mainClass.getMethod("main", args.getClass());
        	
        	String[] newargs = new String[args.length-1];
        	for(int i=1; i sysclass = URLClassLoader.class;
     
    	try {
    		//Please don't read this code. It is a dark way to invoke a private method using reflection.
    		//The clear way consists on creating your own subclass of URLClassLoader.
    		Class[] parameters = new Class[]{URL.class};
    		Method method = sysclass.getDeclaredMethod("addURL",parameters);
    		method.setAccessible(true);
    		method.invoke(sysloader,new Object[]{ u });
    	} catch (Throwable t) {
    		t.printStackTrace();
    		throw new IOException("Error, could not add URL to system classloader");
    	}//end try catch	
    }//end method
    
    /**
     * Parses a eclipse classpath file returning a list of URLs to the libraries in that file.
     */
    protected static URL[] getClassPath(String classpathfile) throws Exception
    {
        
        java.util.ArrayList CLASSPATH = new java.util.ArrayList();
        try
        {
            DocumentBuilder db = DocumentBuilderFactory.newInstance()
            .newDocumentBuilder();
            Document doc = db.parse(classpathfile);
                  
            NodeList nl = doc.getElementsByTagName("classpathentry");
            for(int i=0; i




© 2015 - 2024 Weber Informatics LLC | Privacy Policy