This is the mail archive of the java-discuss@sourceware.cygnus.com mailing list for the Java project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: Dynamic loading - solutions and more problems


Matt Welsh wrote:

> Per Bothner <per@bothner.com> writes:
> >
> > Have you tried compiling MainProgram.java with -fno-assume-compiled ?
>
> This does allow 'MainProgram.so' to load, but it crashes in
> "_Jv_PrepareConstantTimeTables". Here is the error:

I noticed that too. I havn't taken a close look at it yet, mostly because the "-fno-assume-compiled" option doesn't work at all on the cvs compiler
(crashes with a sig11 error from jc1). As you found out, it does seem to work on 2.95.2, but not if you try to specifiy a specific class (like
"-fno-assume-compiled=ToLoad").

Looking at the stack trace it appears that initializeClass() may be getting called on "ToLoad" before it has actually been prepared/loaded.

However... I'm actually not totally convinced that your example is useful. You are loading a class with forName() but then referring to that same class
directly in the code. Therefore, it must be available at compile time, so why not link it the normal way? Or am I missing the point?

A more useful situation would to be to dynamically load a class, unknown at compile time, that implements a known interface or extends a known class, so
your example would look something like:


public class ToLoad
{
  void doit()
  {
    System.out.println("done");
  }
}

public class ToLoadExt extends ToLoad
{
  void doit()
  {
     System.out.println("done ext");
  }
}

public class MainProgram {

  public static void main(String args[]) {
    try {
      Class ToLoadCls = Class.forName("ToLoadExt");
      ToLoad toload;
      toload = (ToLoad)ToLoadCls.newInstance();
      toload.doit();
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
}

Now things seem to work fine. Do you have any good real world example where you'd need your way to work?

regards

  [ bryce ]



Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]