I want to build Java application which uses JNI and code in C. Moreover I want make my app cross-platform by compiling native code and packaging native platform-dependent libraries within JAR file. I was following this guide
but it is not working. I have no examples with maven-native-plugin nor NAR plugins. How is it possible to make java native methods cross-platform?
I would suggest to check the nar-maven-plugin for example.
Related
I'm trying to develop an app that uses Bluetooth technology, so I need javax.bluetooth & javax.obex & javax.microedition packages. I've downloaded it but I don't know how to properly place it and where so the Java can see it while compilation. How do I integrate it?
It depends on what tool you're using to create your jar. For example: Maven, Eclipse or IntelliJ. Usually you can find an answer that is relevant to your build method by simply googling it.
However, if you want to permenantly add them to your JDK, you'll have to paste them in your JDK's integrated jre into the following folder: \jre\lib\ext
For me that would be:
C:\Program Files\Java\jdk1.8.0_231\jre\lib\ext
As you can see, I added RXTX (a serial comm library) to mine and I can now use it in my code.
I'm trying to work out if there is a definitive, recommended, robust way to work with native libraries in Java and Maven.
I know it's possible to do this by putting libraries in the right places, configuring java.library.path and calling System.loadLibrary etc. - but I want a proper solution that works across the whole development / deployment life-cycle and doesn't involve lots of hacks.
My requirements seem fairly straightforward:
Configuration 100% via Maven
Ability to include native dependencies for different platforms / architectures (mainly Linux and Windows, others would be nice though...)
Ability to create a runnable .jar that doesn't require the user to fiddle with command line options to run it (i.e. it will unpack and load any native dependencies it needs at runtime, preferably as temp files or something like that)
The code works during development (in Eclipse)
The code works during normal Maven builds at the command line
Basically, I want natives to just work both during development and for the user at runtime.
Is this possible? If so what do I need to configure to make it happen?
It certainly is possible. A good example that I came across which solves the same problem is sqlite wrapper at https://bitbucket.org/xerial/sqlite-jdbc/overview
By following a pre defined packaging structure for each architecture, we can programmatically extract the desired native library from class path into a temp folder and can set the desired java.library.path to load the native library.
All native libraries would be packaged in the src/main/resources folder
At runtime there needs to be a bootstrap code which can determine the OS, architecture and choose the right native library to extract
the same bootstrap code can extract the native library into a temp folder and call System.loadLibrary
I have not personally tried executing the above flow but could not think of any other way.
I have generated a DLL froma JAR file using IKVM doing:
ikvmc -target:library mylib.jar
I would like to use the DLL in a Visual C++ project. What are the steps required to call dll functions from my own code? How do I know which methods are exported by the DLL and how do I call them without a header file?
The IKVM.NET is written for the .NET framework 2.0. The usage inside a Visual Studio C++ project can be difficult. But it should be possible to access it via COM interface. Take a look at this answer.
I am just starting to learn JNI. I have been following a simple example, and I have created a Java app that calls a Hello World method in a native library. I'd like to target Win32 and Linux x86.
My library resides in a DLL, and I can call it just fine using LoadLibrary when the DLL is added to the root of my Eclipse project.
However, I can't figure out how to get Eclipse to export a runnable JAR that includes the DLL and the .SO file for Linux.
So my question is basically; how would you go about creating a project in Eclipse and include several versions of the same native library?
Thank you,
Martin
For runnable JARs, what you need to do is extract to the temporary directory (maybe in a static { } block) and then load the DLL from that directory (using System.loadLibrary() in the same block). To do this, you need to subclass ClassLoader and override the findLibrary() method to allow libraries to be found in that directory. You can do whatever logic you need to here to load the particular platform libraries. To be honest, the naming for the libraries on the different platforms should be similar anyway -- I believe that you omit the 'lib' part when loading, and the extension. That's the gist of it. Probably easier to use One-JAR as the other poster mentioned :)
You might want to check out the One-JAR project. It lets you package your application and its dependencies (including native libraries) to a single jar file.
Am trying to execute a complete Jython project using java eclipse 3.4.. I have configured Jython environment in my eclipse..I went through certain tutorials which described about Building a factory.. But i guess that method can be used to execute applications that are specific to a particular Jython module.. What I exactly need is::
To run the entire Jython project as a java application, by using the .class file created in the Jython project..
Not sure if this is feasible.. Suggestions please..
One Java class with an embedded PythonInterpreter is capable of kicking off an entire Jython application. May need to play around with setting the appropriate python.home and library paths, look in the PyServletInitializer and PyFilter for examples of how this is done.
Another option is use PyDev and just run a Jython script to start the application, bypassing the need for a Java application all together.