{"id":15047965,"url":"https://github.com/mizux/java-native","last_synced_at":"2025-09-19T10:55:58.509Z","repository":{"id":74261470,"uuid":"263293187","full_name":"Mizux/java-native","owner":"Mizux","description":"Template to build a multi-platforms Native Java Maven package","archived":false,"fork":false,"pushed_at":"2024-01-29T12:51:41.000Z","size":145,"stargazers_count":2,"open_issues_count":0,"forks_count":3,"subscribers_count":3,"default_branch":"main","last_synced_at":"2024-10-13T16:23:13.945Z","etag":null,"topics":["cmake","cpp","java","native-library","swig"],"latest_commit_sha":null,"homepage":"","language":"C++","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Mizux.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":"AUTHORS"}},"created_at":"2020-05-12T09:34:14.000Z","updated_at":"2024-06-30T12:46:02.000Z","dependencies_parsed_at":"2024-01-29T14:49:18.342Z","dependency_job_id":"c7eed65e-9dbd-41bd-8111-2a90ab809a51","html_url":"https://github.com/Mizux/java-native","commit_stats":null,"previous_names":[],"tags_count":0,"template":true,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Mizux%2Fjava-native","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Mizux%2Fjava-native/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Mizux%2Fjava-native/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Mizux%2Fjava-native/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Mizux","download_url":"https://codeload.github.com/Mizux/java-native/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":225365988,"owners_count":17462973,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cmake","cpp","java","native-library","swig"],"created_at":"2024-09-24T21:06:21.572Z","updated_at":"2025-09-19T10:55:53.460Z","avatar_url":"https://github.com/Mizux.png","language":"C++","readme":"Github-CI:\n[![Build Status][github_linux_status]][github_linux_link]\n[![Build Status][github_macos_status]][github_macos_link]\n[![Build Status][github_windows_status]][github_windows_link]\n[![Build Status][github_amd64_docker_status]][github_amd64_docker_link]\n\n[github_linux_status]: https://github.com/Mizux/java-native/actions/workflows/amd64_linux.yml/badge.svg\n[github_linux_link]: https://github.com/Mizux/java-native/actions/workflows/amd64_linux.yml\n[github_macos_status]: https://github.com/Mizux/java-native/actions/workflows/amd64_macos.yml/badge.svg\n[github_macos_link]: https://github.com/Mizux/java-native/actions/workflows/amd64_macos.yml\n[github_windows_status]: https://github.com/Mizux/java-native/actions/workflows/amd64_windows.yml/badge.svg\n[github_windows_link]: https://github.com/Mizux/java-native/actions/workflows/amd64_windows.yml\n[github_amd64_docker_status]: https://github.com/Mizux/java-native/actions/workflows/amd64_docker.yml/badge.svg\n[github_amd64_docker_link]: https://github.com/Mizux/java-native/actions/workflows/amd64_docker.yml\n\n# Introduction\n\u003cnav for=\"project\"\u003e |\n\u003ca href=\"#requirement\"\u003eRequirement\u003c/a\u003e |\n\u003ca href=\"#codemap\"\u003eCodemap\u003c/a\u003e |\n\u003ca href=\"#dependencies\"\u003eDependencies\u003c/a\u003e |\n\u003ca href=\"#build-process\"\u003eBuild\u003c/a\u003e |\n\u003ca href=\"ci/README.md\"\u003eCI\u003c/a\u003e |\n\u003ca href=\"#appendices\"\u003eAppendices\u003c/a\u003e |\n\u003ca href=\"#license\"\u003eLicense\u003c/a\u003e |\n\u003c/nav\u003e\n\nThis is an example of how to create a Modern [CMake](https://cmake.org/) C++/Java Project.\n\nThis project aim to explain how you build a Java 1.8 native (for win32-x86-64,\nlinux-x86-64 and darwin-x86-64) maven multiple package using [`mvn`](http://maven.apache.org/)\nand few [POM.xml](http://maven.apache.org/pom.html).  \ne.g. You have a cross platform C++ library and a JNI wrapper on it thanks to SWIG.\u003cbr\u003e\nThen you want to provide a cross-platform Maven package to consume it in a\nMaven project...\n\nThis project should run on:\n\n* MacOS (darwin-aarch64, darwin-x86-64)\n* GNU/Linux (linux-aarch64, linux-x86-64)\n* Windows (win32-x86-64)\n\n## Requirement\nYou'll need:\n\n* \"CMake \u003e= 3.18\".\n* \"Java SDK \u003e= 1.8\" and \"Maven \u003e= 3.6\".\n\nPlease verify you also have the `JAVA_HOME` environment variable set otherwise CMake\nand Maven won't be able to find your Java SDK.\n\n## Codemap\nThe project layout is as follow:\n\n* [CMakeLists.txt](CMakeLists.txt) Top-level for [CMake](https://cmake.org/cmake/help/latest/) based build.\n* [cmake](cmake) Subsidiary CMake files.\n  * [java.cmake](cmake/java.cmake) All internall Java CMake stuff.\n\n* [ci](ci) Root directory for continuous integration.\n\n* [Foo](Foo) Root directory for `Foo` library.\n  * [CMakeLists.txt](Foo/CMakeLists.txt) for `Foo`.\n  * [include](Foo/include) public folder.\n    * [foo](Foo/include/foo)\n      * [Foo.hpp](Foo/include/foo/Foo.hpp)\n  * [src](Foo/src) private folder.\n    * [src/Foo.cpp](Foo/src/Foo.cpp)\n  * [java](Foo/java)\n    * [CMakeLists.txt](Foo/java/CMakeLists.txt) for `Foo` Java.\n    * [foo.i](Foo/java/foo.i) SWIG Java wrapper.\n* [Bar](Bar) Root directory for `Bar` library.\n  * [CMakeLists.txt](Bar/CMakeLists.txt) for `Bar`.\n  * [include](Bar/include) public folder.\n    * [bar](Bar/include/bar)\n      * [Bar.hpp](Bar/include/bar/Bar.hpp)\n  * [src](Bar/src) private folder.\n    * [src/Bar.cpp](Bar/src/Bar.cpp)\n  * [java](Bar/java)\n    * [CMakeLists.txt](Bar/java/CMakeLists.txt) for `Bar` Java.\n    * [bar.i](Bar/java/bar.i) SWIG Java wrapper.\n* [FooBar](FooBar) Root directory for `FooBar` library.\n  * [CMakeLists.txt](FooBar/CMakeLists.txt) for `FooBar`.\n  * [include](FooBar/include) public folder.\n    * [foobar](FooBar/include/foobar)\n      * [FooBar.hpp](FooBar/include/foobar/FooBar.hpp)\n  * [src](FooBar/src) private folder.\n    * [src/FooBar.cpp](FooBar/src/FooBar.cpp)\n  * [java](FooBar/java)\n    * [CMakeLists.txt](FooBar/java/CMakeLists.txt) for `FooBar` Java.\n    * [foobar.i](FooBar/java/foobar.i) SWIG Java wrapper.\n\n* [java](java) Root directory for Java template files\n  * [base.i](java/base.i) Generic SWIG stuff (e.g. fixing int64 java typemaps).\n  * [pom-native.xml.in](java/) POM template to build the native project.\n  * [Loader.java](java/Loader.java) Unpack and load the correct native libraries.\n  * [pom-local.xml.in](java/pom-local.xml.in) POM template to build the \"pure\" Java project.\n  * [Test.java](java/Test.java) Test source code to verify the Java wrapper is working.\n  * [pom-test.xml.in](java/pom-test.xml.in) POM template to build the test project.\n\n## Dependencies\nTo complexify a little, the CMake project is composed of three libraries (Foo, Bar and FooBar)\nwith the following dependencies:  \n```sh\nFoo:\nBar:\nFooBar: PUBLIC Foo PRIVATE Bar\n```\n\n## Build Process\nTo Create a native dependent package we will split it in two parts:\n\n* A bunch of `org.mizux.javanative:javanative-{platform}` maven packages for each\nsupported platform targeted and containing the native libraries.\n* A generic maven package `org.mizux.javanative:javanative-java` depending on each native\npackages and containing the Java code.\n\n[`platform` names](https://github.com/java-native-access/jna/blob/cc1acdac02e4d0dda93ba01bbe3a3435b8933dab/test/com/sun/jna/PlatformTest.java#L31-L100) come from the JNA project (Java Native Access) which will be use to find at runtime on which platform the code is currently running.\n\n### Local Package\n\nThe pipeline for `linux-x86-64` should be as follow:  \nnote: The pipeline will be similar for other architecture,\ndon't hesitate to look at the CI log! ![Local Pipeline](docs/local_pipeline.svg)\n![Legend](docs/legend.svg)\n\n#### Building local native Package\n\ndisclaimer: In this git repository, we use `CMake` and `SWIG`.  \nThus we have the C++ shared library `libFoo.so` and the SWIG generated Java wrapper `Foo.java`.  \nnote: For a C++ CMake cross-platform project sample, take a look at [Mizux/cmake-cpp](https://github.com/Mizux/cmake-cpp).   \nnote: For a C++/Swig CMake cross-platform project sample, take a look at [Mizux/cmake-swig](https://github.com/Mizux/cmake-swig). \n\nSo first let's create the local `org.mizux.javanative:javanative-{platform}.jar`\nmaven package.\n\nHere some dev-note concerning this `POM.xml`.\n* This package is a native package only containing native libraries.\n\nThen you can generate the package and install it locally using:\n```bash\nmvn package\nmvn install\n```\nnote: this will automatically trigger the `mvn compile` phase.\n\nIf everything good the package (located in\n`\u003cbuildir\u003e/java/org.mizux.javanative-\u003cplatform\u003e/target/`) should have this layout:\n```\n{...}/target/javanative-\u003cplatform\u003e-1.0.jar:\n\\- \u003cplatform\u003e\n   \\-libFoo.so.1.0\n   \\-libjnijavanative.so\n...\n```\nnote: `\u003cplatform\u003e` could be `linux-x86-64`, `darwin-x86-64` or `win32-x86-64`.\n\ntips: since maven package are just zip archive you can use `unzip -l \u003cpackage\u003e.jar`\nto study their layout.\n\n#### Building local Package\n\nSo now, let's create the local `org.mizux.javanative:javanative-java.jar` maven\npackage which will depend on our previous native package.\n\nHere some dev-note concerning this `POM.xml`.\n* Add runtime dependency on each native package(s) availabe:\n  ```xml\n  \u003cdependency\u003e\n    \u003cgroupId\u003eorg.mizux.javanative\u003c/groupId\u003e\n    \u003cartifactId\u003ejavanative-linux-x86-64\u003c/artifactId\u003e\n    \u003cversion\u003e[1.0,)\u003c/version\u003e\n    \u003ctype\u003ejar\u003c/type\u003e\n    \u003cscope\u003eruntime\u003c/scope\u003e\n  \u003c/dependency\u003e\n  ```\n  - Add dependency to jna so we can find at runtime the current `\u003cplatform\u003e`:\n  ```xml\n  \u003cdependency\u003e\n    \u003cgroupId\u003enet.java.dev.jna\u003c/groupId\u003e\n    \u003cartifactId\u003ejna-platform\u003c/artifactId\u003e\n    \u003cversion\u003e5.13.0\u003c/version\u003e\n  \u003c/dependency\u003e\n  ```\n\nThen you can generate the package using:\n```bash\nmvn package\nmvn install\n```\n\nIf everything good the package (located in\n`\u003cbuildir\u003e/java/org.mizux.javanative/target/`) should have this layout:\n```\n{...}/target/javanative-java-1.0.jar:\n\\- org/\n   \\- mizux/\n      \\- javanative/\n         \\- Loader$PathConsumer.class\n         \\- Loader$1.class\n         \\- Loader.class\n         \\- foo/\n            \\- GlobalsJNI.class\n            \\- StringJaggedArray.class\n            \\- IntPair.class\n            \\- StringVector.class\n            \\- Foo.class\n            \\- PairVector.class\n            \\- PairJaggedArray.class\n            \\- Globals.class\n...\n```\n\n#### Testing local Package\n\nWe can test everything is working by using the `org.mizux.javanative.test:javanative-test` project.\n\nFirst you can build it using:\n```\ncmake --build build\n```\nnote: `javanative-test` depends on `javanative-java` which is locally installed in the local maven cache\n(`~/.m2/repository/org/mizux/javanative/...`).\n\nThen you can run it using:\n```sh\ncmake --build build --target test\n```\nor manually using:\n```\ncd \u003cbuilddir\u003e/java/org.mizux.javanative.test\nmvn exec:java -Dexec.mainClass=\"org.mizux.javanative.Test\"\n```\n\n## Appendices\nFew links on the subject...\n\n### Resources\nProject layout:\n* The Pitchfork Layout Revision 1 (cxx-pflR1)\n\nCMake:\n* https://llvm.org/docs/CMakePrimer.html\n* https://cliutils.gitlab.io/modern-cmake/\n* https://cgold.readthedocs.io/en/latest/\n\nJava:\n* [POM.xml reference](http://maven.apache.org/pom.html)\n* [Maven Central POM requirement](https://central.sonatype.org/pages/requirements.html)\n* [Javadoc Plugin](https://maven.apache.org/plugins/maven-javadoc-plugin/)\n* [Java Source Plugin](https://maven.apache.org/plugins/maven-source-plugin/)\n* [Java Native Access Project](https://github.com/java-native-access/jna)\n\n### Misc\nImage has been generated using [plantuml](http://plantuml.com/):\n```bash\nplantuml -Tsvg docs/{file}.dot\n```\nSo you can find the dot source files in [docs](docs).\n\n## License\nApache 2. See the LICENSE file for details.\n\n## Disclaimer\nThis is not an official Google product, it is just code that happens to be\nowned by Google.\n\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmizux%2Fjava-native","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmizux%2Fjava-native","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmizux%2Fjava-native/lists"}