FW: Extending java lang classes

Simmen David simmen at inf.ethz.ch
Fri Jun 1 05:24:58 PDT 2012


Hope this doesn't end as a repost but ain't sure it was sent the first time

From: Simmen David
Sent: Friday, June 01, 2012 1:58 PM
To: 'jdk7-dev at openjdk.java.net'
Subject: Extending java lang classes

Hi

Out of a personal interest I wanted to add an additional method to java.lang.Object which returns the integer 42.

So far I added to Object.java the method:
public final native int get42();

in Object.c:
JNIEXPORT jint JNICALL
Java_java_lang_Object_get42(JNIEnv *env, jobject this) {
                if (this == NULL) {
                                JNU_ThrowNullPointerException(env, NULL);
                                return 0;
                } else {
                                return 42;
                }
}

And in java_lang_Object.h you can see the generated declaration by javah:
JNIEXPORT jint JNICALL Java_java_lang_Object_get42
  (JNIEnv *, jobject);

It all com[piles and so but when I try to call the method I get:
Exception in thread "main" java.lang.UnsatisfiedLinkError: java.lang.Object.get42()I
                at java.lang.Object.get42(Native Method)
                at Main.main(Main.java:13)

I found out that it tries to look it up at runtime and not like for example getClass at startup but I didn't find the place where to specify that it should load it at startup.
So what am I missing here so that it runs?

MfG David





More information about the jdk7-dev mailing list