import java.lang.reflect.*;
import java.net.*;
import java.io.*;
class runMain{
	private String path; // path of class that main is in
	private String className;
	
	// in: String that is the path and name of class for the main method
	public runMain(String p, String c)
	{
		path = p;
		className = c;
	}
	
	public void exitMain()
	{
	}
	
	// go is run to invoke the main.
	public void go()
	{
	    Class[] argTypes = new Class[1];
    	argTypes[0] = String[].class;
    	
    	try {
      		
      		// Testing
      		File p = new File(path);
      		System.out.println(p.canRead());
      		
      		//Method mainMethod = Class.forName(m).getDeclaredMethod("main",argTypes);
      		Method mainMethod = new URLClassLoader(new URL[] {new File(path).toURL()}).loadClass(className).getDeclaredMethod("main",argTypes);
      		Object[] argListForInvokedMain = new Object[1];
      		argListForInvokedMain[0] = new String[0];
      		
      		
      		//argListForInvokedMain[0]=path;
      		
      		
      		// Place whatever args you
      		// want to pass into other
      		// class's main here.
      		
      		mainMethod.invoke(null,argListForInvokedMain);
      		// This is the instance on which you invoke
      		// the method; since main is static, you can pass
      		// null in.
      	}
      	catch (MalformedURLException ex){
      		System.err.println("URL Exception: "+ex);
      	}
      	catch (ClassNotFoundException ex) {
        	System.err.println("Class not found in classpath: "+ex);
      	}
      	catch (NoSuchMethodException ex) {
	        System.err.println("Class does not define public static void main(String[]): "+ex);
    	  }
      	catch (InvocationTargetException ex) {
        	System.err.println("Exception while executing: "+ex.getTargetException());
      	}
      	catch (IllegalAccessException ex) {
        	System.err.println("main(String[]) in class is not public: "+ex);
		}
	}	
}