2016-04-25 60 views
0

我使用javacpp從Java訪問cpp。運行javacpp時java.lang.UnsatisfiedLinkError

我曾嘗試文檔中提供的例子

cpp的代碼:

CompletableFuture<Integer> futureInC(){ 
    @StdFuture f = @cppDemo.futureInC(); 
    CompletableFuture<Integer> future = new CompletableFuture<>(); 
    f.then(int value -> future.complete(value)); 
    return future; 
} 

Java代碼:

import org.bytedeco.javacpp.*; 
import org.bytedeco.javacpp.annotation.*; 

@Platform(include="LegacyLibrary.h") 
@Namespace("LegacyLibrary") 
public class LegacyLibrary { 
    public static class LegacyClass extends Pointer { 
     static { Loader.load(); } 
     public LegacyClass() { allocate(); } 
     private native void allocate(); 

     // to call the getter and setter functions 
     public native @StdString String get_property(); public native void set_property(String property); 

     // to access the member variable directly 
     public native @StdString String property();  public native void property(String property); 
    } 

    public static void main(String[] args) { 
     // Pointer objects allocated in Java get deallocated once they become unreachable, 
     // but C++ destructors can still be called in a timely fashion with Pointer.deallocate() 
     LegacyClass l = new LegacyClass(); 
     l.set_property("Hello World!"); 
     System.out.println(l.property()); 
    } 
} 

我收到以下錯誤,如果我運行NativeLibrary.java文件Intellij Idea:

Exception in thread "main" java.lang.UnsatisfiedLinkError: no jniNativeLibrary in java.library.path 
at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1867) 
at java.lang.Runtime.loadLibrary0(Runtime.java:870) 
at java.lang.System.loadLibrary(System.java:1122) 
at org.bytedeco.javacpp.Loader.loadLibrary(Loader.java:597) 
at org.bytedeco.javacpp.Loader.load(Loader.java:438) 
at org.bytedeco.javacpp.Loader.load(Loader.java:381) 
at com.viettel.demo.NativeLibrary$NativeClass.<clinit>(NativeLibrary.java:13) 
at com.viettel.demo.NativeLibrary.main(NativeLibrary.java:27) 
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
at java.lang.reflect.Method.invoke(Method.java:498) 
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144) 

如何在Intellij Idea中運行示例javacpp,我確實嘗試了在Readme.md中沒有問題的guilde後面的命令行。 感謝您的支持!

+0

你在說什麼文檔?請鏈接 –

+0

https://github.com/bytedeco/javacpp 我正在使用javacpp進行JNI C++到Java –

回答

1

這組異常當應用程序試圖加載機庫。在這種情況下,JVM正在尋找在兩個PATH環境變量和的java.library.path系統property.To解決這個異常被觸發你需要爲你正在加載的庫設置路徑。就像你爲java設置路徑一樣。

+0

我**覺得** **我們**會找到您的答案更**可讀**如果您沒有不會用太多的**標記**。而一些適當的句子,大寫字母,標點符號等等會有所幫助。 –

+0

編輯我的答案爲混亂。 – Priyamal

相關問題