Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / June 2007

Tip: Looking for answers? Try searching our database.

Extensions

Thread view: 
Click - 14 Jun 2007 10:59 GMT
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
Thomas Fritsch - 14 Jun 2007 11:55 GMT
>  try
>  {
[quoted text clipped - 11 lines]
>  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
[quoted text clipped - 14 lines]
>
> 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

Signature

Thomas

Click - 14 Jun 2007 12:08 GMT
"Thomas Fritsch" <i.dont.like.spam@invalid.com> a écrit dans le message de
news: > The API doc at
> <http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Class.html#newInstance()>
> says:
[quoted text clipped - 3 lines]
>   (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?
Patricia Shanahan - 14 Jun 2007 13:17 GMT
> "Thomas Fritsch" <i.dont.like.spam@invalid.com> a écrit dans le message de
> news: > The API doc at
[quoted text clipped - 9 lines]
> 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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.