我正在尝试关注这教程从 c++ 程序创建 JVM。程序编译成功,但运行时失败,提示
error while loading shared libraries: libjvm.so: cannot open shared object file: No such file or directory
该程序(它是从链接教程直接复制的):
#include <jni.h>
#include <iostream>
using namespace std;
int main()
{
JavaVM *jvm; // Pointer to the JVM (Java Virtual Machine)
JNIEnv *env; // Pointer to native interface
//================== prepare loading of Java VM ============================
JavaVMInitArgs vm_args; // Initialization arguments
JavaVMOption* options = new JavaVMOption[1]; // JVM invocation options
options[0].optionString = "-Djava.class.path=."; // where to find java .class
vm_args.version = JNI_VERSION_1_6; // minimum Java version
vm_args.nOptions = 1; // number of options
vm_args.options
= options;
vm_args.ignoreUnrecognized = false; // invalid options make the JVM init fail
//=============== load and initialize Java VM and JNI interface =============
jint rc = JNI_CreateJavaVM(&jvm, (void**)&env, &vm_args); // YES !!
delete options; // we then no longer need the initialisation options.
if (rc != JNI_OK) {
// TO DO: error processing...
std::cin.get();
exit(EXIT_FAILURE);
}
//=============== Display JVM version =======================================
cout << "JVM load succeeded: Version ";
jint ver = env->GetVersion();
cout << ((ver>>16)&0x0f) << "."<<(ver&0x0f) << endl;
// TO DO: add the code that will use JVM <============ (see next steps)
jvm->DestroyJavaVM();
cin.get();
}
生成文件:
CC=g++
CFLAGS=-c -Wall
SOURCES= main.cpp
OBJECTS=$(SOURCES:.cpp=.o)
EXECUTABLE=searchEngineCpp
PROG_PATH = /
all: $(SOURCES) $(EXECUTABLE)
$(EXECUTABLE): $(OBJECTS)
$(CC) $(OBJECTS) -o $@ -L/usr/bin/java -L/usr/lib/jvm/java-8-openjdk-amd64/jre/lib/amd64/server -ljvm
.cpp.o:
$(CC) $(CFLAGS) $< -o $@ -g -I/usr/lib/jvm/java-8-openjdk-amd64/include -I/usr/lib/jvm/java-8-openjdk-amd64/include/linux
clean:
rm -rf *.o searchEngineCpp
我尝试使用以下方法设置 LD_LIBRARY_PATH
export LD_LIBRARY_PATH=/usr/lib/jvm/java-7-oracle/lib/amd64:/usr/lib/jvm/java-7-oracle/lib/amd64/server
我看到一些帖子建议确保 JAVA_HOME 变量指向;我确保它指向上述文件夹。
还有其他帖子建议跑步sudo R CMD javareconf
那里有 JAVA_HOME 变量/usr/lib/jvm/default-java
。我该怎么办?
答案1
您应该能够使用LD_LIBRARY_PATH
,但是您需要确保使用与 Makefile 链接器命令中给出的相同路径,即
LD_LIBRARY_PATH=/usr/lib/jvm/java-8-openjdk-amd64/jre/lib/amd64/server
(FWIW 您不需要-L/usr/bin/java
链接器命令。)
您可以使用 ex 检查路径是否正确ldd
。
LD_LIBRARY_PATH=/usr/lib/jvm/java-8-openjdk-amd64/jre/lib/amd64/server ldd ./searchEngineCpp
对于更持久的解决方案,您可以将文件添加到/etc/ld.so.conf.d/
目录中,然后运行ldconfig
以更新动态链接器缓存。名称并不重要,只要它对您有意义即可。例如:
echo '/usr/lib/jvm/java-8-openjdk-amd64/jre/lib/amd64/server' |
sudo tee -a /etc/ld.so.conf.d/java-8-openjdk-amd64.conf
sudo ldconfig