简体   繁体   中英

Unable to start program because of .dll (JNI)

I was doing the JNI Tutorial for C++ and was able to build successfully. However, when I attempt to run, there is this error popup "Unable to start program 'C:...\\CLibHelloWorld.dll". The resource tutorial i followed were : http://electrofriends.com/articles/jni/jni-part1-java-native-interface/ http://electrofriends.com/articles/jni/part-2-jni-visual-studio-setup-dll-project/

Am I doing something wrong with my .dll configurations here ? I did change my "Additional Include Directories" as indicated as well and my Java is working as I was able to compile the java.class file into a .header file.

My compiled ClibHelloWorld.h file

/* 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

My ClibHelloWorld.cpp is :

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

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

You can't run a DLL. To "run" a JNI DLL you also need a Java program using that DLL. In your case probably something like

public static void main(String argv[]) {
    new HelloWorld().print();
}

The Java runtime will detect that "print" is a native method and will load the DLL and call the function there. To be able to debug the DLL from Visual Studio your best option is to enter the "java" call in the "Debugging" section of the VS project properties. (It's also possible to attach the VS debugger to a running Java process, but short programs are usually finished before you get the chance to attach)

JNI is a technique to make interfacing between Java and native platform code possible. One use is calling (usually) C code from Java. That's what your tutorial is doing. The other use is calling Java code from C. That's a bit more complicated and there are other tutorials out there on how to do that, but only in this case could you build an EXE, that Visual Studio can "just start".

I figured that this code are only the .dll and it will generate a .dll file in the parent Debug folder.

Created a HelloWorld.java and used javac to compile it into a .class file Then used javah to create a header file. Then in the command prompt run "java HelloWorld" to run the program and load the code written in C++ inside the java program

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM