Classload.load and Class.forName question

I know there are several threads about this, but I have a doubt.
I'm getting a ClassNotFoundException running the JUnitSampler of JMeter.
This test is run by maven (using a specific plugin for it). This plugin creates a new Thread to call JMeter with all the parameters that needs.
The class that the JUnitSampler is trying to run is in the classpath (is not jar).
I know this because I run maven with -X parameter and it shows this:
[DEBUG] Test Classpath :
[DEBUG]   /path/to/project/target/classes
[DEBUG]   /path/to/project/target/test-classesLooking at JUnitSampler code I can see this
theclazz = Thread.currentThread().getContextClassLoader().loadClass(className.trim());Is there any reason that the class couldn't be found?

No idea - it is your code.It's JMeter code, not mine.You said it couldn't find your test classes.
>
My original question was about if there were any
difference in using
Class.forName
or
Thread.currentThread().getContextClassLoader().loadClass
No idea. You can print the class loader though.
That was because the JUnitSampler is using the second
option to instantiate the test classes, and
I was receiving a ClassNotFoundException. That
problem is now solved, because it can find the class,
but is unable to cast it to TestCase.
I want to know if there is a way to put my test
classes in the ClassLoader that JMeter is using.You have an instance of some class.
To create that class it was loaded by a class loader.
You can get that class loader via the methods of java.lang.Class.
You can use that class loader to load your class.
That might or might not solve your problem.
Because there are any number of ways it can fail to solve you problem. Below is one, one of many, that might do it.
It won't solve it if you have already loaded your class via the system loaders which will happen if you use absolutely any reference to it.

Similar Messages

  • What is the Difference between ?  new Operator and Class.forName() ???

    plz tel me ....
    what is the Difference between ? new Operator and Class.forName() ??? ........

    Class.forName(), takes the class name as parameter,
    and loads that class in memory. But it doesn't create
    any instance of that class.
    That means static methods/variables are available for
    use.
    new keyword, checks if the class is loaded, if not
    then loads that class, and then creates an instance
    of that class.Class.forName actually returns the class object for that name (class - for -name). it might load it, if the class hasn't already been loaded, but it's misleading to say that's what that method does
    your definition of 'new' is wrong, too. give the dukes back

  • Class.forName question

    Hello,
    I am receiving a ClassNotFoundException on a class in my application during a Class.forName call and I have no idea why. The class is present at the correct path, all words are spelled correctly, and all case-sensitivity is correct. If I even try to instantiate the object which the class represents in my code, it works fine.
    I actually realized that there are two separate java files in my workspace, with the same path and same class name. One just happens to be in a bundled JAR file in WEB-INF and the other is in my code base. Would these duplicate classes cause this CNFE? I guess I'm just a little in the dark over how the method works.

    Yes, that's right. Jars in the server run in a classloader which doesn't have access to any of the web applications that happen to be on the server.
    One way to avoid this problem is to not put your application's jar files in the server's classpath. Put them all in the WEB-INF/lib directory. If this involves putting copies of the jar file in several applications, then so be it.
    Or some servers have a configuration option allowing the application's class loader to be "parent first". Sometimes this helps that sort of thing. But basically you're wrestling with a pig here so don't expect to come out of the experience clean.

  • Difference between DriverManager.registerDriver and Class.forName?

    Hi all,
    I've noticed that the Oracle JDBC driver specifically asks to use DriverManager.registerDriver instead of Class.forName. What is the difference btw those 2 methods in loading and registering the driver?
    Thanks.

    I am trying to understand the jdbc, odbc drivers better.
    Does the class my.sql.Driver refer to a driver for MySql or is it a driver itself?
    with the lines one and two commented out, the code works fine. but with lines one and three commented out and the dbDriver being as specified in line two; no connection is made to the database. Can anyone explain this? and when i do Class.forName(dbDriver), is there a class by that name?
    //String dbDriver = "sun.jdbc.odbc.JdbcOdbcDriver"; //one
    //String dbDriver = "my.sql.Driver"; //two
    String dbDriver = "MySql ODBC 3.51 Driver"; //three
    String dbSource = "jdbc:odbc:mydb";
    try {
         Class.forName(dbDriver);
         conn = DriverManager.getConnection(dbSource);
         statement = conn.createStatement();
    } catch(Exception exception) {
         System.out.println(exception.getMessage()+" from the "+exception.toString());
         System.exit(1);

  • Trouble w/ multiple applets dynamically starting and Class.forName()

    I'm trying to start applets dynamically and I see strange behavior on some systems it works fine and gets the applet class from the jar file. On other systems it makes an http request back to the server to try and get the class. I don't know where in the Class or ClassLoader it's deciding whether to get this class from the jarfile or from the server and all of these classes are contained in the jarfile? Any suggestions?
    Here's the method to start new applets
    public Component startApplet( String className ) {
    System.out.println("Starting applet "+className);
    Class appletClass = null;
    JApplet japplet = null;
    try {
    appletClass = Class.forName("com.actsolar.ui.applet."+className);
    System.out.println(appletClass);
    japplet = (JApplet)appletClass.newInstance();
    japplet.init();
    } catch (Exception e) {
    System.out.println("startapplet Exception: "+e.getMessage());
    return japplet;
    thanks,
    andrew

    Hi,
    I have seen the same problem when I mix up small letters and BIG LETTERS.
    Ex :
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
    <applet code="Function3.class"width="100" height="100">
    </applet>
    </html>
    will work with Function3.class
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
    <applet code="function3.class"width="100" height="100">
    </applet>
    </html>
    Will NOT work.
    Maybe this will help you , I am also new on JAVA so maybe you make the same mistake like I did ?
    Best Regards,
    Didier.

  • Difference between "new classname()" and  class.forName("...")?

    Hi,
    Please clarify me : What is the difference between the instnce creation by using new operator *( new ClassName())* and instance creation using class.forName("fully Qualified Name");
    thanks
    Gaurav

    The compiler can tell you whether new ClassName() will be valid. For the other, you have to wait until runtime to know for sure.
    new ClassName(...) can be invoked with arguments, and you provide them directly and simply. If you want to reflectively create an instance using a constructor that takes arguments, it's a more convoluted process.
    Using new, you know which checked exceptions can be thrown and you catch them directly. Using reflection, you have to catch some other exception and unravel its cause.
    You can use reflection (Class.forName and its more complicated cousins) when you don't know the classname at compile time.You can't do this with new.
    Don't use reflection unless you have to. It's more complex, somewhat less performant, and it denies you certain compile time checks.

  • Tomcat3 and Class.forName(my_class)

    Hi All,
    I am getting a ClassNotFoundException when I try to do a Class.forName(com.iw.command.my_class) in my servlet under Tomcat 3.3.1. I am running on RH Linux 7.2. The class is in my classes folder under WEB-INF. In the the tomcat config docs, it states that WEB-INF/classes is included in the tomcat classpath by default. Any help is greatly appreciated. I am under the gun and need to get this working ASAP.
    Thanks,
    Justin

    Forget it! I must have been visited by the idiot fairy while coding on late night.
    My method call was ... Class.forName("cmdname") when it should have been ...
    Class.forName(cmdname)
    Luckily I haven't spent much time on this.

  • Weblogic.policy and class.forname

    Hi,
    my company develops an application for an external customer (bea 6.1). In
    our cutomers eyes the application is a security risk because our customer is
    afraid that we are using the class.forname command to create manually
    connections to other databases.
    I would like to know, is it possible to disable this command for our
    application with the help of the weblogic.policy file?
    Regards
    Michael

    yes.. i think it returns Class object. But, isthere
    any other way to instantiate a class other thanusing
    the new operatorOthers
    - JNI, similar but not exactly new
    - Serialization which by passes the normal processCloning
    Class.newInstance has been mentioned, but Constructor.newInstance has not

  • What is diff. b/w Class loading and class initializing??

    Hi Guys,
    When we are using final keyword as:
    final static int cout;
    Then class will load but not initialize?
    Could any one explain this??
    Thanks in advance.

    >
    When we are using final keyword as:
    final static int cout;
    Then class will load but not initialize?
    Could any one explain this??
    >
    The class will load but not initialize? Where did you get that from?
    See 4.12.4. final Variables in the Java Language Spec
    http://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html#jls-4.12.4
    >
    A blank final is a final variable whose declaration lacks an initializer.

  • Dinamyc class loading and jar files

    I'm new to java. I would like to load some plugins in my application (it's going to be packaged as a jar at the end). I managed to find some dinamyc class loading examples but they search for the classes to load in a directory.
    This is the code:
        public static void main(String[] args) {
            File pluginDirectory = new File("src/pluginsreloaded/plugins");
            if (!pluginDirectory.exists()) {            // the plugin directory does not exist
                System.out.println("The plugins directory does not exist!");           
                return;
            FilenameFilter filter = new FilenameFilter() {
                public boolean accept(File dir, String name) {
                    return name.endsWith(".class");
            String[] pluginFiles = pluginDirectory.list(filter);
            for (int i = 0; i < pluginFiles.length; i++) {
                if (pluginFiles.indexOf("$") == -1) {
    System.out.println("Loading: " + pluginFiles[i].substring(0, pluginFiles[i].length() - 6));
    IPlugin plugin = pm.loadPlugin(pluginFiles[i].substring(0, pluginFiles[i].length() - 6));
    System.out.println(plugin.description());
    protected static IPlugin loadPlugin(String name) {
            // Query the plugin list for the plugin
            PluginFactory _plugin = (PluginFactory) pluginList.get(name);
            if (_plugin == null) {          // the plugin is not loaded
                try {
                    Class.forName("pluginsReloaded.plugins." + name);
                    // The plugin makes an entry in the plugin list
                    // when loaded
                    _plugin = (PluginFactory) pluginList.get(name);
                    if (_plugin == null) {
                        return null;
                } catch (ClassNotFoundException e) {
                    System.out.println("Plugin " + name + " not found!");
            return _plugin.create();
        }IPlugin is an interface. I am using netbeans 5.0. The error I get is this:
    Exception in thread "main" java.lang.NoClassDefFoundError: pluginsReloaded/plugins/plugin1 (wrong name: pluginsreloaded/plugins/plugin1)
            at java.lang.ClassLoader.defineClass1(Native Method)
            at java.lang.ClassLoader.defineClass(ClassLoader.java:620)
            at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:124)
            at java.net.URLClassLoader.defineClass(URLClassLoader.java:260)
            at java.net.URLClassLoader.access$100(URLClassLoader.java:56)
            at java.net.URLClassLoader$1.run(URLClassLoader.java:195)
            at java.security.AccessController.doPrivileged(Native Method)
            at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
            at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
            at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:268)
            at java.lang.ClassLoader.loadClass(ClassLoader.java:251)
            at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:319)
            at java.lang.Class.forName0(Native Method)
            at java.lang.Class.forName(Class.java:164)
            at pluginsreloaded.PluginManager.loadPlugin(PluginManager.java:30)
            at pluginsreloaded.Main.main(Main.java:44)
    Java Result: 1As far as I can see it can't find the class. My question is how can I load the class/where can I find it?
    Thanks.

    You can use the java.util.jar.JarFile class to enumerate the contents of a jar. It's not good practice to search for plugins in this way though. A plugin may well involve serveral classes, which don't all have to be internal. Furthermore its wise to avoid any comitment about the mechaism by which class files are to be fetched. You might want to do it remotely, some time, or load them from a database.
    What seesms to be the standard approach is to put a list of plugin classes into the jar as a text file.
    Plugins for a given purpose usually implement some specified interface, or extend some abstract class to which their objects can be cast once loaded (without this they aren't really much use).
    Say your plugins implment org.myorg.WidgetFactory then you put a list of them, one fully qualified name to a line, in a file or jar entry called META-INF/services/org.myorg.WidgetFactory. You framework picks up all such files from the classpath using ClassLoader.getResources() and loads all the classes whose names if finds, hence you can add new sets of plugins just by adding a new jar or class directory to the class path.

  • Detecting static calls in methods via Class.forName(...)

    Hi!
    I'm trying to detect, if a method (or a contructor) of a Class object, loaded by Class.forName(...) contains a static call like System.out.println( "some text" );
    I need it to figure out if (in this case above) the class "System" is needed for running this example class.
    I don't have the source code (and don't want it anyway) to write a parser for it. I just want to extract this single information: Is there a method/contructor in the given class containing static calls by any other classes and what's the the of these classes?
    Anybody an idea?
    Thoto

    I'm trying to detect, if a method (or acontructor)
    of a Class object, loaded by Class.forName(...)
    contains a static call like System.out.println("some
    text" );BCEL should be able to do this.But I need to do that in my own program.
    System.out.println() is not a static method.That's right. If it's static or not doesn't really matter, it's just the information WHICH class is embedded into this call. It could also be a call like
    int i = Integer.MAX_VALUE.I just need (in that case) the informatation that the class "Integer" is needed to proceed that call.
    You can use javap to find this, or the BCEL library.I already did that with javap. But it's useless for me, I need it in my own program. I don't want to decompile some program. I have no illegal intentions in hacking or so. It's for my own programs, but I don't want to scan the source code.
    Unless this is homework, why would you want to know
    this information?That sounds, like you know the answer, but can't tell me, because it's the most important treasure in the world and THEY will kill you if you tell others... :-)
    No, it's not a homework and I don't want any source code from you, I'm a curious guy.

  • Why Class.forName("driver class") ?

    Hi,
    whay we are using Class.forName("driver class"). why we dont import the driver package and access the required classes and methods?
    for eg:
    insead of calling
    Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver"); ,can we use import com.microsoft.jdbc.sqlserver.*; ?
    what is the use of DriverManager in this context? any connection between DriverManager and Class.forName() ?
    expecting answers.

    sure you can import the packages and use the classes directly but that defeats the whole purpose of pluggable drivers.
    Class.forName causes the class to be loaded and as stated before that should cause the driver to register itself with the driver manager.
    so then if you have a system where you are talking to several different database vendors then you don't have to worry about which is which as the DriverManager handles it for you.
    the big pros that i have actually seen though for having this be pluggable and not hard-coded (with the exception of the URL although you can do this dynamically as well) is what happens if you switch databases. if you import the package and use fully named classes and you switch databases at some point (could be just an upgrade of the same vendor's database) you are screwed.
    same problem could happen if you switch drivers for some reason.
    at any rate if you use things properly and are careful then you should only ever have to change your URL the rest of the code is fine if you use the DriverManager and use the drivers dynamically otherwise...

  • Can Class.forName("SomeClassName").newInstance() work in all classes?

    I saw some classes don't have a method called "newInstance()".
    but I have seen some advices say that is better using "Class.forName().newInstance()" than using "Class.forName()".

    I saw some classes don't have a method called
    "newInstance()".If you have class Foo, and you do Class.forName("Foo").newInstance();you're not calling Foo's newInstance method. You're calling java.lang.Class' newInstance method. You have a Class object that represents the Foo class. You tell that Class object newInstance() which invokes the public no-arg constructor of class Foo (as if you had done new Foo()). If Foo doesn't have a public no-arg constructor, you'll get an exception.
    but I have seen some advices say that is better using
    "Class.forName().newInstance()" than using
    "Class.forName()".They do two completely different things, and Class.forName().newInstance() first does Class.forName() and then class newInstance() on what that returns.
    Class.forName("Foo") returns the Class object that is associated with class Foo. It also loads the class if it hasn't already been loaded. It returns an instance of java.lang.class
    newInstance() calls the public, no-arg ctor of that class, if that ctor exists. Otherwise it throws an exeption. If it succeeds, it returns an instance of Foo.

  • Class load and instance memory footprint questions

    Lets say I have CLASS_A that has three members:
    private int a;
    private int b;
    private int c;
    but lets say Class_A has 32 methods manipulating the state of the object.
    For argument sake lets say when CLASS_A is compiled this class (Bytecode) file comes out to be 32Kilobytes.
    Now when my compiler has to load this class it loads the whole 32 kilo class file. but for subsequent instances of CLASS_A will the footprint for each instance be 32 Kilobytes or will it be the 12 (4X3)bytes that are needed to represent the member variables (+ some more bytes to refer to classname or something)?
    I guess my question is does each instance take up 32 Kilobytes?
    Thanks for all replies !

    No, each instance (within the same JVM process) will take up the amount that the instance variables take up, not that plus the instance methods. The implementation code won't get loaded multiple times.

  • Dynamic class loading and Casting

    Hi guys,
    spent lots of time trying to figure out how to do one thing with no luck, hope anybody can help me here. Here is what I have:
    I need to create a new object, I have dynamic variable of what kind of an object I need:
    sObjectType = "Article";
    ItemObject oItem = Item.init(1000, 1);           
    oItem.ArticleObjectCall();                      // ERROR is here, method does not exist, ArticleObjectCall is method of the Article classItem is a static Class that inits objects
    public class Item {
         public static ItemObject init(String sObjectType) {
                  ItemObject oClass = Class.forName("com.type." + sObjectType).newInstance();
                  return oClass;
    }Below are the 2 classes Article and ItemObject
    Article
    package com.type;
    public class Article extends ItemObject {     
         public void ArticleObjectCall() {
              System.out.println("Article Hello");
    ItemObject
    public class ItemObject {
         public ItemObject() {          
    }

    Ajaxian wrote:
    This is a method INIT, I am dynamically including class com.type.Article , Article extends ItemObject, I am casting new instance of Article to (ItemObject) to return a proper type but later when I try to call oClass object which is I believe my Article class, I get an error. None of which answers my question, yet it does show that you are thoroughly confused...
    String x = "blub";
    Object y = x;
    System.out.println(y.length); // We both know y is a string, but the compiler does not. => ErrorThe compiler enforces the rules of a static type system, your question is quite explicitly about dynamism, which is not without reason the antonym to statics. If you load classes dynamically, you need to use reflection to invoke their methods.
    With kind regards
    Ben

Maybe you are looking for

  • How to create different users in the same iPad 4?

    I would like to share my new iPad with other person. How can I create two users.?

  • Frozen image with video out to HDMI

    I have my iMac connected via an HDMI adaptor through a 10' HDMI cable into a Hi Def TV. It was working well but suddenly froze with an image that happened to be a photo from iPhoto. That image remains whenever the cable is connected into the TV. When

  • Creation of Excel files using XSLT

    Hi frnds, From the blog provided by the Michal /people/michal.krawczyk2/blog/2005/12/10/xi-generating-excel-files-without-the-java-nor-the-conversion-agent-not-possible I came to know that we can create excel files using XSLT mapping but here inthis

  • BO Enterprise 3.1 SR3 Installation with existing Oracle database

    Hi, I'm completely new to Business Objects and have been reviewing the installation & planning guides for the installation. I'm hoping to use an existing Oracle 11.2g installation as the location for the CMS / Audit. The guides don't really explain i

  • How do I update status on a discussion?

    Sorry, probably a stupid question but looked everywhere - I received 3 emails telling me to update my status and can't find it anywhere - can you please let me know how / where to change so I won't get the emails and why am I getting 3 emails? Thanks