Add JNI Library Compilation with Maven or ant tasks

Asked

Viewed 189 times

6

I am creating a Java project with native methods (JNI) without having to create three separate projects for it. My goal is to compile everything I need all at once.

I use only one DLL/OS in this project generated by a single C file.

How to add this build step and move the library to the dist folder in pom do maven or in the ant tasks?

My indifference to Maven or ant tasks is that I intend to use netbeans to compile this, so whatever is easier to adapt is what I’m looking for.

2 answers

5


Maven owns the Native Maven Plugin and the NAR Plugin for Maven. Both have Goals to work with C/C++ compilers, generate headers with javah, etc..

If you need a "do it yourself", that article (a little old) has an interesting cake recipe. Classes are compiled by calling the command make through the Antrun and it shows you how to properly load and package native libraries in Maven (contemplating the scenario you want to do build for multiple platforms).

2

I use the Nar-Maven-plugin to work with JNI, C and java in a single Maven project. This plugin was for a while without maintenance but now there is a group that resumed the development of the project.

This plugin automatically creates JNI headers from Java methods with the reserved word Native, before the compilation of Java code. It also automatically creates a Java class to load the generated DLL by adjusting the DLL name according to the version (following the Maven default). The plugin generates two artifacts for a JNI project: the JAR file and the DLL; so it is easier to keep married the C and Java code.

View these two articles, in Portuguese:

1 - JNI: Java and C++ In a Single Maven Project

2 - C++ and Maven: Nar-Maven-plugin

I think the first article is exactly what you’re looking for. Both have downloadable source code.

  • Welcome to Stack Overflow. Try to better contextualize your answer by inserting relevant parts of the links that can solve the problem. The way it is, if external sites leave or give or change the link, your response will become useless.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.