Created
March 1, 2011 10:06
-
-
Save anonymous/848928 to your computer and use it in GitHub Desktop.
クラスのロードが出来なくて困っています。。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package loaderClasses; | |
public interface LoadedClass { | |
public void doIt(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package loaderClasses; | |
public class SpeciallyLoadedClass implements LoadedClass { | |
public void doIt() { | |
System.out.println("doIt: Hello from " + getClass().getName()); | |
} | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// This loader takes a name of a directory that names | |
// location from which to load classes. | |
// | |
// Classes are cached. When load is requested with loadClass method, | |
// cache is checked to see if it has been loaded. | |
// | |
import java.io.*; | |
import java.util.*; | |
public class SimpleLoader extends ClassLoader { | |
private Hashtable _cache = new Hashtable(); | |
private String _pathBase; // Directory containg class files | |
public SimpleLoader(String base) | |
{ | |
_pathBase = base; | |
} | |
protected Class loadClass(String name, boolean resolve) | |
throws ClassNotFoundException | |
{ | |
Class c; | |
// Check class cache | |
c = (Class) getCache().get(name); | |
if (c == null) { | |
//try { | |
// c = findSystemClass(name); | |
// return c; | |
//} catch (Exception ClassNotFoundException) { | |
try { | |
String fileName = getPathBase() + | |
System.getProperty("file.separator") + | |
dotToSlash(name) + ".class"; | |
System.out.println("Class file: " + fileName); | |
byte data[] = loadClassData(fileName); | |
System.out.println("File size: " + data.length); | |
c = defineClass(name, data, 0, data.length); | |
// Cache it... | |
getCache().put(name, c); | |
} catch (ClassFormatError e) { | |
throw new ClassNotFoundException(name); | |
} | |
//} | |
} | |
// If we are really going to use class, resolve it! | |
if (resolve) { | |
resolveClass(c); | |
} | |
return c; | |
} | |
private byte loadClassData(String name)[] | |
{ | |
File file = new File(name); | |
try { | |
FileInputStream in = new FileInputStream(file); | |
byte[] buf = new byte[(int)file.length()]; | |
in.read(buf, 0, (int)file.length()); | |
return buf; | |
} catch (Exception e) { | |
e.printStackTrace(); | |
return null; | |
} | |
} | |
public static String dotToSlash(String cname) { | |
return cname.replace('.', | |
System.getProperty("file.separator").charAt(0)); | |
} | |
protected String getPathBase() { | |
return _pathBase; | |
} | |
protected Hashtable getCache() { | |
return _cache; | |
} | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import loaderClasses.*; | |
public class LoaderMain { | |
public static void main(String[] args) | |
{ | |
LoadedClass lc; | |
Class lcClass; | |
SimpleLoader loader = new SimpleLoader(args[0]); | |
try { | |
lcClass = loader.loadClass("loaderClasses.SpeciallyLoadedClass", true); | |
System.err.println("loaded : " + lcClass); | |
lc = (LoadedClass) lcClass.newInstance(); | |
lc.doIt(); | |
System.err.println("loaded : " + lc.getClass().getName()); | |
} catch (ClassNotFoundException e) { | |
e.printStackTrace(); | |
} catch (InstantiationException e) { | |
e.printStackTrace(); | |
} catch (IllegalAccessException iae) { | |
iae.printStackTrace(); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Class file: C:\\loaderClasses\SpeciallyLoadedClass.class | |
File size: 830 | |
Class file: C:\\loaderClasses\LoadedClass.class | |
File size: 138 | |
Class file: C:\\java\lang\Object.class | |
java.io.FileNotFoundException: C:\java\lang\Object.class (指定されたパスが見つかりません。) | |
at java.io.FileInputStream.open(Native Method) | |
at java.io.FileInputStream.<init>(FileInputStream.java:106) | |
at SimpleLoader.loadClassData(SimpleLoader.java:67) | |
at SimpleLoader.loadClass(SimpleLoader.java:40) | |
at java.lang.ClassLoader.loadClass(ClassLoader.java:248) | |
at java.lang.ClassLoader.defineClass1(Native Method) | |
at java.lang.ClassLoader.defineClassCond(ClassLoader.java:632) | |
at java.lang.ClassLoader.defineClass(ClassLoader.java:616) | |
at java.lang.ClassLoader.defineClass(ClassLoader.java:466) | |
at SimpleLoader.loadClass(SimpleLoader.java:42) | |
at java.lang.ClassLoader.loadClass(ClassLoader.java:248) | |
at java.lang.ClassLoader.defineClass1(Native Method) | |
at java.lang.ClassLoader.defineClassCond(ClassLoader.java:632) | |
at java.lang.ClassLoader.defineClass(ClassLoader.java:616) | |
at java.lang.ClassLoader.defineClass(ClassLoader.java:466) | |
at SimpleLoader.loadClass(SimpleLoader.java:42) | |
at LoaderMain.main(LoaderMain.java:13) | |
Exception in thread "main" java.lang.NullPointerException | |
at SimpleLoader.loadClass(SimpleLoader.java:41) | |
at java.lang.ClassLoader.loadClass(ClassLoader.java:248) | |
at java.lang.ClassLoader.defineClass1(Native Method) | |
at java.lang.ClassLoader.defineClassCond(ClassLoader.java:632) | |
at java.lang.ClassLoader.defineClass(ClassLoader.java:616) | |
at java.lang.ClassLoader.defineClass(ClassLoader.java:466) | |
at SimpleLoader.loadClass(SimpleLoader.java:42) | |
at java.lang.ClassLoader.loadClass(ClassLoader.java:248) | |
at java.lang.ClassLoader.defineClass1(Native Method) | |
at java.lang.ClassLoader.defineClassCond(ClassLoader.java:632) | |
at java.lang.ClassLoader.defineClass(ClassLoader.java:616) | |
at java.lang.ClassLoader.defineClass(ClassLoader.java:466) | |
at SimpleLoader.loadClass(SimpleLoader.java:42) | |
at LoaderMain.main(LoaderMain.java:13) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment