gpt4 book ai didi

java - 将代码移入包后出现 UnsatisfiedLinkError

转载 作者:行者123 更新时间:2023-12-02 04:26:18 26 4
gpt4 key购买 nike

我正在开发一个需要使用.so库的项目(ubuntu 18.04),当我将java代码放在/src文件夹中时一切正常(我使用的是IntelliJ Idea),但是在我移动之后我的代码放入命名包(smutil)中,它会导致一些错误,例如“线程“main”java.lang.UnsatisfiedLinkError中的异常:smutil.GmSSL.digest(Ljava/lang/String;[B)[B”

这是我的代码

package smutil;
public class GmSSL {

public native String[] getVersions();
public native String[] getCiphers();
public native String[] getDigests();
public native String[] getMacs();
public native String[] getSignAlgorithms();
public native String[] getPublicKeyEncryptions();
public native String[] getDeriveKeyAlgorithms();
public native byte[] generateRandom(int length);
public native int getCipherIVLength(String cipher);
public native int getCipherKeyLength(String cipher);
public native int getCipherBlockSize(String cipher);
public native byte[] symmetricEncrypt(String cipher, byte[] in, byte[] key, byte[] iv);
public native byte[] symmetricDecrypt(String cipher, byte[] in, byte[] key, byte[] iv);
public native int getDigestLength(String digest);
public native int getDigestBlockSize(String digest);
public native byte[] digest(String algor, byte[] data);
public native String[] getMacLength(String algor);
public native byte[] mac(String algor, byte[] data, byte[] key);
public native byte[] sign(String algor, byte[] data, byte[] privateKey);
public native int verify(String algor, byte[] digest, byte[] signature, byte[] publicKey);
public native byte[] publicKeyEncrypt(String algor, byte[] in, byte[] publicKey);
public native byte[] publicKeyDecrypt(String algor, byte[] in, byte[] privateKey);
public native byte[] deriveKey(String algor, int keyLength, byte[] peerPublicKey, byte[] privateKey);
public native String[] getErrorStrings();

static {
System.loadLibrary("gmssljni");
}
}

最佳答案

您可能将 .so 文件打包到 JAR 中。请注意,一旦您有了 .so,加载库就不那么容易了。

为了加载共享库,您必须确保它位于文件系统上。您可以通过将其提取到临时位置来解决此问题。

查看此处了解说明此主题的完整示例:

https://github.com/mkowsiak/jnicookbook/tree/master/recipes/recipeNo031

基本上,您需要做的是:

  • 在 JAR 中找到您的资源
  • 将其解压到临时位置
  • 用户System.load(fullPathToFile)

就是这样:)

享受 JNI 带来的乐趣!

关于java - 将代码移入包后出现 UnsatisfiedLinkError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56607165/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com