JNA 简单试水

This commit is contained in:
shikong 2023-10-21 22:19:53 +08:00
parent e8953abe12
commit 2396f620fb
5 changed files with 117 additions and 4 deletions

60
pom.xml
View File

@ -23,6 +23,8 @@
<java.version>17</java.version> <java.version>17</java.version>
<springboot.version>3.1.2</springboot.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--> <!--Java Bean-->
<org.mapstruct.version>1.5.3.Final</org.mapstruct.version> <org.mapstruct.version>1.5.3.Final</org.mapstruct.version>
@ -32,9 +34,15 @@
<dependencies> <dependencies>
<dependency> <dependency>
<groupId>com.github.jnr</groupId> <groupId>net.java.dev.jna</groupId>
<artifactId>jnr-ffi</artifactId> <artifactId>jna</artifactId>
<version>2.2.15</version> <version>5.13.0</version>
</dependency>
<dependency>
<groupId>net.java.dev.jna</groupId>
<artifactId>jna-platform</artifactId>
<version>5.13.0</version>
</dependency> </dependency>
<dependency> <dependency>
@ -54,6 +62,20 @@
<scope>test</scope> <scope>test</scope>
</dependency> </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--> <!--MapStruct-->
<dependency> <dependency>
<groupId>org.mapstruct</groupId> <groupId>org.mapstruct</groupId>
@ -78,6 +100,28 @@
<build> <build>
<plugins> <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> <plugin>
<groupId>org.apache.maven.plugins</groupId> <groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId> <artifactId>maven-compiler-plugin</artifactId>
@ -121,6 +165,16 @@
<delimiter>@</delimiter> <delimiter>@</delimiter>
</delimiters> </delimiters>
<useDefaultDelimiters>false</useDefaultDelimiters> <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> </configuration>
</plugin> </plugin>

View File

@ -1,7 +1,9 @@
package cn.skcks.jnr.sevenzip; package cn.skcks.jnr.sevenzip;
import cn.skcks.jnr.sevenzip.hello.Hello;
import org.springframework.boot.SpringApplication; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
@SpringBootApplication @SpringBootApplication
public class Application { public class Application {

View 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));
}
}

Binary file not shown.

View File

@ -1,5 +1,8 @@
package cn.skcks.jnr.sevenzip.sevenzip; 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 lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith; import org.junit.jupiter.api.extension.ExtendWith;
@ -10,8 +13,25 @@ import org.springframework.test.context.junit.jupiter.SpringExtension;
@ExtendWith(SpringExtension.class) @ExtendWith(SpringExtension.class)
@SpringBootTest(classes = ApplicationTest.class) @SpringBootTest(classes = ApplicationTest.class)
public class SimpleTest { 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 @Test
void context(){ @SneakyThrows
public void context(){
log.info("测试"); log.info("测试");
HelloLib hello = HelloLib.INSTANCE;
log.info("{}", hello.add(1,2));
} }
} }