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

使用java类在C++; 我成功地实现了C++代码中的JNI。当我导入名为xeger.jar的外部jar文件并在java代码中使用其函数时,就会出现问题。java文件上的代码在使用ECLIPSE运行它时运行,即使在使用外部库时也是如此。但是,当我在C++代码中调用JNI时,它并没有按预期返回值。p>,java,c++,java-native-interface,Java,C++,Java Native Interface,我有以下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"; Xege

我有以下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;
 }
打印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)
你需要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;
}
1。基于LLVM的设置

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

ARCH=$(shell uname -s | tr '[:upper:]' '[:lower:]')
ifeq ($(ARCH),darwin)
  EXT=dylib
else
  EXT=so
endif
Makefile
。小心标签!!Make要求tab是目标每行的第一个字符

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/*
您可以在这里找到许多不同的JNI代码:

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/*
只需调用
maketest

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
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

你用什么来运行它?您是否使用了g++在很长时间后返回。我试图在linux中运行它,但没有找到llvm:command。已安装llvm,但问题仍然存在。现在存在链接器问题(.text+0x5f):未定义对“JNI_CreateJavaVM”的引用。我的JAVA_主页设置正确,指向/usr/lib/jvm/JAVA-8-oracle请确保在运行代码时使用libjvm.on ld_library_路径。我已将ld_library_路径设置为/usr/lib/jvm/JAVA-8-oracle/lib/amd64:/usr/lib/jvm/JAVA-8-oracle/jre/lib/amd64/server,但仍然存在相同的问题
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
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/*
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
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