Extensions

C

Click

Hello,

In my project, I have to implement a system of extensions : loadable classes
all derived from an abstract class defined in my main project.
I am using netbeans.
So I have created another project beside my main project (which will load
the extension), and I have defined in the project properties, libraries
folder, the compile time libraries to be my main project jar file, and the
same in the run, compile test and run test panel.

In my main project, I load the class file witht he following code :

try
{
CExtClassLoader loader=new CExtClassLoader(extPath);
Class c=Class.forName(extName, false, loader);
CEditExtension object = (CEditExtension)c.newInstance();
return object;
}
catch (LinkageError e)
{...}
catch (ClassNotFoundException e)
{...}
catch (InstantiationException e)
{...}
catch (IllegalAccessException e)
{...}

My class loader locates the class file in the extension folder, creates a
byte array and calls defineClass as explained in the javadoc. It
successfully loads the file.

The problem is that I have a IllegalAccessException in the Class.forName
method. I do not know why. The doc on this exception says :

"An IllegalAccessException is thrown when an application tries to
reflectively create an instance (other than an array), set or get a field,
or invoke a method, but the currently executing method does not have access
to the definition of the specified class, field, method or constructor."

So obviously, it does not find something when instanciating my extension. It
is true that the extension imports some of the packages defined in my main
project. But it compiles fine (as soon as I declared the jar file as a
library).

What am I doing wrong?

Thank you for your input.

Francois
 
T

Thomas Fritsch

Click said:
try
{
CExtClassLoader loader=new CExtClassLoader(extPath);
Class c=Class.forName(extName, false, loader);
CEditExtension object = (CEditExtension)c.newInstance();
return object;
}
catch (LinkageError e)
{...}
catch (ClassNotFoundException e)
{...}
catch (InstantiationException e)
{...}
catch (IllegalAccessException e)
{...}
If you write
catch (IllegalAccessException e) {
e.printStackTrace();
}
what exactly do get ?
My class loader locates the class file in the extension folder, creates a
byte array and calls defineClass as explained in the javadoc. It
successfully loads the file.

The problem is that I have a IllegalAccessException in the Class.forName
method. I do not know why. The doc on this exception says :

"An IllegalAccessException is thrown when an application tries to
reflectively create an instance (other than an array), set or get a field,
or invoke a method, but the currently executing method does not have
access to the definition of the specified class, field, method or
constructor."
So obviously, it does not find something when instanciating my extension.
It is true that the extension imports some of the packages defined in my
main project. But it compiles fine (as soon as I declared the jar file as
a library).

What am I doing wrong?
The API doc at
<http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Class.html#newInstance()>
says:
"IllegalAccessException - if the class or its nullary constructor is not
accessible."
Therefore I would guess that
(1) your class doesn't not have a no-arguments constructor
or (2) your class does have that constructor, but it is not public
 
C

Click

Thomas Fritsch said:
<http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Class.html#newInstance()>
says:
"IllegalAccessException - if the class or its nullary constructor is not
accessible."
Therefore I would guess that
(1) your class doesn't not have a no-arguments constructor
or (2) your class does have that constructor, but it is not public

No I do have a constructor in the class, and it is public. But on the other
have, in the extension I import packages defined in the main runtime. Could
it be that?
 
P

Patricia Shanahan

Click said:
No I do have a constructor in the class, and it is public. But on the other
have, in the extension I import packages defined in the main runtime. Could
it be that?

"import" only affects compile time name resolution.

I would expect a problem with name resolution to show up as an exception
on the Class.forName call. However, there could be an accessible class
with the right name, but without an accessible no-args constructor.

Just in case, if you are not already doing so, try passing a fully
qualified name to Class.forName.

Patricia
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top