This is the mail archive of the java@gcc.gnu.org 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]
Other format: [Raw text]

Re: Dynamic Class Loading in GCJ


Tushar kant wrote:
> Hi! I'm writing an application in Java  using GCJ and would like to
> add a plug-in feature to it. I have searched the internet a lot but
> couldn't find a way to dynamically load .class\.so files dynamically.
> I would really appreciate it if someone could help me on this.

Here's a really simple example:

foo.java:

import java.net.*;

public class foo
{
  public static void main(String[] argv)
    throws Exception
  {
    ClassLoader l
      = (new URLClassLoader
	 (new URL[] {new URL("gcjlib://"
			     + System.getProperty("user.dir")
			     + "/libpop.so")}));
    Class<?> C = l.loadClass("pop");
    C.newInstance();
  }
}

pop.java:

public class pop
{
  pop()
  {
    System.out.println("Cooee!");
  }
}

 $ gcj foo.java  -o foo --main=foo
 $ gcj -findirect-dispatch -Wl,-Bsymbolic -shared pop.java -o libpop.so -fpic

 $ ./foo
Cooee!

Gcj has lots of other class loading mechanisms that you might find useful.
All this, and more, is explained in the gcj documentation.


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