-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Create JAR file for Java native interface
Build with option `BUILD_JNI=1` would compile the Java code and create the JAR file. The dcurl shared library `libdcurl.so` would be packaged into the JAR file and can be extracted from it for Jave native interface easily. Close #106.
- Loading branch information
Showing
4 changed files
with
73 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package org.dltcollab; | ||
|
||
import java.io.*; | ||
import java.nio.file.Files; | ||
import java.nio.file.StandardCopyOption; | ||
|
||
public class Dcurl { | ||
private static final String libraryName = "dcurl"; | ||
private static final String libraryPrefix = "lib"; | ||
private static final String librarySuffix = ".so"; | ||
private static final String libraryFileName = libraryPrefix + libraryName + librarySuffix; | ||
|
||
public void loadLibraryFromJar() { | ||
final File temp; | ||
try { | ||
temp = File.createTempFile(libraryPrefix + libraryName, librarySuffix); | ||
if (temp.exists() && !temp.delete()) { | ||
throw new RuntimeException("File: " + temp.getAbsolutePath() + " already exists and cannot be removed."); | ||
} | ||
if (!temp.createNewFile()) { | ||
throw new RuntimeException("File: " + temp.getAbsolutePath() + " could not be created."); | ||
} | ||
|
||
if (!temp.exists()) { | ||
throw new RuntimeException("File " + temp.getAbsolutePath() + " does not exist."); | ||
} else { | ||
temp.deleteOnExit(); | ||
} | ||
|
||
// attempt to copy the library from the Jar file to the temp destination | ||
try (final InputStream is = getClass().getClassLoader().getResourceAsStream(libraryFileName)) { | ||
if (is == null) { | ||
throw new RuntimeException(libraryFileName + " was not found inside JAR."); | ||
} else { | ||
Files.copy(is, temp.toPath(), StandardCopyOption.REPLACE_EXISTING); | ||
} | ||
} | ||
|
||
System.load(temp.getAbsolutePath()); | ||
} catch (IOException e) { | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters