gpt4 book ai didi

java - 使用在 C++ 中导入外部 jarfile 的 java 类

转载 作者:行者123 更新时间:2023-11-30 05:05:47 25 4
gpt4 key购买 nike

我已经在 C++ 代码中成功实现了 JNI。当我导入名为 xeger.jar 的外部 jar 文件并在 java 代码中使用它的函数时,就会出现问题。 java 文件上的代码在使用 eclipse 运行它时运行,即使我使用外部库也是如此。但是当我在 C++ 代码中使用 JNI 调用它时,它不会按预期返回值。

我有以下java类

package cversion.xeger.implementation;

import nl.flotsam.xeger.Xeger;

public class StringGenerator {

public static int giveMeNumber(int x) {
int y = 33;
String regex = "[1-9]\\|[1-9][0-9]{1,5}\\|1000000";
Xeger generator = new Xeger(regex);
String result = generator.generate();
System.out.println("Hurrayyyyyyyy !!!!!!!!!!!!!!" + result);
return y;
}
}

我的c++代码如下:

#include <iostream>
#include "jni.h"
#include <string.h>
#include <typeinfo>
int main()
{
JavaVMOption options[1];
JNIEnv *env;
JavaVM *jvm;
JavaVMInitArgs vm_args;
long status;
jclass cls;
jmethodID mid;
jint square;
jboolean answer;

// options[0].optionString = const_cast<char *>("-Djava.library.path=/usr/lib/jvm/java-8-oracle/lib/amd64:/usr/lib/jvm/java-8-oracle/jre/lib/amd64/server");
options[0].optionString = const_cast<char *>("-Djava.class.path=/home/aaa/eclipse-workspace/xeger/target/classes:/home/aaa/Downloads/automaton-1.12/dist/automation.jar:/home/aaa/Downloads/dxeger-1.0-SNAPSHOT.jar");
// options[0].optionString = const_cast<char *>("-Djava.class.path=/home/aaa/Desktop/classpath-try/");
// options[0].optionString = const_cast<char *>("-Djava -cp /home/aaa/Desktop/Untitled.jar");
memset(&vm_args, 0, sizeof(vm_args));
vm_args.version = JNI_VERSION_1_8;
vm_args.nOptions = 1;
vm_args.options = options;
status = JNI_CreateJavaVM(&jvm, (void**)&env, &vm_args); //If JVM creation is successful then status value is zero
std::cout<<"Status of JNI creation is : "<<status<<std::endl;

if (status != JNI_OK)
{
std::cout<<"JNI creation Failed : "<<std::endl;
return 1;
}
std::cout<<"JNI creation Passed : "<<std::endl;
cls = env->FindClass("cversion/xeger/implementation/StringGenerator"); // If the class cannot be found, cls will be zero.
std::cout<<"The class of cls is : "<<typeid(cls).name()<<std::endl;
// cls = env->FindClass("Sample2");
std::cout<<"The value of cls is : "<<cls<<std::endl;
if(cls !=0)
{
mid = env->GetStaticMethodID(cls, "giveMeNumber", "(I)I");
std::cout<<"The value of mid is : "<<mid<<std::endl;
if(mid !=0)
{ square = env->CallStaticIntMethod(cls, mid, 25);
std::cout<<"The value of square is "<<square<<std::endl;
printf("Result of implementation : %d\n", square);
}
}
jvm->DestroyJavaVM();
return 0;
}

当我运行 c++ 文件时,行:

printf("Result of intMethod: %d\n", square);

打印 0。它应该打印 33。

现在,如果我从 java 文件中删除以下代码,那么它会打印 33。

String regex = "[1-9]\\|[1-9][0-9]{1,5}\\|1000000";
Xeger generator = new Xeger(regex);
String result = generator.generate();
System.out.println("Hurrayyyyyyyy !!!!!!!!!!!!!!" + result);

最佳答案

对我来说非常好:

lib/main
Status of JNI creation is : 0
JNI creation Passed :
The class of cls is : P7_jclass
The value of cls is : 0x7f9b26c0d0b0
The value of mid is : 0x7f9b26c28230
Hurrayyyyyyyy !!!!!!!!!!!!!!3|27654|1000000
The value of square is 33
Result of implementation : 33

在您的代码中执行以下操作:

int y = 33;
try {
String regex = "[1-9]\\|[1-9][0-9]{1,5}\\|1000000";
Xeger generator = new Xeger(regex);
String result = generator.generate();
System.out.println("Hurrayyyyyyyy !!!!!!!!!!!!!!" + result);
} catch(Throwable ex) {
ex.printStackTrace();
}
return y;

错误会自行显现 ;)

我打赌你会得到:

java.lang.NoClassDefFoundError: dk/brics/automaton/RegExp
at nl.flotsam.xeger.Xeger.<init>(Xeger.java:45)

查看此处,了解完全可重现的用例:

.
├── Makefile
├── Makefile.common
├── c
│   └── main.cc
├── jar
│   ├── automaton.jar
│   └── xeger-1.0-SNAPSHOT.jar
├── java
│   └── simple
│   └── JavaCode.java
├── lib
└── target

你需要main.cc

#include <iostream>
#include "jni.h"
#include <string.h>
#include <typeinfo>
int main()
{
JavaVMOption options[1];
JNIEnv *env;
JavaVM *jvm;
JavaVMInitArgs vm_args;

options[0].optionString = const_cast<char *>("-Djava.class.path=./target:./jar/xeger-1.0-SNAPSHOT.jar:./jar/automaton.jar");
memset(&vm_args, 0, sizeof(vm_args));
vm_args.version = JNI_VERSION_1_8;
vm_args.nOptions = 1;
vm_args.options = options;

long status = JNI_CreateJavaVM(&jvm, (void**)&env, &vm_args);

std::cout << "Status of JNI creation is : "
<< status
<< std::endl;

if( status != JNI_OK ) {
std::cout << "JNI creation Failed : "
<< std::endl;
return 1;
}

std::cout << "JNI creation Passed : "
<< std::endl;

jclass cls = env->FindClass("simple/JavaCode");

std::cout << "The class of cls is : "
<< typeid(cls).name()
<< std::endl
<< "The value of cls is : "
<< cls
<< std::endl;

if( cls != 0 ) {
jmethodID mid = env->GetStaticMethodID(cls, "giveMeNumber", "(I)I");

std::cout << "The value of mid is : "
<< mid
<< std::endl;

if( mid != 0 ) {
jint square = env->CallStaticIntMethod(cls, mid, 25);

std::cout << "The value of square is "
<< square
<< std::endl;

printf("Result of implementation : %d\n", square);
} else {
return 1;
}
} else {
return 1;
}
jvm->DestroyJavaVM();
return 0;
}

和Java代码

package simple;

import nl.flotsam.xeger.Xeger;

public class JavaCode {

public static int giveMeNumber(int x) {
int y = 33;
try {
String regex = "[1-9]\\|[1-9][0-9]{1,5}\\|1000000";
Xeger generator = new Xeger(regex);
String result = generator.generate();
System.out.println("Hurrayyyyyyyy !!!!!!!!!!!!!!" + result);
} catch(Throwable ex) {
ex.printStackTrace();
}
return y;
}
}

<强>1。基于 LLVM 的设置

我使用 make 来构建代码,因为我同时编译 Java 和 native 代码。所以,你需要 Makefile.common

ARCH=$(shell uname -s | tr '[:upper:]' '[:lower:]')
ifeq ($(ARCH),darwin)
EXT=dylib
else
EXT=so
endif

生成文件。小心标签! Make 要求制表符成为每行目标的第一个字符。

include ./Makefile.common

ifeq ($(ARCH),darwin)
CC=llvm-g++
MAC_OS_FLAGS=-rpath ${JAVA_HOME}/jre/lib/server -L/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.12.sdk -dynamic -arch x86_64 -lSystem
else
CC=g++
MAC_OS_FLAGS=
endif

all: lib/main target/simple/JavaCode.class

target/simple/JavaCode.class: java/simple/JavaCode.java
$(JAVA_HOME)/bin/javac -cp ./jar/xeger-1.0-SNAPSHOT.jar -d target $<

lib/main.o: c/main.cc
$(CC) -c -o $@ $^ -I${JAVA_HOME}/include -I${JAVA_HOME}/include/$(ARCH)

lib/main: lib/main.o
llvm-g++ -v -o $@ -L${JAVA_HOME}/jre/lib/server/ -ljvm $(MAC_OS_FLAGS) $^

test: lib/main target/simple/JavaCode.class
lib/main

clean:
-rm -rfv target/*
-rm -rf lib/*

然后,你可以调用make test

llvm-g++ -c -o lib/main.o c/main.cc ...
...
...
lib/main
Status of JNI creation is : 0
JNI creation Passed :
The class of cls is : P7_jclass
The value of cls is : 0x7f895ec0d440
The value of mid is : 0x7f895ee11af0
Hurrayyyyyyyy !!!!!!!!!!!!!!5|64|1000000
The value of square is 33
Result of implementation : 33

您可以在这里找到许多不同的 JNI 代码:JNI Cookbook

<强>2。基于 GNU 的设置

如果是基于 GNU 的设置(例如在 Linux 中),您可以截断 Makefile 并删除 Makefile.common。

CC=g++

all: lib/main target/simple/JavaCode.class

target/simple/JavaCode.class: java/simple/JavaCode.java
$(JAVA_HOME)/bin/javac -cp ./jar/xeger-1.0-SNAPSHOT.jar -d target $<

lib/main.o: c/main.cc
$(CC) -c -o $@ $^ -I${JAVA_HOME}/include -I${JAVA_HOME}/include/linux

lib/main: lib/main.o
$(CC) -v -o $@ -L${JAVA_HOME}/jre/lib/amd64/server/ -ljvm $^

test: lib/main target/simple/JavaCode.class
lib/main

clean:
-rm -rfv target/*
-rm -rf lib/*

当然,你也必须确保下载jar(到jar目录)

curl -O \
"http://www.brics.dk/automaton/automaton.jar"
curl -O \
"https://storage.googleapis.com/google-code-archive-downloads/v2/code.google.com/xeger/xeger-1.0-SNAPSHOT.jar"

然后简单地调用make test

make test
lib/main
Status of JNI creation is : 0
JNI creation Passed :
The class of cls is : P7_jclass
The value of cls is : 0x136b810
The value of mid is : 0x148fdc0
Hurrayyyyyyyy !!!!!!!!!!!!!!6|756293|1000000
The value of square is 33
Result of implementation : 33

关于java - 使用在 C++ 中导入外部 jarfile 的 java 类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48219521/

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