7 Replies Latest reply: Feb 19, 2010 11:45 PM by 843798 RSS

    ClassNotFoundException??

    843798
      Hello there,
      I am trying to develop a small application which uses reflections to find methods and fields from a class.
      I have added this directory to classpath also.
      I tried using
                              String file="com.uni.project.employee.EmployeeInfo";
                     //pojoClass=Class.forName(file);
                     //pojoClass=ClassLoader.getSystemClassLoader().loadClass(file);
                     //pojoClass=Launcher.getLauncher().getClassLoader().loadClass(file);
                     URLClassLoader loader=new URLClassLoader(urls);
                     pojoClass=loader.loadClass(file);
      but none of the method is working
      and following exception is thrown every time
      java.lang.ClassNotFoundException: com.uni.project.employee.EmployeeInfo
           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 java.lang.ClassLoader.loadClass(Unknown Source)
           at com.sachin.PojoClass.<init>(PojoClass.java:27)
           at com.sachin.ProjVal.initiateProjectLoading(ProjVal.java:81)
           at com.sachin.ProjVal.actionPerformed(ProjVal.java:53)
           at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
           at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
           at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
           at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
           at javax.swing.AbstractButton.doClick(Unknown Source)
           at javax.swing.plaf.basic.BasicMenuItemUI.doClick(Unknown Source)
           at javax.swing.plaf.basic.BasicMenuItemUI$Handler.mouseReleased(Unknown Source)
           at java.awt.Component.processMouseEvent(Unknown Source)
           at javax.swing.JComponent.processMouseEvent(Unknown Source)
           at java.awt.Component.processEvent(Unknown Source)
           at java.awt.Container.processEvent(Unknown Source)
           at java.awt.Component.dispatchEventImpl(Unknown Source)
           at java.awt.Container.dispatchEventImpl(Unknown Source)
           at java.awt.Component.dispatchEvent(Unknown Source)
           at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
           at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
           at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
           at java.awt.Container.dispatchEventImpl(Unknown Source)
           at java.awt.Window.dispatchEventImpl(Unknown Source)
           at java.awt.Component.dispatchEvent(Unknown Source)
           at java.awt.EventQueue.dispatchEvent(Unknown Source)
           at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
           at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
           at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
           at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
           at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
           at java.awt.EventDispatchThread.run(Unknown Source)
        • 1. Re: ClassNotFoundException??
          DrClap
          Usually that just means the class wasn't in the classpath. But in this case it just means the class wasn't in any of the URLs which that URLClassLoader was given. That list works just like the classpath.
          • 2. Re: ClassNotFoundException??
            jschellSomeoneStoleMyAlias
            I have added this directory to classpath also.
            Do not do that when you are using a custom class loader. It can confuse whether the code is working or not. (Advice - previous reply explained your problem.)
            • 3. Re: ClassNotFoundException??
              843798
              While using URLClassLoader do we have to give complete physical path where class is located
              for example my class is at
              C:\Java_Projects\proj\com\sachin\proj\MyClass.class
              and the class has package as
              package com.sachin.proj;
              so which is the right way of using URLClassLoader?

              this -
              URL urls[]={
              
               new URL("file://c:/Java_Projects/proj/com/sachin/proj")
              
              };
              
              URLClassLoader loader=new URLClassLoader(urls);
              Class pojoClass=loader.loadClass("MyClass");
              or this -
              URL urls[]={
              
               new URL("file://c:/Java_Projects/proj")
              
              };
              
              URLClassLoader loader=new URLClassLoader(urls);
              Class pojoClass=loader.loadClass("com.sachin.proj.MyClass");
              If someone can give me an example of how to load a class which is not in classpath, that would be so great.
              • 4. Re: ClassNotFoundException??
                jtahlborn
                or this -
                URL urls[]={
                
                new URL("file://c:/Java_Projects/proj")
                
                };
                
                URLClassLoader loader=new URLClassLoader(urls);
                Class pojoClass=loader.loadClass("com.sachin.proj.MyClass");
                this is what you want.
                • 5. Re: ClassNotFoundException??
                  843798
                  File file = new File("c:\\Java_projects\prog\\");

                  URL URL url = file.toURL();
                  URL[] urls = new URL[]url;
                  ClassLoader cl = new URLClassLoader(urls);
                  Class cls = cl.loadClass("com.sachin.project.MyClass");
                  • 6. Re: ClassNotFoundException??
                    EJP
                    File file = new File("c:\\Java_projects\prog\\");
                    That won't compile. Anyway there is never a need to use a backslash in a filename in Java.

                    File file = new File("c:/Java_projects/prog/");

                    Items in a classpath are either .jar files or directories that are the head of a package subtree. So anything that's part of package name shouldn't appear in a classpath.
                    • 7. Re: ClassNotFoundException??
                      843798
                      Thank you for you replies.
                      My problem has been solved and now I can use URLClassLoader to load the classes which are not there in classpath.

                      Thank you all.