How to get the Java Class Object at runtime.

Hai all,

Here I have created one Object return type method

In that I am creating one .java file at runtime in one folder of my system.

In the next step I am creating .class file for that .java file.

The above two steps are successfully completed and I am able to see .java and .class files in my folder

Now In the next step i have to return this class Object to my method.

but am unable to create object for that class file because my program not able to find out the .class file at runtime in that folder but it was there.

My code is:

to create class file at runtime:

>Runtime run = Runtime.getRuntime();

>run.exec("javac D:\\WorkSpace\\Tasks\\Out.java");

to create object at runtime:

>Object obj= Class.forName("Out").newInstance();

but it was failed to find out the .class file

so please help me how to get the object for that class file or is there any way to refresh that current folder at runtime.

[1004 byte] By [sudha279.ambrosia@miraclesofta] at [2007-11-27 1:37:22]
# 1

simplest way is to use a URLClassLoader. I also advise you put all your classes in packages, or you'll hit problems

so construct a URLClassLoader that knows where your class file is (plenty of info on the web about that) and then

URLClassLoader loader = // construct classloader here

Class clazz = Class.forName("my.class.name", true, loader);

Object obj = clazz.newInstance();

georgemca at 2007-7-12 0:47:51 > top of Java-index,Java Essentials,Java Programming...
# 2
Hai georgemc,Clould you please give me the code to create URLClassLoader object. Here am getting errors.thanks
sudha279.ambrosia@miraclesof at 2007-7-12 0:47:51 > top of Java-index,Java Essentials,Java Programming...
# 3

> Hai georgemc,

>

> Clould you please give me the code to create

> URLClassLoader object.

> Here am getting errors.

>

> thanks

URL[] urls ={ new URL("file://c:/path/to/class.class") }; // obviously I'm playing jazz here

URLClassLoader loader = URLClassLoader.newInstance(urls);

// you've got the rest

georgemca at 2007-7-12 0:47:51 > top of Java-index,Java Essentials,Java Programming...
# 4

hai

I tried for below code :

java file name is: Out.java

URL[] urls ={ new URL("file://D:/WorkSpace/Tasks/Out.class") };

URLClassLoader urlLoader = URLClassLoader.newInstance(urls);

Class classObj = Class.forName("Out",true,urlLoader);

Object obj = classObj.newInstance();

is it correct way ?

am getting this error

[4/19/07 21:59:32:674 IST] 00000062 SystemErrR java.lang.ClassNotFoundException: Out

at java.net.URLClassLoader.findClass(URLClassLoader.java(Compiled Code))

at java.lang.ClassLoader.loadClass(ClassLoader.java(Compiled Code))

at java.net.FactoryURLClassLoader.loadClass(URLClassLoader.java:888)

at java.lang.ClassLoader.loadClass(ClassLoader.java(Compiled Code))

at java.lang.Class.forName0(Native Method)

at java.lang.Class.forName(Class.java(Compiled Code))

please check it out...

sudha279.ambrosia@miraclesof at 2007-7-12 0:47:52 > top of Java-index,Java Essentials,Java Programming...
# 5

> hai

>

> I tried for below code :

>

> java file name is: Out.java

>

> URL[] urls ={ new

> URL("file://D:/WorkSpace/Tasks/Out.class") };

> URLClassLoader urlLoader =

> URLClassLoader.newInstance(urls);

> Class classObj =

> Class.forName("Out",true,urlLoader);

> Object obj = classObj.newInstance();

>

> is it correct way ?

>

> am getting this error

>

> [4/19/07 21:59:32:674 IST] 00000062 SystemErrR

> java.lang.ClassNotFoundException: Out

> at

> t

> java.net.URLClassLoader.findClass(URLClassLoader.java(

> Compiled Code))

> at

> t

> java.lang.ClassLoader.loadClass(ClassLoader.java(Compi

> led Code))

> at

> t

> java.net.FactoryURLClassLoader.loadClass(URLClassLoade

> r.java:888)

> at

> t

> java.lang.ClassLoader.loadClass(ClassLoader.java(Compi

> led Code))

> at java.lang.Class.forName0(Native Method)

> at java.lang.Class.forName(Class.java(Compiled

> d Code))

>

>

> please check it out...

my apologies! don't include the actual class name in the URL, just the root folder(s) of where the classes are. silly me, sorry again

and put them in packages!

georgemca at 2007-7-12 0:47:52 > top of Java-index,Java Essentials,Java Programming...