Dynamic Class Loading in GCJ

Andrew Haley aph@redhat.com
Thu Dec 25 10:39:00 GMT 2008


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.


More information about the Java mailing list

AltStyle によって変換されたページ (->オリジナル) /