Git Product home page Git Product logo

jnidemo's Introduction

一个JNI的Demo,双向调用。目前仅在mac上测试,其他系统需修改代码及脚本。

Java调用C++

  1. 编辑Java文件JNIDemo、User
  2. 生成头文件 javac -h -jni -classpath out/production/jnidemo -d src JNIDemo
    JDK11+: javac -encoding utf8 -h src -d out/production/jnidemo src/JNIDemo.java src/User.java
  3. 编写C++文件,编译生成动态链接库 g++ src/JNIDemo.cpp -I $JAVA_HOME/include -I $JAVA_HOME/include/darwin -fPIC -shared -o libdemo.so
  4. 补充Java代码,调用所需的类,运行

C++调用Java

  1. 编写Java代码JNICpp,准备清单文件
  2. 打包:jar cvfm cpp.jar src/MANIFEST.MF -C out/production/jnidemo/ JNICpp.class
  3. 编写C++代码,在C++中手动构建并销毁虚拟机,在中间增添要实现的内容,根据官方文档查找API实现所需的Java方法
  4. 编译:g++ -g src/main.cpp -I $JAVA_HOME/include -I $JAVA_HOME/include/darwin -ljvm -L $JAVA_HOME/lib/server/ -o jnicpp
  5. 运行测试:./jnicpp

也可使用CMake:

  1. ./cmake.sh
  2. ./build/JniDemoCpp

jnidemo's People

Contributors

mxdzs0612 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.