1. 程式人生 > >idea在執行時正常,但打包成一個jar執行檔案時出現Could not load JNR C Library問題

idea在執行時正常,但打包成一個jar執行檔案時出現Could not load JNR C Library問題

在通過java連線cassandra時,其中com.datastax.driver.core是通過maven加入的,通過Idea執行時是正常的,在匯出jar時如果不是將所有的jar打包成一個jar時也是正常的,但是如果將所有的第三方打包成jar,則會出現如下錯誤:

17:38:14.973 [main] DEBUG com.datastax.driver.core.Native - Could not load JNR C Library, native system calls through this library will not be available
java.lang.UnsatisfiedLinkError: unknown
    at jnr.ffi.provider.jffi.NativeLibrary.loadNativeLibraries(NativeLibrary.java:87)
    at jnr.ffi.provider.jffi.NativeLibrary.getNativeLibraries(NativeLibrary.java:70)
    at jnr.ffi.provider.jffi.NativeLibrary.getSymbolAddress(NativeLibrary.java:49)
    at jnr.ffi.provider.jffi.NativeLibrary.findSymbolAddress(NativeLibrary.java:59)
    at jnr.ffi.provider.jffi.AsmLibraryLoader.generateInterfaceImpl(AsmLibraryLoader.java:158)
    at jnr.ffi.provider.jffi.AsmLibraryLoader.loadLibrary(AsmLibraryLoader.java:89)
    at jnr.ffi.provider.jffi.NativeLibraryLoader.loadLibrary(NativeLibraryLoader.java:44)
    at jnr.ffi.LibraryLoader.load(LibraryLoader.java:325)
    at jnr.ffi.LibraryLoader.load(LibraryLoader.java:304)
    at com.datastax.driver.core.Native$LibCLoader.<clinit>(Native.java:104)
    at com.datastax.driver.core.Native.isGettimeofdayAvailable(Native.java:189)
    at com.datastax.driver.core.ClockFactory.newInstance(Clock.java:56)
    at com.datastax.driver.core.AbstractMonotonicTimestampGenerator.<init>(AbstractMonotonicTimestampGenerator.java:42)
    at com.datastax.driver.core.LoggingMonotonicTimestampGenerator.<init>(LoggingMonotonicTimestampGenerator.java:53)
    at com.datastax.driver.core.AtomicMonotonicTimestampGenerator.<init>(AtomicMonotonicTimestampGenerator.java:55)
    at com.datastax.driver.core.AtomicMonotonicTimestampGenerator.<init>(AtomicMonotonicTimestampGenerator.java:37)
    at com.datastax.driver.core.policies.Policies.defaultTimestampGenerator(Policies.java:123)
    at com.datastax.driver.core.policies.Policies$Builder.build(Policies.java:287)
    at com.datastax.driver.core.Cluster$Builder.getConfiguration(Cluster.java:1354)
    at com.datastax.driver.core.Cluster.<init>(Cluster.java:163)
    at com.datastax.driver.core.Cluster.buildFrom(Cluster.java:227)
    at com.datastax.driver.core.Cluster$Builder.build(Cluster.java:1371)
    at com.wincom.mstar.writedb.cas.CasDB.init(CasDB.java:37)
    at com.wincom.mstar.writedb.app.Application.run(Application.java:51)
    at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:813)
    at org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:797)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:324)
    at com.wincom.mstar.writedb.app.Application.main(Application.java:40)

出現這種問題主要是使用了jnr-ffi包引起的

<dependency>
            <groupId>com.github.jnr</groupId>
            <artifactId>jnr-ffi</artifactId>
            <version>2.1.9</version>
        </dependency>

將jnr-ffi更換成jnr-x86asm包即可:

<dependency>
            <groupId>com.github.jnr</groupId>
            <artifactId>jnr-x86asm</artifactId>
            <version>1.0.2</version>
        </dependency>