JNA 简单试水
This commit is contained in:
parent
e8953abe12
commit
2396f620fb
60
pom.xml
60
pom.xml
@ -23,6 +23,8 @@
|
||||
|
||||
<java.version>17</java.version>
|
||||
<springboot.version>3.1.2</springboot.version>
|
||||
<commons-io.version>2.14.0</commons-io.version>
|
||||
<commons-lang3.version>3.13.0</commons-lang3.version>
|
||||
|
||||
<!--Java Bean-->
|
||||
<org.mapstruct.version>1.5.3.Final</org.mapstruct.version>
|
||||
@ -32,9 +34,15 @@
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.github.jnr</groupId>
|
||||
<artifactId>jnr-ffi</artifactId>
|
||||
<version>2.2.15</version>
|
||||
<groupId>net.java.dev.jna</groupId>
|
||||
<artifactId>jna</artifactId>
|
||||
<version>5.13.0</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>net.java.dev.jna</groupId>
|
||||
<artifactId>jna-platform</artifactId>
|
||||
<version>5.13.0</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
@ -54,6 +62,20 @@
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<!-- https://mvnrepository.com/artifact/commons-io/commons-io -->
|
||||
<dependency>
|
||||
<groupId>commons-io</groupId>
|
||||
<artifactId>commons-io</artifactId>
|
||||
<version>${commons-io.version}</version>
|
||||
</dependency>
|
||||
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 -->
|
||||
<dependency>
|
||||
<groupId>org.apache.commons</groupId>
|
||||
<artifactId>commons-lang3</artifactId>
|
||||
<version>${commons-lang3.version}</version>
|
||||
</dependency>
|
||||
|
||||
|
||||
<!--MapStruct-->
|
||||
<dependency>
|
||||
<groupId>org.mapstruct</groupId>
|
||||
@ -78,6 +100,28 @@
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
<configuration>
|
||||
<skip>false</skip>
|
||||
<excludes>
|
||||
<exclude>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
</exclude>
|
||||
</excludes>
|
||||
</configuration>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>repackage</id>
|
||||
<goals>
|
||||
<goal>repackage</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
@ -121,6 +165,16 @@
|
||||
<delimiter>@</delimiter>
|
||||
</delimiters>
|
||||
<useDefaultDelimiters>false</useDefaultDelimiters>
|
||||
|
||||
<encoding>UTF-8</encoding>
|
||||
<nonFilteredFileExtensions>
|
||||
<nonFilteredFileExtension>pfx</nonFilteredFileExtension>
|
||||
<nonFilteredFileExtension>crt</nonFilteredFileExtension>
|
||||
<nonFilteredFileExtension>cer</nonFilteredFileExtension>
|
||||
<nonFilteredFileExtension>key</nonFilteredFileExtension>
|
||||
<nonFilteredFileExtension>dll</nonFilteredFileExtension>
|
||||
<nonFilteredFileExtension>so</nonFilteredFileExtension>
|
||||
</nonFilteredFileExtensions>
|
||||
</configuration>
|
||||
</plugin>
|
||||
|
||||
|
@ -1,7 +1,9 @@
|
||||
package cn.skcks.jnr.sevenzip;
|
||||
|
||||
import cn.skcks.jnr.sevenzip.hello.Hello;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.context.ConfigurableApplicationContext;
|
||||
|
||||
@SpringBootApplication
|
||||
public class Application {
|
||||
|
37
src/main/java/cn/skcks/jnr/sevenzip/hello/Hello.java
Normal file
37
src/main/java/cn/skcks/jnr/sevenzip/hello/Hello.java
Normal file
@ -0,0 +1,37 @@
|
||||
package cn.skcks.jnr.sevenzip.hello;
|
||||
|
||||
import com.sun.jna.Library;
|
||||
import com.sun.jna.Native;
|
||||
import jakarta.annotation.PostConstruct;
|
||||
import lombok.SneakyThrows;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Slf4j
|
||||
@Component
|
||||
public class Hello {
|
||||
@PostConstruct
|
||||
void test(){
|
||||
context();
|
||||
};
|
||||
|
||||
public interface HelloLib extends Library {
|
||||
HelloLib INSTANCE = Native.load(loadLib("/lib/win64/hello.dll"), HelloLib.class);
|
||||
|
||||
int add(int a,int b);
|
||||
}
|
||||
|
||||
@SneakyThrows
|
||||
synchronized static String loadLib(String resourcePath){
|
||||
String path = Native.extractFromResourcePath(resourcePath).getAbsolutePath();
|
||||
log.info("{}", path);
|
||||
return path;
|
||||
}
|
||||
|
||||
@SneakyThrows
|
||||
public void context(){
|
||||
log.info("测试");
|
||||
HelloLib hello = HelloLib.INSTANCE;
|
||||
log.info("{}", hello.add(1,2));
|
||||
}
|
||||
}
|
BIN
src/main/resources/lib/win64/hello.dll
Normal file
BIN
src/main/resources/lib/win64/hello.dll
Normal file
Binary file not shown.
@ -1,5 +1,8 @@
|
||||
package cn.skcks.jnr.sevenzip.sevenzip;
|
||||
|
||||
import com.sun.jna.Library;
|
||||
import com.sun.jna.Native;
|
||||
import lombok.SneakyThrows;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
@ -10,8 +13,25 @@ import org.springframework.test.context.junit.jupiter.SpringExtension;
|
||||
@ExtendWith(SpringExtension.class)
|
||||
@SpringBootTest(classes = ApplicationTest.class)
|
||||
public class SimpleTest {
|
||||
|
||||
public interface HelloLib extends Library {
|
||||
HelloLib INSTANCE = Native.load(loadLib("/lib/win64/hello.dll"), HelloLib.class);
|
||||
|
||||
int add(int a,int b);
|
||||
}
|
||||
|
||||
@SneakyThrows
|
||||
synchronized static String loadLib(String resourcePath){
|
||||
String path = Native.extractFromResourcePath(resourcePath).getAbsolutePath();
|
||||
log.info("{}", path);
|
||||
return path;
|
||||
}
|
||||
|
||||
@Test
|
||||
void context(){
|
||||
@SneakyThrows
|
||||
public void context(){
|
||||
log.info("测试");
|
||||
HelloLib hello = HelloLib.INSTANCE;
|
||||
log.info("{}", hello.add(1,2));
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user