Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Class.forName is giving ClassNotFound Exception

Tags:

java

I am using the following code ::

String className  = "SmsHelper"
Class c = Class.forName(className); 

And I am getting the following

stackTrace ::

 inside main java.lang.ClassNotFoundException: SmsURLHelper
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Unknown Source)
    at com.tcs.sms.actions.SmsUtil.invoke(SmsUtil.java:131)

Note : The SmsHelper class is present.

Please suggest if I have missed any thing.

like image 628
Sashi Kant Avatar asked Sep 27 '12 12:09

Sashi Kant


People also ask

What is class forName ()?

forName(String name, boolean initialize, ClassLoaderClassLoaderThe Java Class Loader is a part of the Java Runtime Environment that dynamically loads Java classes into the Java Virtual Machine. Usually classes are only loaded on demand. The Java run time system does not need to know about files and file systems as this is delegated to the class loader.https://en.wikipedia.org › wiki › Java_ClassloaderJava Classloader - Wikipedia loader) Returns the Class object associated with the class or interface with the given string name, using the given class loader.

How do I load a class using forName?

Load class with forName() method in Java The class object associated with the class with the given string name can be returned with the method java. lang. Class. forName(String name, boolean initialize, ClassLoaderClassLoaderThe Java Class Loader is a part of the Java Runtime Environment that dynamically loads Java classes into the Java Virtual Machine. Usually classes are only loaded on demand. The Java run time system does not need to know about files and file systems as this is delegated to the class loader.https://en.wikipedia.org › wiki › Java_ClassloaderJava Classloader - Wikipedia loader), using the class loader that is used to load the class.

How do I find the class forName?

Class forName() method in Java with Examples This class name is specified as the string parameter. Parameter: This method accepts the parameter className which is the Class for which its instance is required. Return Value: This method returns the instance of this Class with the specified class name.


2 Answers

Use the fully qualified name.

Example:

Class.forName("com.yourownpackage.SmsHelper"); 
like image 109
Mik378 Avatar answered Oct 20 '22 22:10

Mik378


Please write the Class name along with its package name(in which your class resides) as a String in the forName method. Here is the sample to use:

String className  = "com.testpackage.SmsHelper";
Class c = Class.forName(className); 
like image 4
Arun Kumar Avatar answered Oct 20 '22 22:10

Arun Kumar