Skip to content

Instantly share code, notes, and snippets.

@benravago
Last active May 4, 2024 07:06
Show Gist options
  • Select an option

  • Save benravago/57dd6026f6a89091d6b14d957324ce40 to your computer and use it in GitHub Desktop.

Select an option

Save benravago/57dd6026f6a89091d6b14d957324ce40 to your computer and use it in GitHub Desktop.
A jni-to-jlink cheatsheet
gcc = gcc
jdk = ./jdk
jar = $(jdk)/bin/jar
java = $(jdk)/bin/java
javac = $(jdk)/bin/javac
jlink = $(jdk)/bin/jlink
jmod = $(jdk)/bin/jmod
build = ./build
bin = $(build)/bin
classes = $(build)/classes
conf = $(build)/conf
dist = $(build)/dist
include = $(build)/include
jmods = $(build)/jmods
lib = $(build)/lib
legal =$(build)/legal
man = $(build)/man
native = $(build)/native
tmp = $(build)/tmp
demo:
mkdir -p $(tmp)
sed '/package/d' src/hello/world/Main.java > $(tmp)/Main.java
$(javac) -cp $(classes) $(tmp)/Main.java
$(jar) cvfe $(dist)/demo.jar Main -C $(tmp) Main.class
cd $(dist); bin/java -jar demo.jar
image: $(dist)
$(dist): module
$(jlink) --module-path $(jmods) --add-modules hello --output $@
module: $(jmods)/hello.jmod
$(jmods)/hello.jmod: $(classes)/module-info.class $(bin)/main.sh $(native)/libhello.so $(lib)/main.jar
mkdir -p $(conf) $(legal) $(man)
cp -v makefile $(conf)
cp -v README.md $(man)
cp -v LICENSE $(legal)
cp -v src/hello.sh $(bin)
$(jmod) create --main-class hello.world.Main --class-path $(classes) --cmds $(bin) --config $(conf) --header-files $(include) --legal-notices $(legal) --libs $(lib):$(native) --man-pages $(man) $@
$(classes)/module-info.class: src/hello/module-info.java $(lib)/main.jar
$(javac) -d $(classes) $<
$(lib)/main.jar: src/hello/world/Main.java
$(javac) -d $(classes) -sourcepath src $<
$(jar) cvfe $@ hello.world.Main -C $(classes) hello/world/Main.class
$(include)/hello_Say.h: src/hello/Say.java src/hello/Say.c
$(javac) -d $(classes) -sourcepath src -h $(include) $<
$(native)/libhello.so: src/hello/Say.c $(include)/hello_Say.h $(native)/libsay.so
$(gcc) -Wall -shared -fPIC -Ijdk/include -Ijdk/include/linux -I$(include) -L$(native) -lsay -o $@ $<
$(bin)/main.sh: src/main.sh $(bin)/main
cp -v $< $@
$(bin)/main: src/main.c $(native)/libsay.so
$(gcc) -Wall -L$(native)/ -lsay -o $@ $<
$(native)/libsay.so: src/say.c src/say.h
$(gcc) -Wall -shared -fPIC -o $@ $<
clean:
rm -fr $(dist)
find $(build) -type f -exec rm {} ';'
test: $(bin)/main.sh $(lib)/main.jar
sh $(bin)/main.sh
LD_LIBRARY_PATH=$(native) $(java) -cp $(classes) hello.world.Main
$(build):
mkdir -pv $@/{bin,classes,conf,include,legal,man,native}
mkdir -pv $@/{dist,lib}
#
set -x
cd $(dirname $0)
./java -jar ../lib/main.jar
module hello {
exports hello;
}
#include <jni.h>
#include <hello_Say.h>
#include "../say.h"
JNIEXPORT void JNICALL Java_hello_Say_call_1say
(JNIEnv *env, jobject obj)
{
say();
}
package hello;
public class Say {
static { System.loadLibrary("hello"); }
public native void call_say();
public void hello() {
call_say();
}
}
package hello.world;
import hello.Say;
class Main {
public static void main(String[] args) throws Exception {
new Say().hello();
}
}
#include "say.h"
int main(void) {
say();
return 0;
}
#
set -x
cd $(dirname $0)
LD_LIBRARY_PATH=$(find .. -maxdepth 2 -name libsay.so -printf %h) ./main $*
#include <stdio.h>
#include "say.h"
void say() {
puts("Hello world!\n");
}
void say();
#
cd $(dirname $0)
find . -name '*_*' | while read F; do
D=${F//_/\/}
mkdir -pv $(dirname $D)
mv -v ${F} ${D}
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment