I want to create an object I know only its classpath Any help will be appreciated.
Asked
Active
Viewed 3,510 times
0
-
1Can you clarify what you mean by _"I know only its classpath?"_ Exactly what information do you have to work with? +1 to @BalusC's comment as well. – Matt Ball Dec 13 '10 at 20:38
-
2What exactly do you mean with "classpath"? The full qualified classname in a `java.lang.String` or the classpath location in a `java.net.URL` (as obtained by `ClassLoader#getResource()`) or an absolute file path in a `java.io.File`? – BalusC Dec 13 '10 at 20:39
-
Classpath? If you mean something along the lines "org.mycompany.FirstClass" than you're good to go! This kind of string is called Fully Qualified Name. – Rekin Dec 13 '10 at 20:40
2 Answers
6
If you have the full qualified classname in a String
, use Class#forName()
and Class#newInstance()
.
Object o = Class.forName("com.example.Foo").newInstance();
This however requires the class to be already present in the classpath and have a (implicit) default constructor.
If it is not, and you have the class' location in an URL
, then use URLClassLoader
and pass it to another Class#forName()
method which accepts it as an argument.
URL url = getItSomehow();
URLClassLoader classLoader = URLClassLoader.newInstance(new URL[] { url });
Object o = Class.forName("com.example.Foo", true, classLoader).newInstance();
Or, if you have it in a File
instead, then convert it to URL
first:
File file = getItSomehow();
URL url = file.toURI().toURL();
// Continue with URLClassLoader.

BalusC
- 1,082,665
- 372
- 3,610
- 3,555
-
OK, but what if I want to call any method belongs to the class, that I know its path or qualified name – Mina Fouad Dec 13 '10 at 20:55
-
That's a different question, so here's just a tutorial link instead of an answer: http://download.oracle.com/javase/tutorial/reflect/member/methodInvocation.html If you stucks, press `Ask Question` button on right top. Don't forget to include detail about the particular step you're stucking with, preferably along with a small code snippet which reproduces exactly your problem. Be careful with choosing and using the right terms. The term "classpath" was apparently not what you thought it was :) – BalusC Dec 13 '10 at 20:55
-
OK, but I want to clarify that in my project I have 3 packages with 3 classes I want in 4th package and 4th class to instantiate an object of one of the three classes in the 3 packages according to input from the user. – Mina Fouad Dec 13 '10 at 21:10
-
Yes, I already guessed something like that. The 1st point of my answer is then applicable. – BalusC Dec 13 '10 at 21:11
-
but I still have to check if the class 'o' is one of the three classes or in other words repeat the code 3 times for the three classes.. I used reflection to get an optimal solution for this case – Mina Fouad Dec 13 '10 at 21:23
-
Just use `instanceof` and then cast it, or, better, let them implement a common interface as per the abstract factory pattern. If you stucks, press `Ask Question` in right top since that's out of the scope of the original question. – BalusC Dec 13 '10 at 22:39
2
Did you mean this ?
Class c = Class.forName("java.lang.String");