Last active
May 4, 2024 07:06
-
-
Save benravago/57dd6026f6a89091d6b14d957324ce40 to your computer and use it in GitHub Desktop.
A jni-to-jlink cheatsheet
This file contains hidden or 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 gist provides a simple example of the various steps involved in making a java runtime image using the new jlink tool. | |
| This example also has simple jni and shared library parts to show how jmod and jlink deal with those elements. | |
| To build, just download the gist zip file, unzip that and cd into the gist directory, then: | |
| # sh ungist | |
| # ln -s ${JAVA_HOME} jdk | |
| # make build | |
| # make image | |
| # make demo | |
| 'make build' creates the build directory structure to any compilation targets. | |
| 'make image' generates the runtime image (in ./build/dist). | |
| 'make demo' runs a small demo program that uses the generated runtime image. |
This file contains hidden or 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
| 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} |
This file contains hidden or 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
| # | |
| set -x | |
| cd $(dirname $0) | |
| ./java -jar ../lib/main.jar |
This file contains hidden or 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
| module hello { | |
| exports hello; | |
| } |
This file contains hidden or 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
| #include <jni.h> | |
| #include <hello_Say.h> | |
| #include "../say.h" | |
| JNIEXPORT void JNICALL Java_hello_Say_call_1say | |
| (JNIEnv *env, jobject obj) | |
| { | |
| say(); | |
| } |
This file contains hidden or 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
| package hello; | |
| public class Say { | |
| static { System.loadLibrary("hello"); } | |
| public native void call_say(); | |
| public void hello() { | |
| call_say(); | |
| } | |
| } |
This file contains hidden or 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
| package hello.world; | |
| import hello.Say; | |
| class Main { | |
| public static void main(String[] args) throws Exception { | |
| new Say().hello(); | |
| } | |
| } |
This file contains hidden or 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
| #include "say.h" | |
| int main(void) { | |
| say(); | |
| return 0; | |
| } |
This file contains hidden or 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
| # | |
| set -x | |
| cd $(dirname $0) | |
| LD_LIBRARY_PATH=$(find .. -maxdepth 2 -name libsay.so -printf %h) ./main $* |
This file contains hidden or 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
| #include <stdio.h> | |
| #include "say.h" | |
| void say() { | |
| puts("Hello world!\n"); | |
| } |
This file contains hidden or 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
| void say(); |
This file contains hidden or 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
| # | |
| 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