|
-
October 19th, 2009, 11:26 PM
#1
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?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|