UnsatisfiedLinkError for JNI function from Xtext project

  c++, eclipse, java, java-native-interface, xtext

I have a Xtext project and for the validator, I need to call C++ functions. I have generated the header file and created the corresponding cpp file
DummyValidator.h

/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class com_example_dummy_validation_DummyValidator */

#ifndef _Included_com_example_dummy_validation_DummyValidator
#define _Included_com_example_dummy_validation_DummyValidator
#ifdef __cplusplus
extern "C" {
#endif
/*
 * Class:     com_example_dummy_validation_DummyValidator
 * Method:    print
 * Signature: ()V
 */
JNIEXPORT void JNICALL Java_com_example_dummy_validation_DummyValidator_print
  (JNIEnv *, jobject);

#ifdef __cplusplus
}
#endif
#endif

DummyValidator.cpp

#include "DummyValidator.h"

#include <iostream>

using namespace std;

JNIEXPORT void JNICALL

Java_com_example_dummy_validation_DummyValidator_print(JNIEnv *, jobject){

    cout << "Oh Bob, how handsome you are!n";

    return;

}

In the project properties, I have added the library path to Java Build Path

Native library loc: dummy_validator/Debug

But once I run the xtext project as an Eclipse application, while working on the DSL, I get the error

java.lang.UnsatisfiedLinkError: 'void com.example.validation.DummyValidator.print()'

The validator code looks like:

/*
 * generated by Xtext 2.25.0
 */
package com.example.dummy.validation;

import org.eclipse.xtext.validation.Check;

import dummy.DummyType;

/**
 * This class contains custom validation rules. 
 *
 * See https://www.eclipse.org/Xtext/documentation/303_runtime_concepts.html#validation
 */
public class DummyValidator extends AbstractDummyValidator {
    
    private native void print();
    
    public static final String INVALID_NAME = "invalidName";

        
    @Check
    public void checkUniqueNames(DummyType dummy) {
        print();    // UnsatisfiedLinkError
    }
    
    static {

        System.loadLibrary("dummy_validator");

    } 
}

Looks like the library is getting loaded properly. Also, if I load the library from a standalone Java project it works fine.

package com.example.dummy.validation;

class DummyValidator {

    private native void print();

    public static void main(String[] args) {
        System.loadLibrary("dummy_validator");
        new DummyValidator().print();    // works fine
    }

}

Source: Windows Questions C++

LEAVE A COMMENT