U
Ulrich Scholz
Dear all,
I have three ways of reading in a class in a dynamic way: Instantiating
the class works with two of them, with the third this results in an
exception:
Exception in thread "main" java.lang.IllegalAccessError: tried to
access class JSHOP2_Method0 from class domainpresentationplanner
// (1) does not work
File file = new File(full_path_to_file,
"domainpresentationplanner.class");
classCode = getBytesFromFile(file);
domainClass = defineClass("domainpresentationplanner", classCode, 0,
classCode.length);
domain = (Domain) domainClass.newInstance(); // the same in all three
cases
// (2) works
domainClass = loadClass("domainpresentationplanner");
domain = (Domain) domainClass.newInstance();
// (3) works
domainClass = Class.forName("domainpresentationplanner");
domain = (Domain) domainClass.newInstance();
getBytesFromFile is taken from
http://javaalmanac.com/egs/java.io/File2ByteArray.html
I should mention that domainpresentationplanner.java, the java file
whose compilation produced domainpresentation.class, is comprised of
several classes, one of them being JSHOP2_Method0. All resulting class
files are in the same directory as domainpresentation.class.
Why does version (1) not work but (2) and (3) do?
Thank you, Uli
I have three ways of reading in a class in a dynamic way: Instantiating
the class works with two of them, with the third this results in an
exception:
Exception in thread "main" java.lang.IllegalAccessError: tried to
access class JSHOP2_Method0 from class domainpresentationplanner
work. The three ways are:From the class description, I cannot find out why defineClass does not
// (1) does not work
File file = new File(full_path_to_file,
"domainpresentationplanner.class");
classCode = getBytesFromFile(file);
domainClass = defineClass("domainpresentationplanner", classCode, 0,
classCode.length);
domain = (Domain) domainClass.newInstance(); // the same in all three
cases
// (2) works
domainClass = loadClass("domainpresentationplanner");
domain = (Domain) domainClass.newInstance();
// (3) works
domainClass = Class.forName("domainpresentationplanner");
domain = (Domain) domainClass.newInstance();
getBytesFromFile is taken from
http://javaalmanac.com/egs/java.io/File2ByteArray.html
I should mention that domainpresentationplanner.java, the java file
whose compilation produced domainpresentation.class, is comprised of
several classes, one of them being JSHOP2_Method0. All resulting class
files are in the same directory as domainpresentation.class.
Why does version (1) not work but (2) and (3) do?
Thank you, Uli