-1

I get a class file at runtime and I am saving it in a particular location. How can I create an object of this class:

Class.forName(MyDynamicClass); 

This does not work. The class does not have any package, so how to instantiate this class ?

I just have the MyDynamicClass.class file which is in my home folder

I tried "Amir Afghani" answer which throws the ClassNotFound Exception.

Community
  • 1
  • 1
Amaldev
  • 983
  • 8
  • 10

3 Answers3

0
        ClassLoader myClassLoader = ClassLoader.getSystemClassLoader();

         // Step 2: Define a class to be loaded.

        String classNameToBeLoaded = "MyDynamicClass";


         // Step 3: Load the class

        Class myClass = myClassLoader.loadClass(classNameToBeLoaded);


         // Step 4: create a new instance of that class

        Object whatInstance = myClass.newInstance();
Chetan Kinger
  • 15,069
  • 6
  • 45
  • 82
Alok Jain
  • 57
  • 2
  • String classNameToBeLoaded = "MyDynamicClass"; what should i give in place of "MyDynamicClass", in my case "MyDynamicClass.class" is file saved in somelocation – Amaldev May 30 '15 at 07:46
  • 1
    URL myurl[] = { new URL("file:///C:/Amal/file/web/"), new URL("http://www.aba.com/somepath/") }; URLClassLoader x = new URLClassLoader(myurl); Class c = x.loadClass("MyDyamicClass"); Class getArg1[] = { (new String[1]).getClass() }; Method m = c.getMethod("main", getArg1); String[] my1 = { "arg1 passed", "arg2 passed" }; Object myarg1[] = { my1 }; m.invoke(null, myarg1); – Alok Jain May 30 '15 at 09:33
0

I have tested with and without package and both cases working fine.

Below are the classes

Default Package:

public class MyDynamicClass {

}

Under package com.test

package com.test;

public class MyDynamicClass {

}

package com.test;

public class MyDynamicClassTest {

    public static void main(String[] args) {
        try {
            Class clazz = Class.forName("com.test.MyDynamicClass");
            Class clazz2 = Class.forName("MyDynamicClass");


            System.out.println("clazz :: "+ clazz);
            System.out.println("clazz :: "+ clazz2);

        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
}
Output:

clazz :: class com.test.MyDynamicClass
clazz :: class MyDynamicClass

You can call newInstance() to create object once you have class object.

How are you compiling your class? Can you make sure there is .class file exist?

TheCodingFrog
  • 3,406
  • 3
  • 21
  • 27
  • In your case "MyDynamicClass" is saved in side package called "com.test" and you can see the import statement, so "com.test.MyDynamicClass" and "MyDynamicClass" are same – Amaldev May 30 '15 at 07:25
  • There are two - one with the package "com.test" and another in 'default' package. – TheCodingFrog May 30 '15 at 07:27
  • delete "Class clazz = Class.forName("com.test.MyDynamicClass");" and "package com.test;" and run your program – Amaldev May 30 '15 at 07:35
  • forget about everything, just tell me how to create a object of a class file which i saved in "C:\MyDynamicClass.class". i get the class file at runtime i dont have the .java file for that class. – Amaldev May 30 '15 at 07:52
0

Steps that you can try to debug:

  1. Check your class path
    When you perform Class.forName("MyDynamicClass");
    It checks in your classpath for the file MyDynamicClass.class and loads it

  2. When you run your program, run with -verbose argument
    This will help you identify how this class is searched and is loaded once you fix the class path issue

  3. If you do not want to add this to your classpath, then you will most definitely have to write your own class loader (See java.net.URLClassLoader which provides an easier way to do this). This will allow you to put custom paths to be loaded

MJSG
  • 1,035
  • 8
  • 12
  • there is no class path , its just a file "MyDynamicClass.class" saved in one location – Amaldev May 30 '15 at 07:50
  • You should append that directory to your classpath, then try it again. – MJSG May 30 '15 at 08:42
  • @Amal Could you please update your question with the directory structure and the steps that you are performing to do this, also What did u set as your classpath?? – MJSG May 30 '15 at 08:46
  • http://stackoverflow.com/questions/2320404/creating-classes-dynamically-with-java checkout "Amir Afghani" answer – Amaldev May 30 '15 at 08:50