Java利用JavaCPP调用算法示例
目录
- 配置liunx 环境系统
- 配置java 项目
配置liunx 环境系统
配置so 文件存放路径 [root@arch2 ~]# cat /etc/ld.so.conf.d/so.conf /opt/app/tools/so/ 从新调用ldconfig 命令 ldconfig
配置java 项目
配置pom 文件
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>org.jacdong</groupId> <artifactId>javacpptest</artifactId> <version>0.1-SNAPSHOT</version> <properties> <include.path>${basedir}/cppbuild</include.path> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <javacpp.cppbuild.skip>true </javacpp.cppbuild.skip> <!-- To skip execution of cppbuild.sh: -Djavacpp.cppbuild.skip=true --> <javacpp.parser.skip>false </javacpp.parser.skip> <!-- To skip header file parsing phase: -Djavacpp.parser.skip=true --> <javacpp.compiler.skip>true </javacpp.compiler.skip> <!-- To skip native compilation phase: -Djavacpp.compiler.skip=true --> <javacpp.moduleId>${project.artifactId}</javacpp.moduleId> <javacpp.platform.root></javacpp.platform.root> <javacpp.platform.compiler></javacpp.platform.compiler> <javacpp.platform.extension></javacpp.platform.extension> <javacpp.platform.linux-x86>linux-x86</javacpp.platform.linux-x86> <javacpp.platform.linux-x86_64>linux-x86_64</javacpp.platform.linux-x86_64> <javacpp.platform>linux-x86_64</javacpp.platform> <skipTests>true</skipTests> </properties> <dependencies> <dependency> <groupId>org.bytedeco</groupId> <artifactId>javacpp</artifactId> <version>1.5.3</version> </dependency> <!-- https://mvnrepository.com/artifact/junit/junit --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <artifactId>maven-compiler-plugin</artifactId> </plugin> <plugin> <artifactId>maven-jar-plugin</artifactId> <version>3.1.0</version> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-dependency-plugin</artifactId> <version>2.10</version> </plugin> <plugin> <groupId>org.bytedeco</groupId> <artifactId>javacpp</artifactId> <version>1.5.3</version> <configuration> <properties>${javacpp.platform}</properties> <propertyKeysAndValues> <property> <name>platform.root</name> <value>${javacpp.platform.root}</value> </property> <property> <name>platform.compiler</name> <value>${javacpp.platform.compiler}</value> </property> <property> <name>platform.extension</name> <value>${javacpp.platform.extension}</value> </property> </propertyKeysAndValues> <classPath>${project.build.outputDirectory}</classPath> <includePaths> <includePath>${basedir}/src/main/resources/linux-x86_64</includePath> </includePaths> <linkPaths> </linkPaths> </configuration> <executions> <execution> <id>javacpp.parser</id> <phase>generate-sources</phase> <goals> <goal>build</goal> </goals> <configuration> <skip>false</skip> <outputDirectory>${project.build.sourceDirectory}</outputDirectory> <classOrPackageName>com.jacdong.preset.*</classOrPackageName> </configuration> </execution> </executions> </plugin> <plugin> <artifactId>maven-surefire-plugin</artifactId> <version>2.9</version> <configuration> <forkMode>once</forkMode> <environmentVariables> <LD_LIBRARY_PATH>target/classes/linux-x86_64</LD_LIBRARY_PATH> </environmentVariables> </configuration> </plugin> <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>exec-maven-plugin</artifactId> <version>1.1</version> <executions> <execution> <id>build-jnilib</id> <phase>process-classes</phase> <goals> <goal>exec</goal> </goals> <configuration> <executable>java</executable> <commandlineArgs>-jar ./libs/javacpp-1.5.jar -Xcompiler -g -Xcompiler -ggdb -nodelete -d target/classes/linux-x86_64 -Xcompiler -I${env.JAVA_HOME}/include -Xcompiler -I${env.JAVA_HOME}/include/linux -Xcompiler -fvisibility=default -Xcompiler -rdynamic -Xcompiler -std=c++11 -classpath target/classes com.jacdong.wrapper.JavaCPPTest</commandlineArgs> </configuration> </execution> <execution> <id>echoPath</id> <phase>install</phase> <goals> <goal>exec</goal> </goals> <configuration> <executable>echo</executable> <commandlineArgs>"sudo cp ${basedir}/target/classes/linux-x86_64/*.so /usr/lib/;sudo ldconfig"</commandlineArgs> </configuration> </execution> </executions> </plugin> <plugin> <artifactId>maven-clean-plugin</artifactId> <version>3.1.0</version> <configuration> <filesets> <fileset> <directory>src/main/java/com/jacdong/rtcm/wrapper/</directory> <includes> <include>JavaCPPTestTmp.java</include> </includes> <followSymlinks>false</followSymlinks> </fileset> </filesets> </configuration> </plugin> </plugins> <pluginManagement> <plugins> <!--This plugin's configuration is used to store Eclipse m2e settings only. It has no influence on the Maven build itself.--> <plugin> <groupId>org.eclipse.m2e</groupId> <artifactId>lifecycle-mapping</artifactId> <version>1.0.0</version> <configuration> <lifecycleMappingMetadata> <pluginExecutions> <pluginExecution> <pluginExecutionFilter> <groupId>org.bytedeco</groupId> <artifactId>javacpp</artifactId> <versionRange> [1.5.3,) </versionRange> <goals> <goal>build</goal> </goals> </pluginExecutionFilter> <action> <ignore></ignore> </action> </pluginExecution> <pluginExecution> <pluginExecutionFilter> <groupId> org.codehaus.mojo </groupId> <artifactId> exec-maven-plugin </artifactId> <versionRange> [1.1,) </versionRange> <goals> <goal>exec</goal> </goals> </pluginExecutionFilter> <action> <ignore></ignore> </action> </pluginExecution> </pluginExecutions> </lifecycleMappingMetadata> </configuration> </plugin> </plugins> </pluginManagement> </build> </project>
配置文件当中需要主要的几点
<!--第一次生成javacpp 文件的时候,skip 一定要设置为false ,后期对于生成的类进行了处理之后,为了避免每次运行都重启,可以把skip 设置为true--> <configuration> <skip>false</skip> <outputDirectory>${project.build.sourceDirectory}</outputDirectory> <classOrPackageName>com.jacdong.preset.* </classOrPackageName> </configuration>
<!--commandlineArgs 一定不要换行--> <configuration> <executable>java</executable> <commandlineArgs>-jar ./libs/javacpp-1.5.jar -Xcompiler -g -Xcompiler -ggdb -nodelete -d target/classes/linux-x86_64 -Xcompiler -I${env.JAVA_HOME}/include -Xcompiler -I${env.JAVA_HOME}/include/linux -Xcompiler -fvisibility=default -Xcompiler -rdynamic -Xcompiler -std=c++11 -classpath target/classes com.jacdong.wrapper.SingleStationIono</commandlineArgs> </configuration>
定义自己的map 类
@Properties(target = "com.jacdong.wrapper.JavaCPPTestTmp", value = {@Platform(include = {"javacpp.h"}, compiler = "cpp11")}) public class JavacppMapper implements InfoMapper{ public void map(InfoMap infoMap) { infoMap.put(new Info().cppNames("_stdcall").skip()) .put(); } }
注意点
在利用javacpp 的时候,经常会出现找不到so 文件的情况,这个时候,大家可以把so 文件copy 到/usr/lib 目录下,然后更改相应的权限。
到此这篇关于Java利用JavaCPP调用算法示例的文章就介绍到这了,更多相关Java JavaCPP调用内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!
赞 (0)