Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: I dont understand why this happens....

  1. #1
    Junior Member
    Join Date
    May 2011
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default I dont understand why this happens....

    OK here's the situation, when this code is executed from an IDE (netbeans) It functions flawlessly. However once it is compiled into a .class and thrown into a jar it fails at the line classList.add(cl.loadClass("DocDoom")); class path is set to a accommodate the independent .class files it wants to load. DocDoom.class exists within that directory.

    public static boolean loadRecipes() {
                try {
                ArrayList<Class> classList = new ArrayList<Class>();
                ArrayList<String> classes = new ArrayList<String>();
                File dir = new File("C:\\Users\\Akira\\Desktop\\New folder (2)\\Mods\\Recipes");
                ClassLoader cl = new URLClassLoader(new URL[]{dir.toURI().toURL()});
                    try {
                        logger.printMessage("1");
                        classList.add(cl.loadClass("DocDoom"));
                        logger.printMessage("2");
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
     
                System.out.println(classList.toString());
            } catch (MalformedURLException ex) {
                ex.printStackTrace();
            }
                return true;
        }

    when executed from netbeans the logger pushes out a 1 and a 2. from a jar the logger only pushes out a 1 (There is NO error in the logger)
    Last edited by ashenwolf; May 10th, 2011 at 02:53 PM. Reason: Typos


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: I dont understand why this happens....

    Is an exception being thrown? If so, what is it? (you mention no error in the logger, but depending upon where your logger writes to you might miss the exception as if one is thrown it isn't printed to the logger - not sure what library you use for logging but there should be an error method allowing you to pass the exception to the logger) How are you running the app? Applet, webstart, or standalone? If its one of the first two you will need to sign the jar.

  3. #3
    Junior Member
    Join Date
    May 2011
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: I dont understand why this happens....

    It throws an exception NoClassDefFound for DocDoom's super class, which was compiled with it (Lets call it Foo). They only way i can bypass the error is to put a copy of Foo in the same directory as DocDoom, but then when i cast a .newinstance() of the class DocDoom to Foo it says that Class DocDoom cant be cast to Foo...

  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: I dont understand why this happens....

    Foo should be in the same location, as should any other references you make to classes you've written - make sure any other classes that you've written that 'Foo' or DocDoom reference are on the classpath (packaging them as a jar inside their respective packages might help as well).

  5. #5
    Junior Member
    Join Date
    May 2011
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: I dont understand why this happens....

    Perhaps I'm going about this all wrong. I have been banging my head against this problem for several days now (nearing a week). Perhaps there is a better way and I am just not seeing it.

    Let me explain in full what it is I'm trying to do.

    In short is modular class loading at runtime. I write Foo.class and Bar.class Foo extends Bar. I am trying to write a program that knows what the definition of Foo is and goes to to a directory (entered at runtime) that loads all .class files there (assuming) that they are subtypes of Bar. Generate a new instance of each of these classes and cast them to Foo and use them within the program.

    These are the 5 classes i am using

    public class Main {
        public static void main (String args[]){
            Why w = new Why();
        }
    }

    public class Foo extends Bar{
        public Foo(){
            super();
        }
     
        @Override
        public void hitMe(Why w) {
            w.out("FROM FOO");
        }    
    }

    public class Bar {
        public Bar(){
     
        }
        public void hitMe(Why w){
            w.out("FROM BAR");
        }
    }
    import java.io.File;
    import java.net.MalformedURLException;
    import java.net.URL;
    import java.net.URLClassLoader;
    import java.util.ArrayList;
     
    public class TestProject {
     
        public static boolean execute(Why w){
            try {
                ArrayList<Class> classList = new ArrayList<Class>();
                ArrayList<String> classes = new ArrayList<String>();
                File dir = new File("C:\\Users\\Akira\\Desktop\\Target");
                ClassLoader cl = new URLClassLoader(new URL[]{dir.toURI().toURL()});
                    try {
                        classList.add(cl.loadClass("Foo"));
                        System.err.println(classList.size());
                        Bar r = (Bar) classList.get(0).newInstance();
                        r.hitMe(w);
                        return true;
                    } catch (Exception ex) {
                        ex.printStackTrace();
                    }
                System.out.println(classList.toString());
            } catch (MalformedURLException ex) {
                ex.printStackTrace();
            }
            return false;
        }
    }

    public class Why {
     
        /**
         * @param args the command line arguments
         */
        public Why(){
            boolean execute = TestProject.execute(this);
            System.out.println(execute);
        }
     
        public void out(String string) {
            System.err.println(string);
        }
     
    }

    Once the code is compiled into .class files i cut the Foo.class and paste it into the target directory. I then get

    java.lang.ClassDefNotFoundException: Bar
    at java.net.URLClassLoader$1.run(URLClassLoader.java: 202)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.j ava:190)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:3 06)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:2 47)
    at TestProject.execute(TestProject.java:25)
    at Why.<init>(Why.java:16)
    at Main.main(Main.java:12)

    {EDIT}: If i move Bar.class to the same target folder as Foo.class, it will not throw the ClassDeffNotFoundException, however it throws a ClassCastExcception when attempting to cast Foo to Bar.
    Last edited by ashenwolf; May 11th, 2011 at 12:54 AM.

Similar Threads

  1. Can't understand why Interfaces are there
    By vortexnl in forum Object Oriented Programming
    Replies: 9
    Last Post: February 14th, 2011, 01:06 PM
  2. Can't seem to understand why my while loop doesn't exit!
    By vortexnl in forum Loops & Control Statements
    Replies: 6
    Last Post: February 11th, 2011, 03:43 PM
  3. I can''t understand this error
    By ragingdemon in forum What's Wrong With My Code?
    Replies: 8
    Last Post: January 8th, 2011, 06:07 PM
  4. 2 errors I can't understand
    By Brock in forum What's Wrong With My Code?
    Replies: 8
    Last Post: December 27th, 2010, 12:56 AM
  5. Dont understand what to write.. :/
    By johnwalker in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 4th, 2010, 09:31 PM