使用在C ++中导入外部jar文件的java类

栏目: java · 发布时间: 2021-04-13

简介  这篇文章主要介绍了使用在C ++中导入外部jar文件的java类以及相关的经验技巧,文章约9393字,浏览量106,点赞数6,值得推荐!

我已经在c ++代码中成功实现了JNI。当我导入名为xeger.jar的外部jar文件并在java代码中使用其函数时,会出现此问题。即使我使用外部库,使用ecllipse运行它时,java文件上的代码也会运行。但是当我在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
", square);
         }
     }
     jvm->DestroyJavaVM();
    return 0;
 }

当我运行c ++文件然后行:

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

它应该打印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
", 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和本机代码。所以,你需要Makefile.common

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

Makefile。选项卡要小心!使需要制表符成为每行目标的第一个字符。

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

以上就是本文的全部内容,希望对大家的学习有所帮助,版权归原作者或者来源机构所有,感谢作者,如果未能解决你的问题,请参考以下文章。

kindeditor在Java项目中的应用以及图片上传配置

Objective C如何在App内部调用外部dylib 和 framwork(示例代码)

在HTML中导入外部的css

如何在vue.js项目的main.js文件中导入js类并在所有组件中使用它而不是在每个组件中导入?

在eclipse中导入hadoop jar包,和必要时导入源码包。(示例代码)