Problems with javah and JNI [message #83550] |
Wed, 06 August 2003 20:53 |
Eclipse User |
|
|
|
Originally posted by: i.buckley.imperial.ac.uk
Looking back through the mailing list, I see a number of threads on using
the Java Native Interface (JNI). My problem does not quite correspond to
others that have been described.
I find that Eclipse insists that I use a package name in my java file,
HelloWorld.java:
============================================================ ==============
package nativetest; // Eclipse requires this if this is the package name
class HelloWorld {
public native void displayHelloWorld();
static {
System.loadLibrary("hello");
}
public static void main(String[] args) {
new HelloWorld().displayHelloWorld();
}
}
============================================================ ==============
I compile this into a class using Eclipse's compiler to make
HelloWorld.class.
But if I run javah on this manually, outside of Eclipse, the generated
header file, HelloWorld.h makes no mention of the package name:
============================================================ ==============
/* 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: displayHelloWorld
* Signature: ()V
*/
JNIEXPORT void JNICALL Java_HelloWorld_displayHelloWorld
(JNIEnv *, jobject);
#ifdef __cplusplus
}
#endif
#endif
============================================================ ==============
If I compile a corresponding C program:
============================================================ ==============
#include <jni.h>
#include "HelloWorld.h"
#include <stdio.h>
JNIEXPORT void JNICALL
Java_HelloWorld_displayHelloWorld(JNIEnv *env, jobject obj)
{
printf("Hello world!\n");
return;
}
============================================================ =========
and place the hello.dll on my (MS Windows 2000) path, I cannot run the
java code from Eclipse.
However, if I manually edit the .c and .h files to read
JNIEXPORT void JNICALL
Java_nativetest_HelloWorld_displayHelloWorld(JNIEnv *env, jobject obj)
etc...
(i.e. put in the package name), recompile the dll in MS Visual Studio,
then it all works nicely.
I am contributing this message in the hope that it helps someone else with
a similar problem.
The code, you will recognise, is from Sun's JNI tutorial:
http://java.sun.com/docs/books/tutorial/native1.1/index.html
Ian
|
|
|
Powered by
FUDForum. Page generated in 0.09232 seconds