Jump to content
  • Advertisement
Sign in to follow this  
HelloSkitty

JNI Linking

This topic is 2494 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Pretty much taken straight out of the wikipedia Java Native Interface page, I made 3 files:

HelloWorld.java:

class HelloWorld
{
private native void print();
public static void main(String[] args) {
//new HelloWorld().print();
}
static {
System.loadLibrary("HelloWorld");
}
}


HelloWorld.h

/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class HelloWorld */
#ifndef _Included_HelloWorld
#define _Included_HelloWorld
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: HelloWorld
* Method: print
* Signature: ()V
*/
JNIEXPORT void JNICALL Java_HelloWorld_print
(JNIEnv *, jobject);
#ifdef __cplusplus
}
#endif
#endif


libHelloWorld.c

#include "jni.h"
#include <stdio.h>
#include "HelloWorld.h"

JNIEXPORT void JNICALL
Java_HelloWorld_print(JNIEnv *env, jobject obj)
{
printf("Hello World!\n");
return;
}


The java code compiled fine, this line gave me no errors (I am assuming this is the correct usage)


gcc -I C:\glassfish3\jdk\bin\include -shared libHelloWorld.c -o libHelloWorld.so

But then I try to run the java code and I get this:

Exception in Thread "main" java.lang.UnsatifiedLinkError: no HelloWorld in java.library.path

So I replaced, in the java code, the System.loadLibrary parameter with HelloWorld.h, HelloWorld.c, libHelloWorld.c, and libHelloWorld.so. Each gave me that UnsatisfiedLinkError.

My directory looks like this:

a.exe (I'd also like to know how to control the name of the resulting .exe from running c++.exe on a c++ file)
libHelloWorld.c
HelloWorld.class
HelloWorld.h.gch (No idea what that is, or how it got there)
HelloWorld.h
HelloWorld.java
libHelloWorld.so
Compiler.bat

The files are all there in that directory. jni.h is in C:\glassfish3\jdk\bin\include, so where is this UnsatifiedLinkError coming from? Thanks in advance!

Share this post


Link to post
Share on other sites
Advertisement
The VM does not automatically search the working directory or environment directories. You have to specify the the path(s) to native libraries.
It should work relative to the working directory though, so try running you java app using:

java -Djava.library.path=./ HelloWorld

Share this post


Link to post
Share on other sites
You should also give your dynamic library a ".dll" extension on Windows. You might also need to remove the "lib" prefix, but I don't think that's necessary.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!