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!

Recommended Answers

All 4 Replies

Instantiating a class at runtime like that can be done through Reflection. See the following info:
http://java.sun.com/docs/books/tutorial/reflect/index.html


Class.forName() will allow you to get a Class instance for a given full (including package) class name.

There is a ton of info in that tutorial now that I look into it and it might be a lot to digest at once. Here's a quick example of invoking main() on a class by name

try {
            Class c = Class.forName("HelloWorld");
            Method mainMethod = c.getMethod("main",new Class[]{String[].class});
            mainMethod.invoke(null, (Object)new String[0]);
        } catch (Exception ex) {
            ex.printStackTrace();
        }

Thank you very much for the speed and helpfulness!

My working version of executeClass:

public static void executeClass(String path)
{
     try
     {
          Class c = Class.forName(path);
          Object o = c.newInstance();
          System.out.println("Class Loaded.");
     }
     catch(Exception e)
     {
          System.out.println("EXCEPTION");
     }
}

There is a ton of info in that tutorial now that I look into it and it might be a lot to digest at once.

For the moment, CTRL+F remedied that. :D
I'll be sure to read through it, though, as it seems to include a lot of interesting information.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.