I'm trying to compile a jar file with Gradle and it works fine except when I run the jar I get an UnsatisfiedLinkError due to EvalC.dll not being found in java.library.path.
When I run the project inside IntelliJ using Gradle I just specify the following the the Gradle task and it works fine:
systemProperty "java.library.path", "$projectDir\\libs"
I added the same line to the Gradle task for building the jar but that didn't work.
I also tried adding
static
{
System.setProperty("java.library.path", "libs");
}
to my Main class but that doesn't work either. Neither does it work using the full path. The libs directory is in the project folder along with src and resources etc. so the paths would be /ProjectFolder/libs, /ProjectFolder/src etc.
I know I can specify java.library.path on the command line (which I haven't tried because it isn't what I want). The user needs to be able to double click the jar and run it.
What is the correct way to get EvalC.dll to be found correctly without specifying command line arguments? I don't mind if it's via code or via the Gradle task.
EDIT - Minimal Example:
public class TestApp extends Application
{
private static void loadJarDLL(String name) throws IOException
{
InputStream in = TestApp.class.getResourceAsStream(name);
byte[] buffer = new byte[1024];
int read;
File temp = File.createTempFile(name, "");
FileOutputStream fos = new FileOutputStream(temp);
while((read = in.read(buffer)) != -1)
{
fos.write(buffer, 0, read);
}
fos.close();
in.close();
System.load(temp.getAbsolutePath());
}
public static void main(String[] args)
{
launch(args);
}
int numPlayers = 3;
int[] playerHands = {3, 14, 2, 6, 3, 2, 8, 4, 8, 3, 7, 2, 3, 3, 2};
int[] comCards = {11, 2, 4, 1, 10, 2, 8, 2, 2, 1};
public static native int[] evaluateCards(int playersSize, int[] playerHands, int[] comCards);
@Override
public void start(Stage stage) throws IOException
{
try
{
loadJarDLL("EvalC.dll");
}
catch(IOException e)
{
e.printStackTrace();
}
evaluateCards(numPlayers, playerHands, comCards);
}
}
Gradle Run:
run
{
systemProperty "java.library.path", "$projectDir\\libs"
}
Gradle Build Jar:
jar
{
manifest
{
attributes 'Main-Class': "$mainClassName"
}
from
{
configurations.compile.collect { it.isDirectory() ? it : zipTree(it) }
}
destinationDir = file("$rootDir/bin")
}
EDIT 2 - Link to download zipped example project:
https://drive.google.com/open?id=0B3-S5JAgvkPDTDlKOXlNYWxwdmc