私は次のファイルを持っています:
src/
|- cltest/
| |- Main.class
| |- Test.class
| `- TestingClassLoader.class
`- data (a class cltest.LoadedClass without an extension)
Main.java:
package cltest;
public class Main {
protected static String field = "TestTestTest";
public static void main(String[] args) throws Exception {
Class.forName("cltest.Test");
Class.forName("cltest.LoadedClass", true, new TestingClassLoader());
}
}
Test.javaおよび/data
package cltest;
public class Test /* or LoadedClass for /data file */ {
static {
System.out.println(Main.field);
}
}
TestingClassLoader.java
package cltest;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.security.CodeSource;
import java.security.ProtectionDomain;
public class TestingClassLoader extends ClassLoader {
public Class<?> findClass(String name) throws ClassNotFoundException {
try {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
InputStream is = this.getClass().getResourceAsStream("/data");
byte[] buf = new byte[256]; int r;
while ((r = is.read(buf)) != -1) baos.write(buf, 0, r);
byte[] data = baos.toByteArray();
return defineClass(null, data, 0, data.length);
} catch (Exception e) {
throw new ClassNotFoundException(name, e);
}
}
}
このコードは私に行のエラーを与えますClass.forName("cltest.LoadedClass", true, new TestingClassLoader());
:
TestTestTest // (output of 1st Class.forName() call)
Exception in thread "main" java.lang.IllegalAccessError: tried to access field cltest.Main.field from class cltest.LoadedClass
at cltest.LoadedClass.<clinit>(LoadedClass.java:5)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at cltest.Main.main(Main.java:7)
どうすれば修正できますか?