visual-studiojava-native-interfacesyntax-error

jni compile with cl compiler has syntax error, but not relevant


I am trying to use JNI in Windows 7 64 bit. Below is my steps.

Step 1. open new Java Application project in eclipse Kepler 4.3.

Step 2. make a Java Class named "HelloWorld". code is

public class HelloWorld {
    private static String lib_path = "C:\\Users\\thanks\\workspace\\Hello\\src\\";
    private static String lib_file = "hello.dll";
    String greeting = "Hello, Java World!";
    public native void display();
    public static void main(String[] args) {
        HelloWorld hw = new HelloWorld();
        System.out.println(hw.greeting);
        hw.display();
        System.out.println(hw.greeting);
    }

    static{
        System.load(lib_path + lib_file);
    }
};

Step 3. do 'javac' and 'javah' command in 'VS2013 dev command prompt'. Actual command is below.

javac HelloWorld.java
javah -jni HelloWorld 

Step 4. Now I got HelloWorld.java, HelloWorld.class, HelloWorld.h. So make a 'C' file named 'HelloWorld.c'. code is here.

#include "HelloWorld.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

JNIEXPORT void JNICALL JAVA_HelloWorld_display(JNIENV* env, jobject jobj){

    jfieldID fldid;
    jstring greeting;
    const char* tmp;

    jclass instance = (*env)->GetObjectClass(env, jobj);
    fprintf(stderr, "Start C Implementation.\n");

    if(jobj == NULL){
        fprintf(stderr, "Input pointer is null.\n");
        return;
    }

    fldid = (*env)->GetFieldID(env, instance, "greeting", "Ljava/lang/String;");

    if(fldid == NULL){
        fprintf(stderr, "Failed to get field ID.\n");
        return;
    }

    greeting = (*env)->GetObjectField(env, jobj, fldid);
    tmp = (*env)->GetStringUTFChars(env, greeting, NULL);

    printf("%s\n", tmp);
    greeting = NewStringUTF(env, "Hello C World");

    if(greeting == NULL){
        printf("Out of Memory.\n");
        return;
    }

    (*env)SetObjectField(env, jobj, fldid, greeting);
    printf("End of C impelementation.\n");
    return;
}

As you can see no syntax error here.

Step 4. now I compile with 'cl' compiler. command is

cl HelloWorld.c -Fehello.dll -MD -LD

Problem occurs in Step 4. When I try that command, it shows weird syntax errors. (I do not know why Windows command window does not allow dragging. So I just post error codes. But all messages are pointing syntax errors that actually does not exist.)

ErrorCode : C2143 - bracket. C2040 - reference level. C2146 - can not find ';'. C2059 - can not find '('. C2054 - can not find ')'.

Thanks for reading and sharing my errors:D


Solution

  • I solved this. It just problem of VS setting. Since my VS compiling option was Win32(but my OS is 64bits), it crashed.

    After I changed my compile option in VS, it works nice:D

    Thanks for @krsteeve. Your pointing was also right.