-
-
Notifications
You must be signed in to change notification settings - Fork 390
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add example using JNI #3321
Merged
Merged
Add example using JNI #3321
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,59 @@ | ||
import mill._, javalib._ | ||
|
||
object foo extends RootModule with JavaModule { | ||
// Additional source folder to put C sources | ||
def nativeSources = T.sources(millSourcePath / "native-src") | ||
|
||
// Auto-generate JNI `.h` files from Java classes using Javac | ||
def nativeHeaders = T { | ||
os.proc("javac", "-h", T.dest, "-d", T.dest.toString, allSourceFiles().map(_.path)).call() | ||
PathRef(T.dest) | ||
} | ||
|
||
// Compile C | ||
def nativeCompiled = T{ | ||
val cSourceFiles = nativeSources().map(_.path).flatMap(os.walk(_)).filter(_.ext == "c") | ||
val output = "libhelloworld.so" | ||
os.proc( | ||
"clang", "-shared", "-fPIC", | ||
"-I" + nativeHeaders().path, // | ||
"-I" + sys.props("java.home") + "/include/", // global JVM header files | ||
"-I" + sys.props("java.home") + "/include/darwin", | ||
"-I" + sys.props("java.home") + "/include/linux", | ||
"-o", T.dest / output, | ||
cSourceFiles | ||
) | ||
.call(stdout = os.Inherit) | ||
|
||
PathRef(T.dest / output) | ||
} | ||
|
||
def forkEnv = Map("HELLO_WORLD_BINARY" -> nativeCompiled().path.toString) | ||
|
||
object test extends JavaTests with TestModule.Junit4{ | ||
def forkEnv = Map("HELLO_WORLD_BINARY" -> nativeCompiled().path.toString) | ||
} | ||
} | ||
|
||
// This is an example of how use Mill to compile C code together with your Java | ||
// code using JNI. There are three main steps: defining the C source folder, | ||
// generating the header files using `javac`, and then compiling the C code | ||
// using `clang`. After that we have the `libhelloworld.so` on disk ready to use, | ||
// and in this example we use an environment variable to pass the path of that | ||
// file to the application code to load it using `System.load`. | ||
// | ||
// This example is pretty minimal, but it demonstrates the core principles, and | ||
// can be extended if necessary to more elaborate use cases. The `native*` tasks | ||
// can also be extracted out into a `trait` for re-use if you have multiple | ||
// `JavaModule`s that need native C components | ||
|
||
/** Usage | ||
|
||
> ./mill run | ||
Hello, World! | ||
|
||
> ./mill test | ||
Test foo.HelloWorldTest.testSimple started | ||
Test foo.HelloWorldTest.testSimple finished... | ||
... | ||
*/ |
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,8 @@ | ||
#include <jni.h> | ||
#include "foo_HelloWorld.h" | ||
#include <string.h> | ||
|
||
// Implementation of the native method | ||
JNIEXPORT jstring JNICALL Java_foo_HelloWorld_sayHello(JNIEnv *env, jobject obj) { | ||
return (*env)->NewStringUTF(env, "Hello, World!"); | ||
} |
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,16 @@ | ||
package foo; | ||
|
||
public class HelloWorld { | ||
// Declare a native method | ||
public native String sayHello(); | ||
|
||
// Load the native library | ||
static { | ||
System.load(System.getenv("HELLO_WORLD_BINARY")); | ||
} | ||
|
||
public static void main(String[] args) { | ||
HelloWorld helloWorld = new HelloWorld(); | ||
System.out.println(helloWorld.sayHello()); | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
example/javamodule/12-jni/test/src/foo/HelloWorldTest.java
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,10 @@ | ||
package foo; | ||
import static org.junit.Assert.assertEquals; | ||
import org.junit.Test; | ||
|
||
public class HelloWorldTest { | ||
@Test | ||
public void testSimple() { | ||
assertEquals(new HelloWorld().sayHello(), "Hello, World!"); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We have
Jvm.jdkTool
, which tries to look up the tool in the currently used JRE/JDK.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks! I'll use that