服务器之家

服务器之家 > 正文

Java利用JavaCPP调用算法示例

时间:2022-03-04 00:31     来源/作者:jacdong

配置liunx 环境系统

?
1
2
3
4
5
6
配置so 文件存放路径
[root@arch2 ~]# cat /etc/ld.so.conf.d/so.conf
/opt/app/tools/so/
 
从新调用ldconfig 命令
ldconfig

配置java 项目

配置pom 文件

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
<?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>

配置文件当中需要主要的几点

?
1
2
3
4
5
<!--第一次生成javacpp 文件的时候,skip 一定要设置为false ,后期对于生成的类进行了处理之后,为了避免每次运行都重启,可以把skip 设置为true-->
<configuration>
    <skip>false</skip>
    <outputDirectory>${project.build.sourceDirectory}</outputDirectory>                                       <classOrPackageName>com.jacdong.preset.* </classOrPackageName>
</configuration>
?
1
2
3
4
5
<!--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 类

?
1
2
3
4
5
6
7
8
9
10
@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调用内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://blog.csdn.net/Johnny_DongBin/article/details/120967767

标签:

相关文章

热门资讯

2022年最旺的微信头像大全 微信头像2022年最新版图片
2022年最旺的微信头像大全 微信头像2022年最新版图片 2022-01-10
蜘蛛侠3英雄无归3正片免费播放 蜘蛛侠3在线观看免费高清完整
蜘蛛侠3英雄无归3正片免费播放 蜘蛛侠3在线观看免费高清完整 2021-08-24
背刺什么意思 网络词语背刺是什么梗
背刺什么意思 网络词语背刺是什么梗 2020-05-22
yue是什么意思 网络流行语yue了是什么梗
yue是什么意思 网络流行语yue了是什么梗 2020-10-11
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
返回顶部