JAR
- modified library loading - jar now is os indipendent (win11 only tested)
This commit is contained in:
2
.gitignore
vendored
2
.gitignore
vendored
@@ -399,4 +399,4 @@ FodyWeavers.xsd
|
||||
|
||||
# CUSTOM
|
||||
target
|
||||
jsmile*
|
||||
*jsmile*
|
||||
|
||||
64
pom.xml
64
pom.xml
@@ -46,7 +46,7 @@
|
||||
</activation>
|
||||
<properties>
|
||||
<nativeSuffix>macos</nativeSuffix>
|
||||
<smileFile>jsmile.jnilib</smileFile>
|
||||
<smileFile>libjsmile.jnilib</smileFile>
|
||||
</properties>
|
||||
</profile>
|
||||
<profile>
|
||||
@@ -70,7 +70,7 @@
|
||||
</activation>
|
||||
<properties>
|
||||
<nativeSuffix>linux64</nativeSuffix>
|
||||
<smileFile>jsmile.so</smileFile>
|
||||
<smileFile>libjsmile.so</smileFile>
|
||||
</properties>
|
||||
</profile>
|
||||
</profiles>
|
||||
@@ -92,7 +92,47 @@
|
||||
<artifactItems>
|
||||
<artifactItem>
|
||||
<groupId>com.bayesfusion</groupId>
|
||||
<artifactId>jsmile-native-${nativeSuffix}</artifactId>
|
||||
<artifactId>jsmile-native-win64</artifactId>
|
||||
<version>${smile.version}</version>
|
||||
<type>zip</type>
|
||||
<overWrite>true</overWrite>
|
||||
<includes>*jsmile*</includes>
|
||||
</artifactItem>
|
||||
</artifactItems>
|
||||
<outputDirectory>${project.basedir}/src/main/resources/</outputDirectory>
|
||||
</configuration>
|
||||
</execution>
|
||||
<execution>
|
||||
<id>resource-dependencies</id>
|
||||
<phase>compile</phase>
|
||||
<goals>
|
||||
<goal>unpack</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<artifactItems>
|
||||
<artifactItem>
|
||||
<groupId>com.bayesfusion</groupId>
|
||||
<artifactId>jsmile-native-macos</artifactId>
|
||||
<version>${smile.version}</version>
|
||||
<type>zip</type>
|
||||
<overWrite>true</overWrite>
|
||||
<includes>*jsmile*</includes>
|
||||
</artifactItem>
|
||||
</artifactItems>
|
||||
<outputDirectory>${project.basedir}/src/main/resources/</outputDirectory>
|
||||
</configuration>
|
||||
</execution>
|
||||
<execution>
|
||||
<id>resource-dependencies</id>
|
||||
<phase>compile</phase>
|
||||
<goals>
|
||||
<goal>unpack</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<artifactItems>
|
||||
<artifactItem>
|
||||
<groupId>com.bayesfusion</groupId>
|
||||
<artifactId>jsmile-native-linux64</artifactId>
|
||||
<version>${smile.version}</version>
|
||||
<type>zip</type>
|
||||
<overWrite>true</overWrite>
|
||||
@@ -104,23 +144,12 @@
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
<!-- Renaming the jsmile & make a copy to test resources -->
|
||||
<!-- Make a copy of JSMILE to test resources -->
|
||||
<plugin>
|
||||
<groupId>com.coderplus.maven.plugins</groupId>
|
||||
<artifactId>copy-rename-maven-plugin</artifactId>
|
||||
<version>1.0</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>rename-file</id>
|
||||
<phase>compile</phase>
|
||||
<goals>
|
||||
<goal>copy</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<sourceFile>${project.basedir}/src/main/resources/${smileFile}</sourceFile>
|
||||
<destinationFile>${project.basedir}/src/main/resources/jsmile</destinationFile>
|
||||
</configuration>
|
||||
</execution>
|
||||
<execution>
|
||||
<id>copy-file</id>
|
||||
<phase>compile</phase>
|
||||
@@ -128,12 +157,7 @@
|
||||
<goal>copy</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<!-- IDK Why but using 2 files makes the loading work, so i copy them in test too -->
|
||||
<fileSets>
|
||||
<fileSet>
|
||||
<sourceFile>${project.basedir}/src/main/resources/jsmile</sourceFile>
|
||||
<destinationFile>${project.basedir}/src/test/resources/jsmile</destinationFile>
|
||||
</fileSet>
|
||||
<fileSet>
|
||||
<sourceFile>${project.basedir}/src/main/resources/${smileFile}</sourceFile>
|
||||
<destinationFile>${project.basedir}/src/test/resources/${smileFile}</destinationFile>
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
package net.berack.upo.ai.problem3;
|
||||
|
||||
import java.io.File;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
@@ -21,12 +23,25 @@ import smile.Network;
|
||||
public class SmileLib {
|
||||
|
||||
static {
|
||||
try {
|
||||
var jsmile = "jsmile";
|
||||
var jsmile = jsmileLibName();
|
||||
var loader = SmileLib.class.getClassLoader();
|
||||
var resource = loader.getResource(jsmile);
|
||||
|
||||
try {
|
||||
var uri = resource.toURI();
|
||||
var path = Path.of(uri).toString();
|
||||
var path = "";
|
||||
|
||||
if(uri.toString().startsWith("jar")) {
|
||||
var tmp = System.getProperty("java.io.tmpdir");
|
||||
var file = new File(tmp + jsmile);
|
||||
if(!file.exists()) {
|
||||
var in = resource.openStream();
|
||||
Files.copy(in, file.toPath());
|
||||
}
|
||||
path = file.getAbsolutePath();
|
||||
}
|
||||
else path = Path.of(uri).toString();
|
||||
|
||||
System.setProperty("jsmile.native.library", path);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
@@ -53,6 +68,21 @@ public class SmileLib {
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Ritorna il nome della libreria jsmile in base al sistema operativo.
|
||||
* @return il node del file
|
||||
*/
|
||||
private static String jsmileLibName() {
|
||||
var os = System.getProperty("os.name").toLowerCase();
|
||||
if(os.indexOf("win") >= 0) return "jsmile.dll";
|
||||
if(os.indexOf("mac") >= 0) return "libjsmile.jnilib";
|
||||
if(os.indexOf("nix") >= 0
|
||||
|| os.indexOf("nux") >= 0
|
||||
|| os.indexOf("aix") >= 0) return "libjsmile.so";
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Crea un Network dal file indicato
|
||||
* Il file deve essere una risorsa del jar o un file esterno
|
||||
|
||||
Reference in New Issue
Block a user