Skip to content

Commit

Permalink
feat: Create JAR file for Java native interface
Browse files Browse the repository at this point in the history
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
marktwtn committed Apr 24, 2019
1 parent bef02b8 commit 22526da
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 7 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ docs/html
*.obj
*.elf

# Precompiled Headers
# Precompiled headers
*.gch
*.pch

Expand All @@ -23,3 +23,6 @@ docs/html
*.so
*.so.*
*.dylib

# Java bytecode
*.class
12 changes: 11 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
VERSION = 0.2.0

OUT ?= ./build
SRC := src

Expand Down Expand Up @@ -89,7 +91,15 @@ TESTS := $(addprefix $(OUT)/test-, $(TESTS))
LIBS = libdcurl.so
LIBS := $(addprefix $(OUT)/, $(LIBS))

all: config $(TESTS) $(LIBS)
JARS := dcurljni-$(VERSION).jar
JARS := $(addprefix $(OUT)/, $(JARS))

PREQ := config $(TESTS) $(LIBS)
ifeq ("$(BUILD_JNI)","1")
PREQ += $(JARS)
endif

all: $(PREQ)
.DEFAULT_GOAL := all

OBJS = \
Expand Down
43 changes: 43 additions & 0 deletions java/org/dltcollab/Dcurl.java
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) {
}
}
}
20 changes: 15 additions & 5 deletions mk/java.mk
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
UNAME_S := $(shell uname -s)

JAVAC := $(shell which javac)
ifndef JAVAC
$(error "javac is not available. Please check JDK installation")
endif

# if JAVA_HOME is not set, guess it according to system configurations
ifndef JAVA_HOME
ifeq ($(UNAME_S),Darwin)
# macOS
JAVA_HOME := $(shell /usr/libexec/java_home)
else
# Default to Linux
JAVAC := $(shell which javac)
ifndef JAVAC
$(error "javac is not available. Please check JDK installation")
endif
# Default to Linux
JAVA_HOME := $(shell readlink -f $(JAVAC) | sed "s:bin/javac::")
endif
endif # JAVA_HOME
Expand Down Expand Up @@ -60,3 +61,12 @@ jni/iri-pearldiver-exlib.c: $(OUT)/jni/iri-pearldiver-exlib.h
$(OUT)/jni/%.o: jni/%.c
$(VECHO) " CC\t$@\n"
$(Q)$(CC) -o $@ $(CFLAGS) $(CFLAGS_JNI) -c -MMD -MF $@.d $<

java/org/dltcollab/%.class: java/org/dltcollab/%.java
$(VECHO) " JAVAC\t$@\n"
$(Q)$(JAVAC) $<

$(OUT)/dcurljni-$(VERSION).jar: $(OUT)/libdcurl.so java/org/dltcollab/Dcurl.class
$(VECHO) " JAR\t$@\n"
$(Q)jar -cf $@ -C $(OUT) libdcurl.so
$(Q)jar -uf $@ -C java org/dltcollab/Dcurl.class

0 comments on commit 22526da

Please sign in to comment.