Getting Class type from String












66















I have a String which has a name of a class say "Ex" (no .class extension). I want to assign it to a Class variable, like this:



Class cls = (string).class


How can i do that?










share|improve this question

























  • what if the class is in different project?

    – Steven
    Mar 9 '10 at 12:25













  • To your comment: What!??? If your class was in a different project, then wouldn't it depend on your IDE? As far as your application is concerned, it would be as though it was in the same project anyways because it's libraries are referenced externally. Like, you know that the the Java API classes are not in your project, right? But, the compiler for your IDE knows where to find them, if your IDE is set up correctly. The same applies to your classes from another project.

    – user919860
    Jan 16 '13 at 17:12


















66















I have a String which has a name of a class say "Ex" (no .class extension). I want to assign it to a Class variable, like this:



Class cls = (string).class


How can i do that?










share|improve this question

























  • what if the class is in different project?

    – Steven
    Mar 9 '10 at 12:25













  • To your comment: What!??? If your class was in a different project, then wouldn't it depend on your IDE? As far as your application is concerned, it would be as though it was in the same project anyways because it's libraries are referenced externally. Like, you know that the the Java API classes are not in your project, right? But, the compiler for your IDE knows where to find them, if your IDE is set up correctly. The same applies to your classes from another project.

    – user919860
    Jan 16 '13 at 17:12
















66












66








66


20






I have a String which has a name of a class say "Ex" (no .class extension). I want to assign it to a Class variable, like this:



Class cls = (string).class


How can i do that?










share|improve this question
















I have a String which has a name of a class say "Ex" (no .class extension). I want to assign it to a Class variable, like this:



Class cls = (string).class


How can i do that?







java class reflection






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Aug 23 '17 at 9:22









Vadim Kotov

4,77163549




4,77163549










asked Mar 9 '10 at 12:02









StevenSteven

1,46751427




1,46751427













  • what if the class is in different project?

    – Steven
    Mar 9 '10 at 12:25













  • To your comment: What!??? If your class was in a different project, then wouldn't it depend on your IDE? As far as your application is concerned, it would be as though it was in the same project anyways because it's libraries are referenced externally. Like, you know that the the Java API classes are not in your project, right? But, the compiler for your IDE knows where to find them, if your IDE is set up correctly. The same applies to your classes from another project.

    – user919860
    Jan 16 '13 at 17:12





















  • what if the class is in different project?

    – Steven
    Mar 9 '10 at 12:25













  • To your comment: What!??? If your class was in a different project, then wouldn't it depend on your IDE? As far as your application is concerned, it would be as though it was in the same project anyways because it's libraries are referenced externally. Like, you know that the the Java API classes are not in your project, right? But, the compiler for your IDE knows where to find them, if your IDE is set up correctly. The same applies to your classes from another project.

    – user919860
    Jan 16 '13 at 17:12



















what if the class is in different project?

– Steven
Mar 9 '10 at 12:25







what if the class is in different project?

– Steven
Mar 9 '10 at 12:25















To your comment: What!??? If your class was in a different project, then wouldn't it depend on your IDE? As far as your application is concerned, it would be as though it was in the same project anyways because it's libraries are referenced externally. Like, you know that the the Java API classes are not in your project, right? But, the compiler for your IDE knows where to find them, if your IDE is set up correctly. The same applies to your classes from another project.

– user919860
Jan 16 '13 at 17:12







To your comment: What!??? If your class was in a different project, then wouldn't it depend on your IDE? As far as your application is concerned, it would be as though it was in the same project anyways because it's libraries are referenced externally. Like, you know that the the Java API classes are not in your project, right? But, the compiler for your IDE knows where to find them, if your IDE is set up correctly. The same applies to your classes from another project.

– user919860
Jan 16 '13 at 17:12














6 Answers
6






active

oldest

votes


















145














Class<?> cls = Class.forName(className);


But your className should be fully-qualified - i.e. com.mycompany.MyClass






share|improve this answer





















  • 2





    its throwing ClassNotFound Exception

    – Steven
    Mar 9 '10 at 12:11











  • then either the class is not on the classpath or you are not passing the fully qualified class name e.g. com.mycompany.project.ClassName

    – Hans Westerbeek
    Mar 9 '10 at 12:14













  • what value of className are you passing? Is there such a class on your classpath?

    – Bozho
    Mar 9 '10 at 12:14











  • yeah the Class is in same package

    – Steven
    Mar 9 '10 at 12:16






  • 1





    I got the same problem with ClassNotFoundException. The trick was to handle the exception in a try-catch. Something like this: try { Class<?> cls = Class.forName("com.company.MyClass"); } catch (ClassNotFoundException ex) { /* do something to handle the case when the string isn't valid and therefor the class can't be found */ }

    – kumaheiyama
    Oct 24 '14 at 19:53





















37














String clsName = "Ex";  // use fully qualified name
Class cls = Class.forName(clsName);
Object clsInstance = (Object) cls.newInstance();


Check the Java Tutorial trail on Reflection at http://java.sun.com/docs/books/tutorial/reflect/TOC.html for further details.






share|improve this answer



















  • 3





    Bozho's answer might have more points, but I think that yours is more descriptive. PS. Why is your name JuanZe??? Almost sounds like a mix between Spanish and Chinese. :D. Is there some sort of clever meaning behind it?

    – user919860
    Jan 16 '13 at 17:09











  • @user919860, You need to read it backwards.

    – Pacerier
    Aug 22 '14 at 13:14











  • I'm working with a Dynamic web project in eclipse. I am using above code to fetch a Class. but it returns ClassNotFoundException, when I use a string variable like this: String clsName = "com.mydoamin.className"; Class cls = Class.forName(clsName); If is hardcode the fully qualified class name as the parameter then things work fine. Any suggestion?

    – Dibyanshu Jaiswal
    Nov 25 '15 at 4:58



















6














You can use the forName method of Class:



Class cls = Class.forName(clsName);
Object obj = cls.newInstance();





share|improve this answer


























  • I'm working with a Dynamic web project in eclipse. I am using above code to fetch a Class. but it returns ClassNotFoundException, when I use a string variable like this: String clsName = "com.mydoamin.className"; Class cls = Class.forName(clsName); If is hardcode the fully qualified class name as the parameter then things work fine. Any suggestion?

    – Dibyanshu Jaiswal
    Nov 25 '15 at 4:55













  • @djthequest If our clsName variable contains the fully qualified classname there should be no difference between using it and the hardcoded string. You might not have the class you need in the classpath. It might help to instantiate a common class from the same jar in your startup code to make sure the library is present in the classloader.

    – rsp
    Dec 1 '15 at 9:43











  • thanks for your response, but instead of the variable if I hardcode the string, works fins, it means the classes are in class path, right? Later i figured out, Class.forName() method expects a final variable. A simple variable won't work.

    – Dibyanshu Jaiswal
    Dec 3 '15 at 6:01



















4














You can get the Class reference of any class during run time through the Java Reflection Concept.



Check the Below Code. Explanation is given below



Here is one example that uses returned Class to create an instance of AClass:



package com.xyzws;
class AClass {
public AClass() {
System.out.println("AClass's Constructor");
}
static {
System.out.println("static block in AClass");
}
}
public class Program {
public static void main(String args) {
try {
System.out.println("The first time calls forName:");
Class c = Class.forName("com.xyzws.AClass");
AClass a = (AClass)c.newInstance();
System.out.println("The second time calls forName:");
Class c1 = Class.forName("com.xyzws.AClass");
} catch (ClassNotFoundException e) {
// ...
} catch (InstantiationException e) {
// ...
} catch (IllegalAccessException e) {
// ...
}
}
}


The printed output is



    The first time calls forName:
static block in AClass
AClass's Constructor
The second time calls forName:


The class has already been loaded so there is no second "static block in AClass"



The Explanation is below



Class.ForName is called to get a Class Object



By Using the Class Object we are creating the new instance of the Class.



Any doubts about this let me know






share|improve this answer


























  • what if class is in different project?

    – Steven
    Mar 9 '10 at 12:26






  • 1





    Can you get a jar file of that proect and specify the class name some thing like Class.forName("oracle.jdbc.driver.OracleDriver")

    – gmhk
    Mar 9 '10 at 13:30



















3














eeh.. Class.forName(String classname) ?






share|improve this answer
























  • Can you also do this for Subclasses?

    – Gobliins
    Mar 20 '17 at 16:15



















2














Not sure what you are asking, but... Class.forname, maybe?






share|improve this answer
























  • its throwing ClassNotFound Exception

    – Steven
    Mar 9 '10 at 12:12











  • what if class is in different project?

    – Steven
    Mar 9 '10 at 12:26













  • I see that no one is answering your question. It shouldn't matter if it's in another project as long as the compiler knows where to look. Since, you're probably using an IDE, it probably depends solely on the IDE. The thing about references, you know that all of the classes that you reference from the Java libraries are not in your project, right? But, the compiler knows where to look for them because it knows that they're referenced externally. It seems that you need to learn basic Java programming when you're attempting to advanced Java programming. :|

    – user919860
    Jan 16 '13 at 17:11











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f2408789%2fgetting-class-type-from-string%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























6 Answers
6






active

oldest

votes








6 Answers
6






active

oldest

votes









active

oldest

votes






active

oldest

votes









145














Class<?> cls = Class.forName(className);


But your className should be fully-qualified - i.e. com.mycompany.MyClass






share|improve this answer





















  • 2





    its throwing ClassNotFound Exception

    – Steven
    Mar 9 '10 at 12:11











  • then either the class is not on the classpath or you are not passing the fully qualified class name e.g. com.mycompany.project.ClassName

    – Hans Westerbeek
    Mar 9 '10 at 12:14













  • what value of className are you passing? Is there such a class on your classpath?

    – Bozho
    Mar 9 '10 at 12:14











  • yeah the Class is in same package

    – Steven
    Mar 9 '10 at 12:16






  • 1





    I got the same problem with ClassNotFoundException. The trick was to handle the exception in a try-catch. Something like this: try { Class<?> cls = Class.forName("com.company.MyClass"); } catch (ClassNotFoundException ex) { /* do something to handle the case when the string isn't valid and therefor the class can't be found */ }

    – kumaheiyama
    Oct 24 '14 at 19:53


















145














Class<?> cls = Class.forName(className);


But your className should be fully-qualified - i.e. com.mycompany.MyClass






share|improve this answer





















  • 2





    its throwing ClassNotFound Exception

    – Steven
    Mar 9 '10 at 12:11











  • then either the class is not on the classpath or you are not passing the fully qualified class name e.g. com.mycompany.project.ClassName

    – Hans Westerbeek
    Mar 9 '10 at 12:14













  • what value of className are you passing? Is there such a class on your classpath?

    – Bozho
    Mar 9 '10 at 12:14











  • yeah the Class is in same package

    – Steven
    Mar 9 '10 at 12:16






  • 1





    I got the same problem with ClassNotFoundException. The trick was to handle the exception in a try-catch. Something like this: try { Class<?> cls = Class.forName("com.company.MyClass"); } catch (ClassNotFoundException ex) { /* do something to handle the case when the string isn't valid and therefor the class can't be found */ }

    – kumaheiyama
    Oct 24 '14 at 19:53
















145












145








145







Class<?> cls = Class.forName(className);


But your className should be fully-qualified - i.e. com.mycompany.MyClass






share|improve this answer















Class<?> cls = Class.forName(className);


But your className should be fully-qualified - i.e. com.mycompany.MyClass







share|improve this answer














share|improve this answer



share|improve this answer








edited Mar 9 '10 at 12:24

























answered Mar 9 '10 at 12:06









BozhoBozho

490k1089611074




490k1089611074








  • 2





    its throwing ClassNotFound Exception

    – Steven
    Mar 9 '10 at 12:11











  • then either the class is not on the classpath or you are not passing the fully qualified class name e.g. com.mycompany.project.ClassName

    – Hans Westerbeek
    Mar 9 '10 at 12:14













  • what value of className are you passing? Is there such a class on your classpath?

    – Bozho
    Mar 9 '10 at 12:14











  • yeah the Class is in same package

    – Steven
    Mar 9 '10 at 12:16






  • 1





    I got the same problem with ClassNotFoundException. The trick was to handle the exception in a try-catch. Something like this: try { Class<?> cls = Class.forName("com.company.MyClass"); } catch (ClassNotFoundException ex) { /* do something to handle the case when the string isn't valid and therefor the class can't be found */ }

    – kumaheiyama
    Oct 24 '14 at 19:53
















  • 2





    its throwing ClassNotFound Exception

    – Steven
    Mar 9 '10 at 12:11











  • then either the class is not on the classpath or you are not passing the fully qualified class name e.g. com.mycompany.project.ClassName

    – Hans Westerbeek
    Mar 9 '10 at 12:14













  • what value of className are you passing? Is there such a class on your classpath?

    – Bozho
    Mar 9 '10 at 12:14











  • yeah the Class is in same package

    – Steven
    Mar 9 '10 at 12:16






  • 1





    I got the same problem with ClassNotFoundException. The trick was to handle the exception in a try-catch. Something like this: try { Class<?> cls = Class.forName("com.company.MyClass"); } catch (ClassNotFoundException ex) { /* do something to handle the case when the string isn't valid and therefor the class can't be found */ }

    – kumaheiyama
    Oct 24 '14 at 19:53










2




2





its throwing ClassNotFound Exception

– Steven
Mar 9 '10 at 12:11





its throwing ClassNotFound Exception

– Steven
Mar 9 '10 at 12:11













then either the class is not on the classpath or you are not passing the fully qualified class name e.g. com.mycompany.project.ClassName

– Hans Westerbeek
Mar 9 '10 at 12:14







then either the class is not on the classpath or you are not passing the fully qualified class name e.g. com.mycompany.project.ClassName

– Hans Westerbeek
Mar 9 '10 at 12:14















what value of className are you passing? Is there such a class on your classpath?

– Bozho
Mar 9 '10 at 12:14





what value of className are you passing? Is there such a class on your classpath?

– Bozho
Mar 9 '10 at 12:14













yeah the Class is in same package

– Steven
Mar 9 '10 at 12:16





yeah the Class is in same package

– Steven
Mar 9 '10 at 12:16




1




1





I got the same problem with ClassNotFoundException. The trick was to handle the exception in a try-catch. Something like this: try { Class<?> cls = Class.forName("com.company.MyClass"); } catch (ClassNotFoundException ex) { /* do something to handle the case when the string isn't valid and therefor the class can't be found */ }

– kumaheiyama
Oct 24 '14 at 19:53







I got the same problem with ClassNotFoundException. The trick was to handle the exception in a try-catch. Something like this: try { Class<?> cls = Class.forName("com.company.MyClass"); } catch (ClassNotFoundException ex) { /* do something to handle the case when the string isn't valid and therefor the class can't be found */ }

– kumaheiyama
Oct 24 '14 at 19:53















37














String clsName = "Ex";  // use fully qualified name
Class cls = Class.forName(clsName);
Object clsInstance = (Object) cls.newInstance();


Check the Java Tutorial trail on Reflection at http://java.sun.com/docs/books/tutorial/reflect/TOC.html for further details.






share|improve this answer



















  • 3





    Bozho's answer might have more points, but I think that yours is more descriptive. PS. Why is your name JuanZe??? Almost sounds like a mix between Spanish and Chinese. :D. Is there some sort of clever meaning behind it?

    – user919860
    Jan 16 '13 at 17:09











  • @user919860, You need to read it backwards.

    – Pacerier
    Aug 22 '14 at 13:14











  • I'm working with a Dynamic web project in eclipse. I am using above code to fetch a Class. but it returns ClassNotFoundException, when I use a string variable like this: String clsName = "com.mydoamin.className"; Class cls = Class.forName(clsName); If is hardcode the fully qualified class name as the parameter then things work fine. Any suggestion?

    – Dibyanshu Jaiswal
    Nov 25 '15 at 4:58
















37














String clsName = "Ex";  // use fully qualified name
Class cls = Class.forName(clsName);
Object clsInstance = (Object) cls.newInstance();


Check the Java Tutorial trail on Reflection at http://java.sun.com/docs/books/tutorial/reflect/TOC.html for further details.






share|improve this answer



















  • 3





    Bozho's answer might have more points, but I think that yours is more descriptive. PS. Why is your name JuanZe??? Almost sounds like a mix between Spanish and Chinese. :D. Is there some sort of clever meaning behind it?

    – user919860
    Jan 16 '13 at 17:09











  • @user919860, You need to read it backwards.

    – Pacerier
    Aug 22 '14 at 13:14











  • I'm working with a Dynamic web project in eclipse. I am using above code to fetch a Class. but it returns ClassNotFoundException, when I use a string variable like this: String clsName = "com.mydoamin.className"; Class cls = Class.forName(clsName); If is hardcode the fully qualified class name as the parameter then things work fine. Any suggestion?

    – Dibyanshu Jaiswal
    Nov 25 '15 at 4:58














37












37








37







String clsName = "Ex";  // use fully qualified name
Class cls = Class.forName(clsName);
Object clsInstance = (Object) cls.newInstance();


Check the Java Tutorial trail on Reflection at http://java.sun.com/docs/books/tutorial/reflect/TOC.html for further details.






share|improve this answer













String clsName = "Ex";  // use fully qualified name
Class cls = Class.forName(clsName);
Object clsInstance = (Object) cls.newInstance();


Check the Java Tutorial trail on Reflection at http://java.sun.com/docs/books/tutorial/reflect/TOC.html for further details.







share|improve this answer












share|improve this answer



share|improve this answer










answered Mar 9 '10 at 12:07









JuanZeJuanZe

6,9503655




6,9503655








  • 3





    Bozho's answer might have more points, but I think that yours is more descriptive. PS. Why is your name JuanZe??? Almost sounds like a mix between Spanish and Chinese. :D. Is there some sort of clever meaning behind it?

    – user919860
    Jan 16 '13 at 17:09











  • @user919860, You need to read it backwards.

    – Pacerier
    Aug 22 '14 at 13:14











  • I'm working with a Dynamic web project in eclipse. I am using above code to fetch a Class. but it returns ClassNotFoundException, when I use a string variable like this: String clsName = "com.mydoamin.className"; Class cls = Class.forName(clsName); If is hardcode the fully qualified class name as the parameter then things work fine. Any suggestion?

    – Dibyanshu Jaiswal
    Nov 25 '15 at 4:58














  • 3





    Bozho's answer might have more points, but I think that yours is more descriptive. PS. Why is your name JuanZe??? Almost sounds like a mix between Spanish and Chinese. :D. Is there some sort of clever meaning behind it?

    – user919860
    Jan 16 '13 at 17:09











  • @user919860, You need to read it backwards.

    – Pacerier
    Aug 22 '14 at 13:14











  • I'm working with a Dynamic web project in eclipse. I am using above code to fetch a Class. but it returns ClassNotFoundException, when I use a string variable like this: String clsName = "com.mydoamin.className"; Class cls = Class.forName(clsName); If is hardcode the fully qualified class name as the parameter then things work fine. Any suggestion?

    – Dibyanshu Jaiswal
    Nov 25 '15 at 4:58








3




3





Bozho's answer might have more points, but I think that yours is more descriptive. PS. Why is your name JuanZe??? Almost sounds like a mix between Spanish and Chinese. :D. Is there some sort of clever meaning behind it?

– user919860
Jan 16 '13 at 17:09





Bozho's answer might have more points, but I think that yours is more descriptive. PS. Why is your name JuanZe??? Almost sounds like a mix between Spanish and Chinese. :D. Is there some sort of clever meaning behind it?

– user919860
Jan 16 '13 at 17:09













@user919860, You need to read it backwards.

– Pacerier
Aug 22 '14 at 13:14





@user919860, You need to read it backwards.

– Pacerier
Aug 22 '14 at 13:14













I'm working with a Dynamic web project in eclipse. I am using above code to fetch a Class. but it returns ClassNotFoundException, when I use a string variable like this: String clsName = "com.mydoamin.className"; Class cls = Class.forName(clsName); If is hardcode the fully qualified class name as the parameter then things work fine. Any suggestion?

– Dibyanshu Jaiswal
Nov 25 '15 at 4:58





I'm working with a Dynamic web project in eclipse. I am using above code to fetch a Class. but it returns ClassNotFoundException, when I use a string variable like this: String clsName = "com.mydoamin.className"; Class cls = Class.forName(clsName); If is hardcode the fully qualified class name as the parameter then things work fine. Any suggestion?

– Dibyanshu Jaiswal
Nov 25 '15 at 4:58











6














You can use the forName method of Class:



Class cls = Class.forName(clsName);
Object obj = cls.newInstance();





share|improve this answer


























  • I'm working with a Dynamic web project in eclipse. I am using above code to fetch a Class. but it returns ClassNotFoundException, when I use a string variable like this: String clsName = "com.mydoamin.className"; Class cls = Class.forName(clsName); If is hardcode the fully qualified class name as the parameter then things work fine. Any suggestion?

    – Dibyanshu Jaiswal
    Nov 25 '15 at 4:55













  • @djthequest If our clsName variable contains the fully qualified classname there should be no difference between using it and the hardcoded string. You might not have the class you need in the classpath. It might help to instantiate a common class from the same jar in your startup code to make sure the library is present in the classloader.

    – rsp
    Dec 1 '15 at 9:43











  • thanks for your response, but instead of the variable if I hardcode the string, works fins, it means the classes are in class path, right? Later i figured out, Class.forName() method expects a final variable. A simple variable won't work.

    – Dibyanshu Jaiswal
    Dec 3 '15 at 6:01
















6














You can use the forName method of Class:



Class cls = Class.forName(clsName);
Object obj = cls.newInstance();





share|improve this answer


























  • I'm working with a Dynamic web project in eclipse. I am using above code to fetch a Class. but it returns ClassNotFoundException, when I use a string variable like this: String clsName = "com.mydoamin.className"; Class cls = Class.forName(clsName); If is hardcode the fully qualified class name as the parameter then things work fine. Any suggestion?

    – Dibyanshu Jaiswal
    Nov 25 '15 at 4:55













  • @djthequest If our clsName variable contains the fully qualified classname there should be no difference between using it and the hardcoded string. You might not have the class you need in the classpath. It might help to instantiate a common class from the same jar in your startup code to make sure the library is present in the classloader.

    – rsp
    Dec 1 '15 at 9:43











  • thanks for your response, but instead of the variable if I hardcode the string, works fins, it means the classes are in class path, right? Later i figured out, Class.forName() method expects a final variable. A simple variable won't work.

    – Dibyanshu Jaiswal
    Dec 3 '15 at 6:01














6












6








6







You can use the forName method of Class:



Class cls = Class.forName(clsName);
Object obj = cls.newInstance();





share|improve this answer















You can use the forName method of Class:



Class cls = Class.forName(clsName);
Object obj = cls.newInstance();






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 16 '18 at 0:06









A1rPun

9,95554472




9,95554472










answered Mar 9 '10 at 12:07









rsprsp

20.3k44561




20.3k44561













  • I'm working with a Dynamic web project in eclipse. I am using above code to fetch a Class. but it returns ClassNotFoundException, when I use a string variable like this: String clsName = "com.mydoamin.className"; Class cls = Class.forName(clsName); If is hardcode the fully qualified class name as the parameter then things work fine. Any suggestion?

    – Dibyanshu Jaiswal
    Nov 25 '15 at 4:55













  • @djthequest If our clsName variable contains the fully qualified classname there should be no difference between using it and the hardcoded string. You might not have the class you need in the classpath. It might help to instantiate a common class from the same jar in your startup code to make sure the library is present in the classloader.

    – rsp
    Dec 1 '15 at 9:43











  • thanks for your response, but instead of the variable if I hardcode the string, works fins, it means the classes are in class path, right? Later i figured out, Class.forName() method expects a final variable. A simple variable won't work.

    – Dibyanshu Jaiswal
    Dec 3 '15 at 6:01



















  • I'm working with a Dynamic web project in eclipse. I am using above code to fetch a Class. but it returns ClassNotFoundException, when I use a string variable like this: String clsName = "com.mydoamin.className"; Class cls = Class.forName(clsName); If is hardcode the fully qualified class name as the parameter then things work fine. Any suggestion?

    – Dibyanshu Jaiswal
    Nov 25 '15 at 4:55













  • @djthequest If our clsName variable contains the fully qualified classname there should be no difference between using it and the hardcoded string. You might not have the class you need in the classpath. It might help to instantiate a common class from the same jar in your startup code to make sure the library is present in the classloader.

    – rsp
    Dec 1 '15 at 9:43











  • thanks for your response, but instead of the variable if I hardcode the string, works fins, it means the classes are in class path, right? Later i figured out, Class.forName() method expects a final variable. A simple variable won't work.

    – Dibyanshu Jaiswal
    Dec 3 '15 at 6:01

















I'm working with a Dynamic web project in eclipse. I am using above code to fetch a Class. but it returns ClassNotFoundException, when I use a string variable like this: String clsName = "com.mydoamin.className"; Class cls = Class.forName(clsName); If is hardcode the fully qualified class name as the parameter then things work fine. Any suggestion?

– Dibyanshu Jaiswal
Nov 25 '15 at 4:55







I'm working with a Dynamic web project in eclipse. I am using above code to fetch a Class. but it returns ClassNotFoundException, when I use a string variable like this: String clsName = "com.mydoamin.className"; Class cls = Class.forName(clsName); If is hardcode the fully qualified class name as the parameter then things work fine. Any suggestion?

– Dibyanshu Jaiswal
Nov 25 '15 at 4:55















@djthequest If our clsName variable contains the fully qualified classname there should be no difference between using it and the hardcoded string. You might not have the class you need in the classpath. It might help to instantiate a common class from the same jar in your startup code to make sure the library is present in the classloader.

– rsp
Dec 1 '15 at 9:43





@djthequest If our clsName variable contains the fully qualified classname there should be no difference between using it and the hardcoded string. You might not have the class you need in the classpath. It might help to instantiate a common class from the same jar in your startup code to make sure the library is present in the classloader.

– rsp
Dec 1 '15 at 9:43













thanks for your response, but instead of the variable if I hardcode the string, works fins, it means the classes are in class path, right? Later i figured out, Class.forName() method expects a final variable. A simple variable won't work.

– Dibyanshu Jaiswal
Dec 3 '15 at 6:01





thanks for your response, but instead of the variable if I hardcode the string, works fins, it means the classes are in class path, right? Later i figured out, Class.forName() method expects a final variable. A simple variable won't work.

– Dibyanshu Jaiswal
Dec 3 '15 at 6:01











4














You can get the Class reference of any class during run time through the Java Reflection Concept.



Check the Below Code. Explanation is given below



Here is one example that uses returned Class to create an instance of AClass:



package com.xyzws;
class AClass {
public AClass() {
System.out.println("AClass's Constructor");
}
static {
System.out.println("static block in AClass");
}
}
public class Program {
public static void main(String args) {
try {
System.out.println("The first time calls forName:");
Class c = Class.forName("com.xyzws.AClass");
AClass a = (AClass)c.newInstance();
System.out.println("The second time calls forName:");
Class c1 = Class.forName("com.xyzws.AClass");
} catch (ClassNotFoundException e) {
// ...
} catch (InstantiationException e) {
// ...
} catch (IllegalAccessException e) {
// ...
}
}
}


The printed output is



    The first time calls forName:
static block in AClass
AClass's Constructor
The second time calls forName:


The class has already been loaded so there is no second "static block in AClass"



The Explanation is below



Class.ForName is called to get a Class Object



By Using the Class Object we are creating the new instance of the Class.



Any doubts about this let me know






share|improve this answer


























  • what if class is in different project?

    – Steven
    Mar 9 '10 at 12:26






  • 1





    Can you get a jar file of that proect and specify the class name some thing like Class.forName("oracle.jdbc.driver.OracleDriver")

    – gmhk
    Mar 9 '10 at 13:30
















4














You can get the Class reference of any class during run time through the Java Reflection Concept.



Check the Below Code. Explanation is given below



Here is one example that uses returned Class to create an instance of AClass:



package com.xyzws;
class AClass {
public AClass() {
System.out.println("AClass's Constructor");
}
static {
System.out.println("static block in AClass");
}
}
public class Program {
public static void main(String args) {
try {
System.out.println("The first time calls forName:");
Class c = Class.forName("com.xyzws.AClass");
AClass a = (AClass)c.newInstance();
System.out.println("The second time calls forName:");
Class c1 = Class.forName("com.xyzws.AClass");
} catch (ClassNotFoundException e) {
// ...
} catch (InstantiationException e) {
// ...
} catch (IllegalAccessException e) {
// ...
}
}
}


The printed output is



    The first time calls forName:
static block in AClass
AClass's Constructor
The second time calls forName:


The class has already been loaded so there is no second "static block in AClass"



The Explanation is below



Class.ForName is called to get a Class Object



By Using the Class Object we are creating the new instance of the Class.



Any doubts about this let me know






share|improve this answer


























  • what if class is in different project?

    – Steven
    Mar 9 '10 at 12:26






  • 1





    Can you get a jar file of that proect and specify the class name some thing like Class.forName("oracle.jdbc.driver.OracleDriver")

    – gmhk
    Mar 9 '10 at 13:30














4












4








4







You can get the Class reference of any class during run time through the Java Reflection Concept.



Check the Below Code. Explanation is given below



Here is one example that uses returned Class to create an instance of AClass:



package com.xyzws;
class AClass {
public AClass() {
System.out.println("AClass's Constructor");
}
static {
System.out.println("static block in AClass");
}
}
public class Program {
public static void main(String args) {
try {
System.out.println("The first time calls forName:");
Class c = Class.forName("com.xyzws.AClass");
AClass a = (AClass)c.newInstance();
System.out.println("The second time calls forName:");
Class c1 = Class.forName("com.xyzws.AClass");
} catch (ClassNotFoundException e) {
// ...
} catch (InstantiationException e) {
// ...
} catch (IllegalAccessException e) {
// ...
}
}
}


The printed output is



    The first time calls forName:
static block in AClass
AClass's Constructor
The second time calls forName:


The class has already been loaded so there is no second "static block in AClass"



The Explanation is below



Class.ForName is called to get a Class Object



By Using the Class Object we are creating the new instance of the Class.



Any doubts about this let me know






share|improve this answer















You can get the Class reference of any class during run time through the Java Reflection Concept.



Check the Below Code. Explanation is given below



Here is one example that uses returned Class to create an instance of AClass:



package com.xyzws;
class AClass {
public AClass() {
System.out.println("AClass's Constructor");
}
static {
System.out.println("static block in AClass");
}
}
public class Program {
public static void main(String args) {
try {
System.out.println("The first time calls forName:");
Class c = Class.forName("com.xyzws.AClass");
AClass a = (AClass)c.newInstance();
System.out.println("The second time calls forName:");
Class c1 = Class.forName("com.xyzws.AClass");
} catch (ClassNotFoundException e) {
// ...
} catch (InstantiationException e) {
// ...
} catch (IllegalAccessException e) {
// ...
}
}
}


The printed output is



    The first time calls forName:
static block in AClass
AClass's Constructor
The second time calls forName:


The class has already been loaded so there is no second "static block in AClass"



The Explanation is below



Class.ForName is called to get a Class Object



By Using the Class Object we are creating the new instance of the Class.



Any doubts about this let me know







share|improve this answer














share|improve this answer



share|improve this answer








edited May 29 '18 at 22:53


























community wiki





2 revs, 2 users 76%
gmhk














  • what if class is in different project?

    – Steven
    Mar 9 '10 at 12:26






  • 1





    Can you get a jar file of that proect and specify the class name some thing like Class.forName("oracle.jdbc.driver.OracleDriver")

    – gmhk
    Mar 9 '10 at 13:30



















  • what if class is in different project?

    – Steven
    Mar 9 '10 at 12:26






  • 1





    Can you get a jar file of that proect and specify the class name some thing like Class.forName("oracle.jdbc.driver.OracleDriver")

    – gmhk
    Mar 9 '10 at 13:30

















what if class is in different project?

– Steven
Mar 9 '10 at 12:26





what if class is in different project?

– Steven
Mar 9 '10 at 12:26




1




1





Can you get a jar file of that proect and specify the class name some thing like Class.forName("oracle.jdbc.driver.OracleDriver")

– gmhk
Mar 9 '10 at 13:30





Can you get a jar file of that proect and specify the class name some thing like Class.forName("oracle.jdbc.driver.OracleDriver")

– gmhk
Mar 9 '10 at 13:30











3














eeh.. Class.forName(String classname) ?






share|improve this answer
























  • Can you also do this for Subclasses?

    – Gobliins
    Mar 20 '17 at 16:15
















3














eeh.. Class.forName(String classname) ?






share|improve this answer
























  • Can you also do this for Subclasses?

    – Gobliins
    Mar 20 '17 at 16:15














3












3








3







eeh.. Class.forName(String classname) ?






share|improve this answer













eeh.. Class.forName(String classname) ?







share|improve this answer












share|improve this answer



share|improve this answer










answered Mar 9 '10 at 12:06









Hans WesterbeekHans Westerbeek

4,21932833




4,21932833













  • Can you also do this for Subclasses?

    – Gobliins
    Mar 20 '17 at 16:15



















  • Can you also do this for Subclasses?

    – Gobliins
    Mar 20 '17 at 16:15

















Can you also do this for Subclasses?

– Gobliins
Mar 20 '17 at 16:15





Can you also do this for Subclasses?

– Gobliins
Mar 20 '17 at 16:15











2














Not sure what you are asking, but... Class.forname, maybe?






share|improve this answer
























  • its throwing ClassNotFound Exception

    – Steven
    Mar 9 '10 at 12:12











  • what if class is in different project?

    – Steven
    Mar 9 '10 at 12:26













  • I see that no one is answering your question. It shouldn't matter if it's in another project as long as the compiler knows where to look. Since, you're probably using an IDE, it probably depends solely on the IDE. The thing about references, you know that all of the classes that you reference from the Java libraries are not in your project, right? But, the compiler knows where to look for them because it knows that they're referenced externally. It seems that you need to learn basic Java programming when you're attempting to advanced Java programming. :|

    – user919860
    Jan 16 '13 at 17:11
















2














Not sure what you are asking, but... Class.forname, maybe?






share|improve this answer
























  • its throwing ClassNotFound Exception

    – Steven
    Mar 9 '10 at 12:12











  • what if class is in different project?

    – Steven
    Mar 9 '10 at 12:26













  • I see that no one is answering your question. It shouldn't matter if it's in another project as long as the compiler knows where to look. Since, you're probably using an IDE, it probably depends solely on the IDE. The thing about references, you know that all of the classes that you reference from the Java libraries are not in your project, right? But, the compiler knows where to look for them because it knows that they're referenced externally. It seems that you need to learn basic Java programming when you're attempting to advanced Java programming. :|

    – user919860
    Jan 16 '13 at 17:11














2












2








2







Not sure what you are asking, but... Class.forname, maybe?






share|improve this answer













Not sure what you are asking, but... Class.forname, maybe?







share|improve this answer












share|improve this answer



share|improve this answer










answered Mar 9 '10 at 12:06









Manrico CorazziManrico Corazzi

9,26884262




9,26884262













  • its throwing ClassNotFound Exception

    – Steven
    Mar 9 '10 at 12:12











  • what if class is in different project?

    – Steven
    Mar 9 '10 at 12:26













  • I see that no one is answering your question. It shouldn't matter if it's in another project as long as the compiler knows where to look. Since, you're probably using an IDE, it probably depends solely on the IDE. The thing about references, you know that all of the classes that you reference from the Java libraries are not in your project, right? But, the compiler knows where to look for them because it knows that they're referenced externally. It seems that you need to learn basic Java programming when you're attempting to advanced Java programming. :|

    – user919860
    Jan 16 '13 at 17:11



















  • its throwing ClassNotFound Exception

    – Steven
    Mar 9 '10 at 12:12











  • what if class is in different project?

    – Steven
    Mar 9 '10 at 12:26













  • I see that no one is answering your question. It shouldn't matter if it's in another project as long as the compiler knows where to look. Since, you're probably using an IDE, it probably depends solely on the IDE. The thing about references, you know that all of the classes that you reference from the Java libraries are not in your project, right? But, the compiler knows where to look for them because it knows that they're referenced externally. It seems that you need to learn basic Java programming when you're attempting to advanced Java programming. :|

    – user919860
    Jan 16 '13 at 17:11

















its throwing ClassNotFound Exception

– Steven
Mar 9 '10 at 12:12





its throwing ClassNotFound Exception

– Steven
Mar 9 '10 at 12:12













what if class is in different project?

– Steven
Mar 9 '10 at 12:26







what if class is in different project?

– Steven
Mar 9 '10 at 12:26















I see that no one is answering your question. It shouldn't matter if it's in another project as long as the compiler knows where to look. Since, you're probably using an IDE, it probably depends solely on the IDE. The thing about references, you know that all of the classes that you reference from the Java libraries are not in your project, right? But, the compiler knows where to look for them because it knows that they're referenced externally. It seems that you need to learn basic Java programming when you're attempting to advanced Java programming. :|

– user919860
Jan 16 '13 at 17:11





I see that no one is answering your question. It shouldn't matter if it's in another project as long as the compiler knows where to look. Since, you're probably using an IDE, it probably depends solely on the IDE. The thing about references, you know that all of the classes that you reference from the Java libraries are not in your project, right? But, the compiler knows where to look for them because it knows that they're referenced externally. It seems that you need to learn basic Java programming when you're attempting to advanced Java programming. :|

– user919860
Jan 16 '13 at 17:11


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f2408789%2fgetting-class-type-from-string%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Bressuire

Vorschmack

Quarantine