Home > Archive > Java Help > December 2006 > JNI
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
|
|
|
| Hi I have the following code from
http://forum.java.sun.com/thread.jspa?threadID=716966. But I am having
trouble compiling it. If anyone could point me in the right direction I
would be grateful.
// Java:
package hk;
class HotkeyHandler {
static void onHotKey() {
System.out.println("hot key pressed");
}
}
// JNI:
JavaVM * jvm = NULL;
JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM * _jvm, void * reserved) {
jvm = _jvm;
return JNI_VERSION_1_4;
}
void OnHotKey(void *) {
JNIEnv * env;
if (jvm->AttachCurrentThread((void **)&env, NULL) < 0) {
printf("Error AttachCurrentThread\n");
return;
}
jclass cls = env->FindClass("hk/HotkeyHandler");
if (cls == NULL) { printf("Error FindClass\n"); return; }
jmethodID methId = env->GetStaticMethodID(cls, "onHotKey", "()V");
if (methId == NULL) { printf("Error GetStaticMethodID\n"); return;
}
env->CallStaticVoidMethod(cls, methId);
}
| |
| Gordon Beaton 2006-12-18, 4:16 am |
| On 16 Dec 2006 17:59:55 -0800, ab wrote:
> Hi I have the following code from
> http://forum.java.sun.com/thread.jspa?threadID=716966. But I am
> having trouble compiling it. If anyone could point me in the right
> direction I would be grateful.
What "trouble" are you having? It compiles fine for me.
How you compile is entirely dependent on what platform you're
compiling for, and what compiler you intend to use. You failed to
specify either of these things.
/gordon
--
[ don't email me support questions or followups ]
g o r d o n + n e w s @ b a l d e r 1 3 . s e
| |
|
| Sorry I wrote the above message in a rush as it was getting late. The
real problem is I have no idea how to get JNI working. e.g. What do i
do with the JNI code and java code. Do i seperate them into files or
just 1 java file. If you could explain how you compiled it, that enough
would get me started.
I have tried compiling the C code in devcpp and got a .o file and I am
100% sure I am doing it wrong. I tried the javah command with no luck.
Thanks.
| |
| Gordon Beaton 2006-12-18, 7:12 pm |
| On 18 Dec 2006 06:55:14 -0800, ab wrote:
> Sorry I wrote the above message in a rush as it was getting late.
> The real problem is I have no idea how to get JNI working. e.g. What
> do i do with the JNI code and java code. Do i seperate them into
> files or just 1 java file. If you could explain how you compiled it,
> that enough would get me started.
>
> I have tried compiling the C code in devcpp and got a .o file and I
> am 100% sure I am doing it wrong. I tried the javah command with no
> luck.
Store the C++ code in one file and the Java code in another.
I compiled and linked the C++ code exactly like this:
gcc -fPIC -shared -I $JDK/include -I $JDK/include/linux foo.C -o libfoo.so
Compiling and linking separately would have worked too:
gcc -fPIC -I $JDK/include -I $JDK/include/linux -c foo.C
gcc -shared foo.o -o libfoo.so
A few random points:
- the code you posted is C++, not C.
- javah is only necessary when your java class declares native
methods (i.e. so that you can call C++ from Java). That isn't the
case here, the code demonstrates calling Java from C++. It doesn't
show who calls the C++ function though.
- it's not complete; it seems to assume an external hook to keyboard
events that hasn't been provided. It's also missing an entry point
(i.e. main(), either in Java, C++ or C).
- normally you compile native code to a shared library or DLL, which
is different than compiling to an object file or an exectuable.
Look for that in your compiler documentation, or a linking and
libraries guide for your OS.
- if you're just trying to understand or learn JNI, get a "hello
world" example to work properly first. The code you've posted
requires a good understanding of how the components will work
together, as well as how they will interact with an external event
source.
Start reading here:
http://java.sun.com/docs/books/jni/html/jniTOC.html
http://java.sun.com/j2se/1.5.0/docs.../jni/index.html
/gordon
--
[ don't email me support questions or followups ]
g o r d o n + n e w s @ b a l d e r 1 3 . s e
| |
|
| Those links are exactly what I was looking for before I gave up and
decided to ask here. Its obvious I need to do some reading before I
continue.
Thanks Gordon, I appreciate the help.
| |
|
| Those links are exactly what I was looking for before I gave up and
decided to ask here. Its obvious I need to do some reading before I
continue.
Thanks Gordon, I appreciate the help.
|
|
|
|
|