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.
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.
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.
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.
Use the fully qualified name.
Example:
Class.forName("com.yourownpackage.SmsHelper");
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With