JNI绑定:JVM无法找到依赖库

时间:2017-09-10 08:26:44

标签: java c makefile java-native-interface

我正在尝试为名为shoco的C项目创建JNI绑定。 尝试使用

加载库时
System.loadLibrary("shocolibjni")

JVM退出时出现错误“JVM无法找到依赖库”。 我创建了一个包含要导出的函数的def文件:

EXPORTS
shoco_compress
shoco_decompress
Java_org_shoco_Shoco_compress
Java_org_shoco_Shoco_decompress

我正在使用MINGW64下项目的更新makefile进行编译。 我试图将库放入系统目录并将java.library.path设置为“。”但是我得到了同样的错误。我认为在这个项目中没有依赖库这样的东西。 可能是什么问题?

JAVA代码:

package org.shoco;

public class Shoco {

    static {
        System.out.println(System.getProperty("java.library.path"));
        System.loadLibrary("shocolibjni");
    }

    public static native String compress(String s);

    public static native String decompress(String s, int compressedLength);

    public static String decompress(String s) {
        return decompress(s, s.length());
    }

    public static void main(String[] args) {
        String s = "This is a string which is used for test";
        String c = compress(s);
        System.out.println(c);
        String d = decompress(c);
        System.out.println(d);
    }
}

头文件org_shoco_Shoco.h:

/* DO NOT EDIT THIS FILE - it is machine generated */
#define __int64 int64_t
#include <jni.h>
/* Header for class org_shoco_Shoco */
#ifndef _Included_org_shoco_Shoco
#define _Included_org_shoco_Shoco
#ifdef __cplusplus
extern "C" {
#endif
/*
 * Class:     org_shoco_Shoco
 * Method:    compress
 * Signature: (Ljava/lang/String;)Ljava/lang/String;
 */
JNIEXPORT jstring JNICALL Java_org_shoco_Shoco_compress
  (JNIEnv *, jclass, jstring);

/*
 * Class:     org_shoco_Shoco
 * Method:    decompress
 * Signature: (Ljava/lang/String;)Ljava/lang/String;
 */
JNIEXPORT jstring JNICALL Java_org_shoco_Shoco_decompress
  (JNIEnv *, jclass, jstring, jint);

#ifdef __cplusplus
}
#endif
#endif

C file org_shoco_Shoco.c:

#define __int64 int64_t
#include <jni.h>
#include <stdio.h>
#include "org_shoco_Shoco.h"


/*
 * Class:     org_shoco_Shoco
 * Method:    compress
 * Signature: (Ljava/lang/String;)Ljava/lang/String;
 */
JNIEXPORT jstring JNICALL Java_org_shoco_Shoco_compress
  (JNIEnv *env, jclass jcl, jstring jstr)
{
    char buffer[4096];
    const char *str = (*env)->GetStringUTFChars(env, jstr, 0);
    shoco_compress(str, 0, buffer, 4096);
    (*env)->ReleaseStringUTFChars(env, jstr, str);
    return (*env)->NewStringUTF(env, buffer);
}

/*
 * Class:     org_shoco_Shoco
 * Method:    decompress
 * Signature: (Ljava/lang/String;)Ljava/lang/String;
 */
JNIEXPORT jstring JNICALL Java_org_shoco_Shoco_decompress
  (JNIEnv *env, jclass jcl, jstring jstr, jint comp_len)
{
    char buffer[4096];
    const char *str = (*env)->GetStringUTFChars(env, jstr, 0);
    shoco_decompress(str, comp_len, buffer, 4096);
    (*env)->ReleaseStringUTFChars(env, jstr, str);
    return (*env)->NewStringUTF(env, buffer);

}

Makefile:

FLAGS=$(CFLAGS) -std=c99 -O3 -Wall -I"/c/jdk1.7.0_80/include" -I"/c/jdk1.7.0_80/include/win32"
SOURCES=shoco.c org_shoco_Shoco.c
OBJECTS=$(SOURCES:.c=.o)
HEADERS=shoco.h shoco_model.h org_shoco_Shoco.h
GENERATOR=generate_compressor_model.py
TRAINING_DATA_DIR=training_data
TRAINING_DATA=$(wildcard training_data/*.txt)
TABLES_DIR=models
TABLES=$(TABLES_DIR)/text_en.h $(TABLES_DIR)/words_en.h $(TABLES_DIR)/filepaths.h

.PHONY: all
all: shoco shocolibjni.dll

shoco: shoco-bin.o $(OBJECTS) $(HEADERS)
    $(CC) $(LDFLAGS) $(OBJECTS) -s $< -o $@

test_input: test_input.o $(OBJECTS) $(HEADERS)
    $(CC) $(LDFLAGS) $(OBJECTS) -s $< -o $@

$(OBJECTS): %.o: %.c $(HEADERS)
    $(CC) $(FLAGS) $< -c

shoco_model.h: $(TABLES_DIR)/words_en.h
    cp $< $@

.PHONY: models
models: $(TABLES)

$(TABLES_DIR)/text_en.h: $(TRAINING_DATA) $(GENERATOR)
    python $(GENERATOR) $(TRAINING_DATA) -o $@

$(TABLES_DIR)/words_en.h: $(TRAINING_DATA) $(GENERATOR)
    python $(GENERATOR) --split=whitespace --strip=punctuation $(TRAINING_DATA) -o $@

$(TABLES_DIR)/dictionary.h: /usr/share/dict/words $(GENERATOR)
    python $(GENERATOR) $< -o $@

# Warning: This is *slow*! Use pypy when possible
$(TABLES_DIR)/filepaths.h: $(GENERATOR)
    find / -print 2>/dev/null | pypy $(GENERATOR) --optimize-encoding -o $@

.PHONY: check
check: tests

tests: tests.o $(OBJECTS) $(HEADERS)
    $(CC) $(LDFLAGS) $(OBJECTS) $< -o $@
    ./tests

.PHONY: clean
clean:
    rm *.o

.PHONY: js
js: shoco.js

shoco.js: $(OBJECTS) $(HEADERS) pre.js
    emcc shoco.c -O3 -o $@ --closure 1 -s EXPORTED_FUNCTIONS="['_shoco_compress', '_shoco_decompress']" --pre-js pre.js

shocolibjni.dll: $(OBJECTS)
    $(CC) -shared $^ -o $@ org_shoco_Shoco.def

0 个答案:

没有答案
相关问题