Search code examples
javac++visual-studio-2010java-native-interface

Error in VS2010 when trying to compile basic JNI Test Code


I am trying to compile a basic JNI Test in preparation for a DLL (with headers and lib file) I am expecting to receive in the next few days that I will need to create a JNI interface for.

I have tried pointing it to the x64/x86 lib of JNI.lib. I have tried not pointing it to the lib at all. I have tried pointing it to both x64/x86 versions of the include directories but it is always the same error as below.

Visual Studio is giving me the following error and I have no idea why:

1>------ Build started: Project: CLibHelloWorld, Configuration: Debug Win32 ------
1>Build started 04/03/2015 19:04:09.
1>InitializeBuildStatus:
1>  Creating "Debug\CLibHelloWorld.unsuccessfulbuild" because "AlwaysCreate" was specified.
1>ClCompile:
1>  CLibHelloWorld.c
1>Link:
1>     Creating library F:\Work\04-Companies\DigitalARCSystems\KlearKapture\CameraInterface\test\test-jni\Debug\CLibHelloWorld.lib and object F:\Work\04-Companies\DigitalARCSystems\KlearKapture\CameraInterface\test\test-jni\Debug\CLibHelloWorld.exp
1>  test-jni.vcxproj -> F:\Work\04-Companies\DigitalARCSystems\KlearKapture\CameraInterface\test\test-jni\Debug\CLibHelloWorld.dll
1>FinalizeBuildStatus:
1>  Deleting file "Debug\CLibHelloWorld.unsuccessfulbuild".
1>  Touching "Debug\CLibHelloWorld.lastbuildstate".
1>
1>Build succeeded.
1>
1>Time Elapsed 00:00:00.43
========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========

HelloWorld.java (compiled with javac HelloWorld.java):

class HelloWorld {
    public native void print();  //native method
    static   //static initializer code
    {
        System.loadLibrary("CLibHelloWorld");
    } 

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

HelloWorld.h (generated by javah -jni HelloWorld):

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

CLibHelloWorld.c:

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

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

Solution

  • Converting a comment into an answer, since apparently it helped solve the problem.

    One way to investigate the problems with MSBuild is by looking at detailed logs. From VS command prompt, cd to directory of the project, then execute command msbuild MyProject.vcxproj /v:d. This will build with detailed verbosity level. Search for linker command in the output, you should see something like

    C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\bin\link.exe /OUT:"c:\my_output_path\myfile.dll" ...
    

    The /OUT option of the linker is what you are after. If linker is not invoked at all, you have some setting that does not require producing binary for the project.