Re: Dynamic Class Loading in GCJ

Andrew Haley <[email protected]>
Newsgroups gmane.comp.gcc.java.devel
Message-ID <[email protected]>
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.
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.