Heaton Research

JNA with Maven Quickstart

Java Native Access (JNA) and Java Native Interface (JNI) are two frameworks that allow Java to directly call C-style functions located in executable libraries, such as Windows Dynamic Link Libraries (DLLs). Very often the Java code seeks to directly access functions inside of the WIN32 API. This posting describes how I setup a minimal “Hello World” application for JNA that makes use of Maven to obtain the appropriate JAR files for JNA.

Though JNA and JNI both accomplish similar goals, there are some important differences. JNI has much better performance than JNA. However, JNA is much easier to work with than JNI. The focus of this posting is to show how to setup a simple “Hello World” JNA application that uses Maven to incorporate the necessary JARs needed for JNA support. JNA is not built into Java. Because of this a number of JAR files must be included in the build.

The easiest way to ensure that the JNA JARs are available is to make use of Maven. For the simple example that I provide here, I use the following Maven script:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>org.jeffheaton</groupId>
<artifactId>jna-example</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>gare-win32</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency> <!-- JNA dependency -->
<groupId>net.java.dev.jna</groupId>
<artifactId>jna</artifactId>
<version>4.5.2</version>
</dependency>
<dependency> <!-- JNA dependency -->
<groupId>net.java.dev.jna</groupId>
<artifactId>jna-platform</artifactId>
<version>4.5.2</version>
</dependency>
</dependencies>
<properties>
<maven.compiler.source>1.6</maven.compiler.source>
<maven.compiler.target>1.6</maven.compiler.target>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>attached</goal>
</goals>
<phase>package</phase>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass>org.jeffheaton.jnaexample.JNAExample</mainClass>
</manifest>
</archive>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>

The above Maven file is pretty standard. It could easily be converted to a Gradle file as well. However, for very simple build scripts I prefer Maven for its simplicity. There are two main concepts going on:

  • Two JNA dependences - The dependancies section ensures that the two JNA jars are available to the program.
  • Fat Jar - The main class is specified and a JAR file is built with all dependancies included. This provides a single JAR file that contains everything needed to run this project.

The actual Java program is based on several JNA examples that simply dumps a list of all top level windows using WIN32. The source code for this example is provided here:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package org.jeffheaton.jnaexample;

import com.sun.jna.Native;
import com.sun.jna.Pointer;
import com.sun.jna.platform.win32.WinDef.HWND;
import com.sun.jna.platform.win32.WinUser;
import com.sun.jna.platform.win32.WinUser.WNDENUMPROC;
import com.sun.jna.win32.StdCallLibrary;

public class JNAExample {
public interface User32 extends StdCallLibrary {
User32 INSTANCE = (User32) Native.loadLibrary("user32", User32.class);
boolean EnumWindows(WinUser.WNDENUMPROC lpEnumFunc, Pointer arg);
int GetWindowTextA(HWND hWnd, byte[] lpString, int nMaxCount);
}

public static void main(String[] args) {
final User32 user32 = User32.INSTANCE;
user32.EnumWindows(new WNDENUMPROC() {
int count = 0;
@Override
public boolean callback(HWND hWnd, Pointer arg1) {
byte[] windowText = new byte[1024];
user32.GetWindowTextA(hWnd, windowText, windowText.length);
String wText = Native.toString(windowText);

if (wText.isEmpty()) {
return true;
}

System.out.println("Window, hwnd:" + hWnd + ", total " + ++count
+ " Title: " + wText);
return true;
}
}, null);
}
}

To build the FatJAR use the following command:

1
mvn package

To run the program, use the following command:

1
java -jar jna-example-1.0-SNAPSHOT-jar-with-dependencies.jar 

The complete source code to this project can be found at my GitHub repository. This should produce output similar to the following:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
Window, hwnd:native@0x1014a, total 1 Title: G
Window, hwnd:native@0x1021c, total 2 Title: Battery Meter
Window, hwnd:native@0x10280, total 3 Title: Network Flyout
Window, hwnd:native@0x10500, total 4 Title: HardwareMonitorWindow
Window, hwnd:native@0x4037a, total 5 Title: HardwareMonitorWindow
Window, hwnd:native@0x605cc, total 6 Title: Command Prompt - java -jar jna-example-1.0-SNAPSHOT-jar-with-dependencies.jar
Window, hwnd:native@0x50262, total 7 Title: jeffheaton/jna-example: A simple JNA example with Maven build script - Google Chrome
Window, hwnd:native@0x60714, total 8 Title: projects
Window, hwnd:native@0xb0600, total 9 Title: C:\Users\jheaton\projects\jna-example\src\test\java\org\jeffheaton\jna-example\AppTest.java - Notepad++
Window, hwnd:native@0x305f2, total 10 Title: rgare
Window, hwnd:native@0x107ce, total 11 Title: Progress
Window, hwnd:native@0x107c8, total 12 Title: G
Window, hwnd:native@0x3066c, total 13 Title: OutlookFbThreadWnd
Window, hwnd:native@0x50540, total 14 Title: Number of unread items in this folder
Window, hwnd:native@0x1042a, total 15 Title: Inbox - JHeaton@rgare.com - Outlook
Window, hwnd:native@0x20576, total 16 Title: Word
Window, hwnd:native@0x50534, total 17 Title: WMS ST Notif Window 0000247C 00002A7C
Window, hwnd:native@0x204e0, total 18 Title: Outlook Send/Receive Progress
Window, hwnd:native@0x204f6, total 19 Title: DDE Server Window
Window, hwnd:native@0x700d8, total 20 Title: WISPTIS
Window, hwnd:native@0x80510, total 21 Title: WMS ST Notif Window 00002120 000020BC
Window, hwnd:native@0x4026a, total 22 Title: G
Window, hwnd:native@0x40048, total 23 Title: OfficePowerManagerWindow
Window, hwnd:native@0x104ee, total 24 Title: V
Window, hwnd:native@0x20468, total 25 Title: T
Window, hwnd:native@0x104ec, total 26 Title: Microsoft Outlook Social Connector
Window, hwnd:native@0x3031a, total 27 Title: AdxTaskPane
Window, hwnd:native@0x104da, total 28 Title: FolderViewServiceForm
Window, hwnd:native@0x104d8, total 29 Title: AdxTaskPane
Window, hwnd:native@0x104d4, total 30 Title: ReadingPaneServiceForm
Window, hwnd:native@0x30224, total 31 Title: Cisco ViewMail for Outlook Add-In_{62e65727-15dd-444f-b1a9-ea58b91c6184}
Window, hwnd:native@0x30236, total 32 Title: .NET-BroadcastEventWindow.2.0.0.0.33c0d9d.0
Window, hwnd:native@0x10492, total 33 Title: crash service
Window, hwnd:native@0x2048a, total 34 Title: Phish Alert_{5cb25b73-0cb5-4b46-8d77-f504402449b5}
Window, hwnd:native@0x1048c, total 35 Title: .NET-BroadcastEventWindow.4.0.0.0.1ca0192.0
Window, hwnd:native@0x203b4, total 36 Title: GpgOLResponder
Window, hwnd:native@0x1049a, total 37 Title: Slack - RGA Enterprise Services Company
Window, hwnd:native@0x20436, total 38 Title: WMS ST Notif Window 0000247C 00002480
Window, hwnd:native@0x10426, total 39 Title: W
Window, hwnd:native@0x203ca, total 40 Title: OfficePowerManagerWindow
Window, hwnd:native@0x6027c, total 41 Title: G
Window, hwnd:native@0x203fe, total 42 Title: Microsoft Outlook
Window, hwnd:native@0x203da, total 43 Title: .NET-BroadcastEventWindow.4.0.0.0.3e799b.0
Window, hwnd:native@0x103d6, total 44 Title: G
Window, hwnd:native@0x103aa, total 45 Title: .NET-BroadcastEventWindow.4.0.0.0.2cf3e95.0
Window, hwnd:native@0x103a8, total 46 Title: MediaContextNotificationWindow
Window, hwnd:native@0x103a4, total 47 Title: SystemResourceNotifyWindow
Window, hwnd:native@0x102fc, total 48 Title: Skype for Business
Window, hwnd:native@0x1039e, total 49 Title: PPP Client Console
Window, hwnd:native@0x60370, total 50 Title: Printer Installer Client Interface
Window, hwnd:native@0x60320, total 51 Title: Rtc Video PnP Listener
Window, hwnd:native@0x70322, total 52 Title: CUccMediaDeviceManager
Window, hwnd:native@0x10304, total 53 Title: W
Window, hwnd:native@0x202f8, total 54 Title: OfficePowerManagerWindow
Window, hwnd:native@0x3023e, total 55 Title: G
Window, hwnd:native@0x30250, total 56 Title: LogCheckerHiddenRootWindow_ETL
Window, hwnd:native@0x30258, total 57 Title: LogCheckerLogRolloverHandlerHiddenWindow
Window, hwnd:native@0x102ea, total 58 Title: C:\Program Files\Box\Box Sync\BoxSyncMonitor.exe
Window, hwnd:native@0x301be, total 59 Title: Box Sync
Window, hwnd:native@0x702ae, total 60 Title: G
Window, hwnd:native@0x602c2, total 61 Title: .NET-BroadcastEventWindow.4.0.0.0.f96fc5.0
Window, hwnd:native@0x102b0, total 62 Title: Realtek HD Audio Background Process for Vista
Window, hwnd:native@0x102aa, total 63 Title: G
Window, hwnd:native@0x402a6, total 64 Title: Realtek HD Audio New CPL background window
Window, hwnd:native@0x1029e, total 65 Title: CVenderSpecSetting
Window, hwnd:native@0x201f0, total 66 Title: ViennaSettingUI
Window, hwnd:native@0x10294, total 67 Title: Realtek HD Audio New GUI
Window, hwnd:native@0x10292, total 68 Title: Animate Manager
Window, hwnd:native@0x10290, total 69 Title: RTK TRAYICON
Window, hwnd:native@0x1028e, total 70 Title: AudioEngine For Realtek HD Audio New GUI
Window, hwnd:native@0x30260, total 71 Title: RTK AUDIO DRIVER EVENT MGR
Window, hwnd:native@0x5025a, total 72 Title: MS_WebcheckMonitor
Window, hwnd:native@0x10212, total 73 Title: DDE Server Window
Window, hwnd:native@0x101c8, total 74 Title: HotKey Listener
Window, hwnd:native@0x10170, total 75 Title: DDE Server Window
Window, hwnd:native@0x10122, total 76 Title: EXPLORER
Window, hwnd:native@0x20032, total 77 Title: .NET-BroadcastEventWindow.4.0.0.0.1ca0192.0
Window, hwnd:native@0x5006c, total 78 Title: BluetoothNotificationAreaIconWindowClass
Window, hwnd:native@0x1008c, total 79 Title: Windows Push Notifications Platform
Window, hwnd:native@0x2001a, total 80 Title: Task Host Window
Window, hwnd:native@0x10040, total 81 Title: DWM Notification Window
Window, hwnd:native@0x101ba, total 82 Title: igfxtrayWindow
Window, hwnd:native@0x104d2, total 83 Title: ADXOlReceiverWindowCaption
Window, hwnd:native@0x10142, total 84 Title: Program Manager
Window, hwnd:native@0x1014e, total 85 Title: Default IME
Window, hwnd:native@0x101ac, total 86 Title: Default IME
Window, hwnd:native@0x10136, total 87 Title: MSCTFIME UI
Window, hwnd:native@0x100c4, total 88 Title: Default IME
Window, hwnd:native@0x10232, total 89 Title: Default IME
Window, hwnd:native@0x203c2, total 90 Title: MSCTFIME UI
Window, hwnd:native@0x103dc, total 91 Title: Default IME
Window, hwnd:native@0x106ea, total 92 Title: MSCTFIME UI
Window, hwnd:native@0x106cc, total 93 Title: Default IME
Window, hwnd:native@0x806fe, total 94 Title: MSCTFIME UI
Window, hwnd:native@0x6053e, total 95 Title: Default IME
Window, hwnd:native@0x2072c, total 96 Title: MSCTFIME UI
Window, hwnd:native@0x706fc, total 97 Title: Default IME
Window, hwnd:native@0x10630, total 98 Title: MSCTFIME UI
Window, hwnd:native@0x405c6, total 99 Title: Default IME
Window, hwnd:native@0x30206, total 100 Title: MSCTFIME UI
Window, hwnd:native@0x205fa, total 101 Title: Default IME
Window, hwnd:native@0x407b8, total 102 Title: MSCTFIME UI
Window, hwnd:native@0x107d0, total 103 Title: Default IME
Window, hwnd:native@0x107cc, total 104 Title: Default IME
Window, hwnd:native@0xa06f8, total 105 Title: Default IME
Window, hwnd:native@0x606d6, total 106 Title: Default IME
Window, hwnd:native@0x80608, total 107 Title: Default IME
Window, hwnd:native@0x403f8, total 108 Title: MSCTFIME UI
Window, hwnd:native@0x10164, total 109 Title: Default IME
Window, hwnd:native@0x30564, total 110 Title: Default IME
Window, hwnd:native@0x1044a, total 111 Title: MSCTFIME UI
Window, hwnd:native@0x2041a, total 112 Title: Default IME
Window, hwnd:native@0x1053a, total 113 Title: Default IME
Window, hwnd:native@0x2053c, total 114 Title: Default IME
Window, hwnd:native@0x80062, total 115 Title: Default IME
Window, hwnd:native@0x104aa, total 116 Title: Default IME
Window, hwnd:native@0x10494, total 117 Title: Default IME
Window, hwnd:native@0x1047c, total 118 Title: Default IME
Window, hwnd:native@0x20130, total 119 Title: Default IME
Window, hwnd:native@0x10480, total 120 Title: Default IME
Window, hwnd:native@0x303d8, total 121 Title: Default IME
Window, hwnd:native@0x103a6, total 122 Title: Default IME
Window, hwnd:native@0x2034c, total 123 Title: MSCTFIME UI
Window, hwnd:native@0x202e6, total 124 Title: Default IME
Window, hwnd:native@0x10388, total 125 Title: Default IME
Window, hwnd:native@0x102ec, total 126 Title: Default IME
Window, hwnd:native@0x102e0, total 127 Title: MSCTFIME UI
Window, hwnd:native@0x50068, total 128 Title: Default IME
Window, hwnd:native@0xc02c4, total 129 Title: Default IME
Window, hwnd:native@0x102b2, total 130 Title: Default IME
Window, hwnd:native@0x102ac, total 131 Title: Default IME
Window, hwnd:native@0x30256, total 132 Title: Default IME
Window, hwnd:native@0x1024a, total 133 Title: Default IME
Window, hwnd:native@0x10216, total 134 Title: Default IME
Window, hwnd:native@0x101de, total 135 Title: MSCTFIME UI
Window, hwnd:native@0x101ca, total 136 Title: Default IME
Window, hwnd:native@0x1013e, total 137 Title: Default IME
Window, hwnd:native@0x10124, total 138 Title: Default IME
Window, hwnd:native@0x200e4, total 139 Title: Default IME
Window, hwnd:native@0x10096, total 140 Title: Default IME
Window, hwnd:native@0x20092, total 141 Title: Default IME
Window, hwnd:native@0x1008e, total 142 Title: Default IME
Window, hwnd:native@0x1015c, total 143 Title: MSCTFIME UI
Window, hwnd:native@0x100aa, total 144 Title: Default IME