CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Sep 2009
    Posts
    11

    Class.forName(...,...,...) isnt finding my .class files

    Hello.
    Im using Class.forName to find .class files within my jar file, but its complaining with a ClassNotFoundException. I cant tell why this is happening because its displaying in my output that the .class files exist, but they arent ... "found"? Is it because forName requires just the name of the class, without the ".class" appended? I tried doing this too and it came back with a error saying:

    java.lang.NoClassDefFoundError: Ma (wrong name: slider/Ma)
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClass(Unknown Source)
    at java.security.SecureClassLoader.defineClass(Unknown Source)
    at java.net.URLClassLoader.defineClass(Unknown Source)
    at java.net.URLClassLoader.access$000(Unknown Source)
    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.net.FactoryURLClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClassInternal(Unknown Source)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Unknown Source)
    at slider.Loader.getPuzzleNames(Loader.java:149)
    at slider.Loader.main(Loader.java:165)

    Anyways, heres some code dealing with the matter, does anyone know whats happening here?

    ------ global variables -----
    ClassLoader loader;
    JarURLConnection conn;
    JarFile jar;

    --------methods-----------

    /**
    * Returns the names of all puzzles available.
    */
    public Set<String> getPuzzleNames()
    {
    TreeSet<String> set = new TreeSet<String>();
    Enumeration<JarEntry> iter = jar.entries();
    while( iter.hasMoreElements() )
    {
    JarEntry entry = iter.nextElement();
    String name = entry.getName();
    Pattern p = Pattern.compile("(.)+\\.class$");
    Matcher m = p.matcher( name );
    if( m.matches() )
    {
    String[] arr = name.split("\\.");
    try{
    Class<?> c = Class.forName(name, true, loader); <-- this is where it says ClassNotFoundException
    if( Puzzle.class.isAssignableFrom(c))
    {
    set.add( name );
    }
    }catch(ClassNotFoundException e ){
    System.out.println("Class not found for " + name );
    }
    }
    }
    return set;
    }

    Then my output looks like :
    Class not found for Ma.class
    Class not found for CharBoard.class
    Set of names: []

    However when i change my forName statement to say
    Class<?> c = Class.forName(arr[0], true, loader);
    That is when it gives me that NoClassDefFoundError Error.

    when it should say....
    Set of names:[Ma.class, CharBoard.class] ... well techinically only [Ma.class] because Charboard doesnt implement a Puzzle... but yeah, I hope you get the point.

    Thank you everyone, let me know if you need more info.
    P.S. - how do you post code in these forums "properly", so it indents and what not?

  2. #2
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: Class.forName(...,...,...) isnt finding my .class files

    The classloader 'loader' doesn't seem to be initialised, which won't help. You need to use the fully qualified class name, e.g. "foo.bar.MyClass". You need to be sure the .class file for the class you want is present and correct.

    Other than that, I suggest you strip out all the irrelevant stuff and write a little test to try and load your class, and print out the class name you're using. When you've got that working, you can plug it back into the main code.

    P.S. - how do you post code in these forums "properly", so it indents and what not?
    See my sig.

    It is better to have an approximate answer to the right question than an exact answer to the wrong one...
    J. Tukey
    Please use &#91;CODE]...your code here...&#91;/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured