Skip to main content

Easy Start with JNA

JNA (Java Native Access) is the good and easy answer to the JNI(Java Native Interface). Those who find it cumbersome to to call C, C++ DLL functions using JNI, then JNA is for you. The project is hosted at https://jna.dev.java.net/ .

Here is my first encounter(experiment) with the JNA.

Problem: I want to call one DLL function in Java program

Solution: The first thing that comes into my mind is JNI, coz I heard a lot about it whenever something "native" has to be called in Java. Just while surfing for the more information on basic JNI programming, I came across the JNA project/api and find it interesting to give it try.

Here are the basic two things you need to have in order to use JNA
1) JNA api JAR file (jna.jar):
Download it from here

2) DLL which you want to call in your Java program
Now this may be the big thing for a guy who haven't worked on VC++, DLL, MFC etc things. So here is the quick link about how to create the DLL
http://www.icynorth.com/development/createdlltutorial.html

This is the best about JNA, you don't need to write header file as that we need to do in JNI

JAVA Code

import com.sun.jna.*;
import com.sun.jna.ptr.*;
/** Simple example of JNA interface mapping and usage. */
public class HelloWorld {
// This is the standard, stable way of mapping, which supports extensive
// customization and mapping of Java to native types.
public interface TestLibrary extends Library {
TestLibrary INSTANCE = (TestLibrary)
Native.loadLibrary("TestDLL", TestLibrary.class);
// Call C/c++ function
void helloWorld(int n);
}

public static void main(String[] args) {
int n = TestLibrary.INSTANCE.helloWorld(1);
}
}

C/C++ Code

extern "C" __declspec(dllexport) int helloWorld (int divider)
{
return 77/divider;
}

Comments

Popular posts from this blog

Installing i3lock-color on Ubuntu

i3lock is fancy lock screen for Linux. There is i3lock dep available on Ubuntu but its not what I was looking for; I was more interested in i3lock-color . Lets see how we can install the same on ubuntu. PS: I'm using Ubuntu 16.04 Get source code for i3lock-color $ git clone https://github.com/PandorasFox/i3lock-color.git  $ cd i3lock-color Install required packages to build i3lock  $ apt install libev-dev $ apt install libxcb-composite0 libxcb-composite0-dev libxcb-xinerama0 libxcb-randr0  $ apt install libxcb-xinerama0-dev  $ apt install libxcb-xkb-dev  $ apt install libxcb-image0-dev  $ apt install libxcb-util-dev $ apt install libxkbcommon-x11-dev $ apt install libjpeg-turbo8-dev  $ apt install libpam0g-dev Build  $ autoreconf -i && ./configure && make Execute $ ./lock.sh Assign Shortcut In order to assign shortcut, install compizconfig $ sudo apt-get install compizconfig-settings-manager compiz-plugins-extra and then go

INTO OUTFILE query, How to modify & output null values ?

I tried one query (originally asked at JR) on MySQL which export the table data into a file, with a comma delimited field. The query is straight forward and replace the null column value by "\N" character, but when I want to replace that "\N" value by some other character like "0" or simple empty field like " ", its gets little tricky. Here is the base query SELECT Id, name, age FROM student INTO OUTFILE 'c:/result.txt' FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"' LINES TERMINATED BY '\n'; And it outputs like, 1, "sagar", 23 2, "anant", 24 Now suppose some 'name' fields are null, then it outputs like 1, "sagar", 23 2, \N, 24 Now to obtain my desired results, which replace this null (\N) values by empty string like, "", I tried out two solutions --1. Using CASE statement SELECT Id, CASE WHEN name IS NULL THEN '' ELSE name END AS NewName, age FROM s

GlassFish V3 admin console taking too much time to load.

If you have installed Glassfish V3 and trying to load admin console, but after signing in, is it taking too much time to get to the main page ? Do you have server.log entry like this: admin console: initSessionAttributes() Cannot refresh Catalog : Connection timed out then its time to tweak some files. Here its how: 1. Update the %GLASSFISH_HOME/glassfish/domains/domain1/domain.xml -Dcom.sun.enterprise.tools.admingui.NO_NETWORK=true This will block up the News item, the registration item, etc 2. Remove update tool jar (Backup and remove this JAR) %GLASSFISH_HOME/glassfish/modules/console-updatecenter-plugin.jar Delete this dir: %GLASSFISH_HOME/glassfish/domains/domain1/osgi-cache %GLASSFISH_HOME/glassfish/domains/domain1/generated Now start the server (bin/asadmin start-domain) and you will see the admin console won't be hang up and take you directly to main page.