I want to use JNA to detect foreground application on Linux (Ubuntu 14). I followed this link
Find out what application (window) is in focus in Java
but I got the following error:
Exception in thread "main" java.lang.UnsatisfiedLinkError: Unable to load library 'XLib': Native library (linux-x86-64/libXLib.so) not found in resource path ([file:/home/zzhou/workspace/home_prioritization_plus/bin/, file:/home/zzhou/Downloads/jna-4.1.0.jar, file:/home/zzhou/Downloads/jna-platform-4.1.0.jar])
at com.sun.jna.NativeLibrary.loadLibrary(NativeLibrary.java:271)
at com.sun.jna.NativeLibrary.getInstance(NativeLibrary.java:398)
at com.sun.jna.Library$Handler.<init>(Library.java:147)
at com.sun.jna.Native.loadLibrary(Native.java:412)
at com.sun.jna.Native.loadLibrary(Native.java:391)
at FunctionalityTest$XLib.<clinit>(FunctionalityTest.java:15)
at FunctionalityTest.main(FunctionalityTest.java:23)
The code is:
import com.sun.jna.Native;
import com.sun.jna.Platform;
import com.sun.jna.Pointer;
import com.sun.jna.platform.unix.X11;
import com.sun.jna.win32.StdCallLibrary;
public class FunctionalityTest {
static class Psapi {
static { Native.register("psapi"); }
public static native int GetModuleBaseNameW(Pointer hProcess, Pointer hmodule, char[] lpBaseName, int size);
}
public interface XLib extends StdCallLibrary {
XLib INSTANCE = (XLib) Native.loadLibrary("XLib", Psapi.class); // <-- PROBLEM
int XGetInputFocus(X11.Display display, X11.Window focus_return, Pointer revert_to_return);
}
public static void main(String args[]) {
if(Platform.isLinux()) { // Possibly most of the Unix systems will work here too, e.g. FreeBSD
final X11 x11 = X11.INSTANCE;
final XLib xlib= XLib.INSTANCE;
X11.Display display = x11.XOpenDisplay(null);
X11.Window window=new X11.Window();
xlib.XGetInputFocus(display, window,Pointer.NULL);
X11.XTextProperty name=new X11.XTextProperty();
x11.XGetWMName(display, window, name);
System.out.println(name.toString());
}
}
}
To import JNA library, I downloaded jna and jna-platform from https://github.com/twall/jna and use Configure Build Path... in Eclipse to add library. I did not install anything. May that be the source of the problem?
Thanks for your help.
Afaik, even for JNA, you have to load the library in Java in order for JNA to find it. (tested for win32, not linux)
Try this just above Native.loadLibrary:
System.loadLibrary("XLib");
Related
I tried libvirt java bindings and always get Access Violation error when the library try to use Native.free method to free memory allocated in the dll, so I write the following simple test.
public class HelloWorld {
public interface CLibrary extends Library {
CLibrary INSTANCE = (CLibrary) Native.loadLibrary("msvcrt", CLibrary.class);
Pointer _strdup(String src);
}
public static void main(String[] args) {
Pointer p = CLibrary.INSTANCE._strdup("HelloWorld");
Native.free(Pointer.nativeValue(p));
}
}
My environment is as following:
Windows 7 Ultimate SP1 64Bit, Jdk&Jvm is also 64bit(1.8.0_101), Eclipse Neon Release 64Bit, JNA version 4.2.2.
The program stop at Native.free line, and prompt App crash dialog, information is as following:
Problem signature:
Problem Event Name: APPCRASH
Application name: javaw.exe
Application version: 8.0.1010.13
Application timestamp: 576a4c7e
Fault module name: StackHash_c84f
Faulty module version: 6.1.7601.18247
Faulty module timestamp: 521eaf24
Exception code: c0000374
Exception offset: 00000000000c4102
OS version: 6.1.7601.2.1.0.256.1
Locale ID: 2052
Additional Information 1: c84f
Additional information 2: c84f3cec06e628f5bb4621d27c86f80d
Additional Information 3: 3b2e
Additional Information 4: 3b2e22e89759af30ea1b1d716fbf08f3
So I wondering if this way to free memory created in dll is wrong? Can you give me some advice, thanks!
import com.sun.jna.Library;
import com.sun.jna.Native;
import com.sun.jna.Pointer;
public class MemoryTest {
public interface CLibrary extends Library {
CLibrary INSTANCE = (CLibrary) Native.loadLibrary("c", CLibrary.class);
Pointer strdup(String src);
}
public static void main(String[] args) {
Pointer p = CLibrary.INSTANCE.strdup("HelloWorld");
Native.free(Pointer.nativeValue(p));
System.gc();
}
}
this test did run on Linux Ubuntu64 16.04 without any crash.
I need to call Windows Color System functions from Java. Following this tutorial i tried to call DLL function using Java Native Access. All examples from this tutorial works fine. When i try to load and use Mscms.dll (one of the WCS libraries) that DLL seems to be loaded successfully, but i can not call any functions. List of functions is here.
I got a message:
Exception in thread "main" java.lang.UnsatisfiedLinkError: Error looking up function 'GetColorDirectory'
What's wrong with my code? Could you help me please?
import com.sun.jna.Library;
import com.sun.jna.Native;
public class WCS_test {
public interface Mscms extends Library {
// http://msdn.microsoft.com/en-us/library/dd316928%28v=vs.85%29.aspx
boolean GetColorDirectory(String pMachineName, String[] pBuffer, int pdwSize);
}
private static Mscms mscms = (Mscms) Native.loadLibrary("C:/Windows/system32/Mscms.dll", Mscms.class);
public static void main(String[] args) {
if (mscms != null)
System.out.println("Library loaded\n");
else
System.err.println("Library loading error\n");
String[] pBuffer = new String[1024];
mscms.GetColorDirectory(null, pBuffer, pBuffer.length);
}
}
When you get a java.lang.UnsatisfiedLinkError that means that it could not find the function 'GetColorDirectory' inside of the Mscms.dll. Looking at the link from your source code http://msdn.microsoft.com/en-us/library/dd316928%28v=vs.85%29.aspx you should try the Unicode name GetColorDirectoryW.
I am just learning how to use Java JNA, and I am trying to call a simple function from the Microsoft Kinect SDK. (NuiGetSensorCount) which just returns the number of connected kinects.
Here is my attempt:
import com.sun.jna.Library;
import com.sun.jna.Native;
import com.sun.jna.NativeLong;
import com.sun.jna.Pointer;
public class Driver {
public interface KinectLibrary extends Library {
KinectLibrary INSTANCE = (KinectLibrary)Native.loadLibrary(("Microsoft.Kinect"),KinectLibrary.class);
//_Check_return_ HRESULT NUIAPI NuiGetSensorCount( _In_ int * pCount );
NativeLong NuiGetSensorCount(Pointer pCount);
}
public static void main(String[] args) {
Pointer devCount = new Pointer(0);
KinectLibrary.INSTANCE.NuiGetSensorCount(devCount);
System.out.println("Devices:"+devCount.getInt(0));
}
}
But I get:
Exception in thread "main" java.lang.UnsatisfiedLinkError: Error looking up function 'NuiGetSensorCount': The specified procedure could not be found.
at com.sun.jna.Function.<init>(Function.java:208)
at com.sun.jna.NativeLibrary.getFunction(NativeLibrary.java:536)
at com.sun.jna.NativeLibrary.getFunction(NativeLibrary.java:513)
at com.sun.jna.NativeLibrary.getFunction(NativeLibrary.java:499)
at com.sun.jna.Library$Handler.invoke(Library.java:199)
at $Proxy0.NuiGetSensorCount(Unknown Source)
at Driver.main(Driver.java:30)
Can anybody provide help of how to change my code so it finds the correct native function? And also provide some information/reference so that I could try to debug this myself (some way to see what function Java JNA is looking for, and compare it to what the .dll contains)
I figured out my problem. First, I used a program called dependency walker http://dependencywalker.com/ to view all the symbols in the DLL and I determined that the DLL I was using (Microsoft.Kinect.dll) did not actually contain the function I was trying to call. I found out that Kinect10.dll was the one I needed. After changing that, I had to make a small change to my pointer and it works perfectly!
Here is the fixed code.
import com.sun.jna.Native;
import com.sun.jna.NativeLong;
import com.sun.jna.ptr.IntByReference;
import com.sun.jna.win32.StdCallLibrary;
public class Driver{
public interface KinectLibrary extends StdCallLibrary {
KinectLibrary INSTANCE = (KinectLibrary)Native.loadLibrary(("Kinect10"),KinectLibrary.class);
//_Check_return_ HRESULT NUIAPI NuiGetSensorCount( _In_ int * pCount );
NativeLong NuiGetSensorCount(IntByReference pCount);
}
public static void main(String[] args) {
IntByReference a = new IntByReference();
KinectLibrary.INSTANCE.NuiGetSensorCount(a);
System.out.println("Devices:"+a.getValue());
}
}
I am using dll in java using JNA, but i am getting below error
Exception in thread "main" java.lang.UnsatisfiedLinkError: Error looking up function 'GetStatus': The specified procedure could not be found.
Not getting how to resolve this issue?
Please help.
Here is java code
import com.sun.jna.Library;
import com.sun.jna.Native;
/** Simple example of native library declaration and usage. */
public class First {
public interface TEST extends Library {
public String GetStatus();
}
public static void main(String[] args) {
TEST obj = (TEST ) Native.loadLibrary("TEST ", TEST .class);
System.out.println( obj.GetStatus());
}
}
This Nugget is super easy to use and works perfectly. https://www.nuget.org/packages/UnmanagedExports
You need Visual Studio 2012 (express).
Once installed, just add [RGiesecke.DllExport.DllExport] before any static function you want to export. That's it!
Example:
C#
[RGiesecke.DllExport.DllExport]
public static int YourFunction(string data)
{
/*Your code here*/
return 1;
}
Java
Add the import at the top:
import com.sun.jna.Native;
Add the interface in your class. Its your C# function name preceded by the letter "I":
public interface IYourFunction extends com.sun.jna.Library
{
public int YourFunction(String tStr);
};
Call your DLL where you need it in your class:
IYourFunction iYourFunction = (IYourFunction )Native.loadLibrary("full or relative path to DLL withouth the .dll extention", IYourFunction.class);//call JNA
System.out.println("Returned: " + IYourFunction.YourFunction("some parameter"));
EDIT:
If the DLL is 32bit, the JDK/JRE has to be 32bit as well. Add the following check to your code:
if(!System.getProperty("os.arch").equals("x86")) {
throw new Exception(".NET DLL " + "32bits JRE/JDK is required. Currently using " + System.getProperty("os.arch") + ".\r\nTry changing your PATH environement variable.");
}
Could you please advice. I have simple DLL (under Windows 7 64-bit) and simple Java code with JNA access to this DLL.
The problem is when I use 64-bit version of this DLL looks like I can't get parameter from Java in my DLL test function giveIntGetInt, and I have no problem when I use 32-bit DLL.
Where I was wrong?
Thank you!
import com.sun.jna.Library;
import com.sun.jna.Native;
import com.sun.jna.Platform;
import com.sun.jna.Pointer;
/** Simple example of native library declaration and usage. */
public class HelloWorld {
public interface simpleDLL extends Library {
simpleDLL INSTANCE = (simpleDLL) Native.loadLibrary(
(Platform.isWindows() ? "simpleDLL" : "simpleDLLLinuxPort"), simpleDLL.class);
int giveIntGetInt(int a); // int giveIntGetInt(int a);
}
public static void main(String[] args) {
int b = simpleDLL.INSTANCE.giveIntGetInt(2);
System.out.println("Hello, World\n");
System.out.println(String.format("Argument %d", b));
}
}
this is C dll method:
int simpleDLL::giveIntGetInt(int a)
{
return 2*a;
}
For example, this is I've got with 64-bit dll:
Hello, World
Argument 181140
32-bit dll:
Hello, World
Argument 4