From 22526dacf106a4f1585ad32cbd3b10d8ec2a8827 Mon Sep 17 00:00:00 2001 From: marktwtn Date: Fri, 19 Apr 2019 04:11:13 +0800 Subject: [PATCH] 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. --- .gitignore | 5 +++- Makefile | 12 +++++++++- java/org/dltcollab/Dcurl.java | 43 +++++++++++++++++++++++++++++++++++ mk/java.mk | 20 ++++++++++++---- 4 files changed, 73 insertions(+), 7 deletions(-) create mode 100644 java/org/dltcollab/Dcurl.java diff --git a/.gitignore b/.gitignore index 34cb540..bb17d56 100644 --- a/.gitignore +++ b/.gitignore @@ -8,7 +8,7 @@ docs/html *.obj *.elf -# Precompiled Headers +# Precompiled headers *.gch *.pch @@ -23,3 +23,6 @@ docs/html *.so *.so.* *.dylib + +# Java bytecode +*.class diff --git a/Makefile b/Makefile index 0987a4e..475bb46 100644 --- a/Makefile +++ b/Makefile @@ -1,3 +1,5 @@ +VERSION = 0.2.0 + OUT ?= ./build SRC := src @@ -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 = \ diff --git a/java/org/dltcollab/Dcurl.java b/java/org/dltcollab/Dcurl.java new file mode 100644 index 0000000..c1085bf --- /dev/null +++ b/java/org/dltcollab/Dcurl.java @@ -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) { + } + } +} diff --git a/mk/java.mk b/mk/java.mk index af0e09d..b8c6812 100644 --- a/mk/java.mk +++ b/mk/java.mk @@ -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 @@ -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