Question

I want to create a simple JNI layer. I used Visual studio 2008 to create a dll (Win 32 Console Application project type with DLL as the option). Im getting this exception when I invoke the native method:

Exception occurred during event dispatching:
java.lang.UnsatisfiedLinkError: com.tpd.vcdba.console.TaskScheduler.vcdbaTaskSch
edulerNative.Hello()V
        at com.tpd.vcdba.console.TaskScheduler.vcdbaTaskSchedulerNative.Hello(Na
tive Method)
        at com.tpd.vcdba.console.TaskScheduler.vcdbaTaskSchedulerUtil.isTaskExis
ts(vcdbaTaskSchedulerUtil.java:118)
        at com.tpd.vcdba.console.Dialogs.schedulerWizardPage.scheduleTaskPage.wz
Finish(scheduleTaskPage.java:969)
        at com.tpd.vcdba.console.wizard.vcdbaWizard.gotoFinish(vcdbaWizard.java:
434)
        at com.tpd.vcdba.console.wizard.wzActionPanel.actionPerformed(wzActionPa
nel.java:163)
        at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)

The header file generated is :

/* DO NOT EDIT THIS FILE - it is machine generated */

    #include <jni.h>
    /* Header for class com_tpd_vcdba_console_TaskScheduler_vcdbaTaskSchedulerNative */

    #ifndef _Included_com_tpd_vcdba_console_TaskScheduler_
    vcdbaTaskSchedulerNative
    #define _Included_com_tpd_vcdba_console_TaskScheduler_
    vcdbaTaskSchedulerNative
    #ifdef __cplusplus
    extern "C" {
    #endif
    /*
     * Class:     com_tpd_vcdba_console_TaskScheduler_vcdbaTaskSchedulerNative
     * Method:    Hello
     * Signature: ()V
     */
    JNIEXPORT void JNICALL Java_com_tpd_vcdba_console_TaskScheduler_vcdbaTaskSchedulerNative_Hello
      (JNIEnv *, jobject);

    #ifdef __cplusplus
    }
    #endif
    #endif

The implementation file is:

#pragma once
#include "com_tpd_vcdba_console_TaskScheduler_
vcdbaTaskSchedulerNative.h"
#include "stdafx.h"
#include "jni.h"

/*
 * Class:     com_tpd_vcdba_console_TaskScheduler_vcdbaTaskScheduler_native
 * Method:    Hello
 * Signature: ()V
 */
JNIEXPORT void JNICALL Java_com_tpd_vcdba_console_TaskScheduler_vcdbaTaskSchedulerNative_Hello
  (JNIEnv *envs, jobject obj){
    printf("hello world");
}

The java file is:

package com.tpd.vcdba.console.TaskScheduler;


import com.tpd.vcdba.console.TaskScheduler.ScheduleTask;

public class vcdbaTaskSchedulerNative {


    public native void Hello();
    private static vcdbaTaskSchedulerNative instance = null;

    static{
        try{
            System.loadLibrary("JNITrial");

        }
        catch(Exception ex){

        }

    }

    public vcdbaTaskSchedulerNative(){

    }

    public static vcdbaTaskSchedulerNative getInstance(){
        if(instance == null){
            instance = new vcdbaTaskSchedulerNative();
        }
        return instance;
    }

}

When I invoke the native method "Hello" i get the execption.

Another thing I observed is that when I compile in command line using: “cl -I"C:\Program Files (x86)\Java\jdk1.7.0\include" -I"C:\Program Files (x86)\Java\jdk1.7.0\include\win32" -LD "C:\Users\administrator.RMDOM\Documents\Visual Studio 2008\Projects\JNITrial\JNITrial\JNIInt.cpp" -FeJNITrial.dll” , everything works fine.

Am I missing out something in Visual Studio Settings? I have option Use of MFC as "Use MFC in a Shared DLL", Code generation option as "Multi-threaded DLL (/MD)". Its a 64 bit dll. Is there something else that I need to add?

Any help is welcome. Thanks in advance.

Was it helpful?

Solution 2

I figured out the solution.

My project had use precompiled headers option set, so the compiler was skipping the statement:

#include "com_tpd_vcdba_console_TaskScheduler_vcdbaTaskSchedulerNative.h"

Once I removed that option it worked like magic.

OTHER TIPS

Could you tell me what kind of JVM are you using, 32 or 64-bit? Your library is 640bit dll, but in your path I can see C:\Program Files (x86)... so maybe this is the problem.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top