Java to access the C ++ method JavaCPP

Source: Internet
Author: User

Java to access the C ++ method JavaCPP

JavaCPP provides efficient access to local C ++ in Java. JNI technology is used to implement all Java implementations, including Android, Avian, and RoboVM.

JavaCPP provides a series of Annotation functions to map Java code to C ++ code, use an executable jar package to convert the C ++ code to a dynamic link library file that can be called from the JVM.

Maven:

 
 
  1. <dependency> 
  2.     <groupId>org.bytedeco</groupId> 
  3.     <artifactId>javacpp</artifactId> 
  4.     <version>0.11</version> 
  5. </dependency> 

Usage:

C ++:

 
 
  1. #include <string> 
  2.   
  3. namespace LegacyLibrary { 
  4.     class LegacyClass { 
  5.         public: 
  6.             const std::string& get_property() { return property; } 
  7.             void set_property(const std::string& property) { this->property = property; } 
  8.             std::string property; 
  9.     }; 

Java:

 
 
  1. import org.bytedeco.javacpp.*; 
  2. import org.bytedeco.javacpp.annotation.*; 
  3.   
  4. @Platform(include="LegacyLibrary.h") 
  5. @Namespace("LegacyLibrary") 
  6. public class LegacyLibrary { 
  7.     public static class LegacyClass extends Pointer { 
  8.         static { Loader.load(); } 
  9.         public LegacyClass() { allocate(); } 
  10.         private native void allocate(); 
  11.   
  12.         // to call the getter and setter functions  
  13.         public native @StdString String get_property(); public native void set_property(String property); 
  14.   
  15.         // to access the member variable directly 
  16.         public native @StdString String property();     public native void property(String property); 
  17.     } 
  18.   
  19.     public static void main(String[] args) { 
  20.         // Pointer objects allocated in Java get deallocated once they become unreachable, 
  21.         // but C++ destructors can still be called in a timely fashion with Pointer.deallocate() 
  22.         LegacyClass l = new LegacyClass(); 
  23.         l.set_property("Hello World!"); 
  24.         System.out.println(l.property()); 
  25.     } 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.