I'm not sure if my title adequately explains what I want to do, so I'll provide a scenario.
I have two classes in a folder, one named UserInterface.class and another named HelloWorld.class, and the contents are as follows:
import java.util.Scanner;
public class UserInterface
{
public static void main(String[] args)
{
Scanner kboard = new Scanner(System.in);
System.out.print("Enter path of class to execute: ");
String path = kboard.nextLine();
executeClass(path);
}
public static void executeClass(String path)
{
//???
}
}
public class HelloWorld
{
public HelloWorld()
{
System.out.println("Hello World!");
}
}
I want to be able to input "HelloWorld.class" when UserInterface.class is run, and have "Hello World!" printed to the screen. What I am having trouble doing is finding how to do this, specifically what would be in the executeClass method in this example.
Is this possible? And if so, can someone point me in the right direction?
I have searched the documentation on sun.com and if I understand the defineClass method of ClassLoader correctly, it seems to be promising if I were to read the contents of a class file into an array of bytes, but I'm not completely sure what I would do with the return, and the notation "protected final Class<?>" as the return type seems somewhat cryptic to me. (Why is it protected, why is it final, and what in the world does <?> mean?)
Thanks for any advice you can give!