0

I tried to call an existing native method, but get UnsatisfiedLinkError, and do not understand why since I copied existing code from the JDK and didn't wrote my own C function.

I copied some code from the JDK Sockets Java source code. Running on Ubuntu.

public class Sokket {

static {
    System.loadLibrary ("net");
}

static {
    initProto ();
}

// rest ommited for brevity

}

Running this code results in:

Exception in thread "main" java.lang.UnsatisfiedLinkError: 

be.good.Sokket.initProto()V

at be.good.Sokket.initProto(Native Method)

at be.good.Sokket.<clinit>(Sokket.java:12)

I tried other native methods but same error, I changed the name of "net" to test if the library can be found and then I do get an error that that lib cannot be found in the java.library.path, I changed the name of "initProto" to test if the method can be found and then i do get the same error. So it looks to me that the "libnet.so" in the JDK lib directory has no initProto() but also no createSocket().

When I run with -verbose:jni I do see a lot of libs loaded but no reference to "net" or "initProto".

How can I call existing native methods from my own code?

Spangen
  • 4,420
  • 5
  • 37
  • 42
  • You should use `System.loadLibrary ("libnet");` . And, probably it is not a good idea to put it under the JDF folder. – Daniele Jun 03 '18 at 18:04
  • The native symbol name is derived from the names of the package and class, so it will look for a function called `Java_be_good_Sokket_initProto`, which (probably) doesn't exist in that library. But, it does look like you can link the methods manually: https://stackoverflow.com/q/1010645 (I've never tried that though) – Jorn Vernee Jun 03 '18 at 18:18
  • @Daniele `"net"` on linux is mapped to `libnet.so` as a file name, so that part is correct. – Jorn Vernee Jun 03 '18 at 18:20
  • @JornVernee not sure how that would work; of course you would not want to call `System.loadLibrary("libnet.so");`; since you need omit the `.so` suffix. In fact, `System.loadLibrary("net");` would look for a `net.so` file, which AFAIU is not mapped – Daniele Jun 03 '18 at 18:23
  • @Daniele See this QA: https://stackoverflow.com/q/37203247 – Jorn Vernee Jun 03 '18 at 18:27
  • right.. thanks for explaining :) – Daniele Jun 03 '18 at 18:40
  • @JornVernee: a big THANKS for pointing that out to me, will investigate on that further – SunnySunday Jun 03 '18 at 19:46

0 Answers0