Sigar UnsatisfiedLinkError - java

I'm new to Sigar. I would like to run a simple test to know how I can monitor my system.
I added sigar-1.6.4 and log4j as external libraries, but when I go to run it, I face this error:
Exception in thread "main" java.lang.UnsatisfiedLinkError: org.hyperic.sigar.Sigar.getCpuInfoList()[Lorg/hyperic/sigar/CpuInfo;
at org.hyperic.sigar.Sigar.getCpuInfoList(Native Method)
Here is my code:
import java.util.Map;
import org.hyperic.sigar.CpuInfo;
import org.hyperic.sigar.FileSystem;
import org.hyperic.sigar.Sigar;
import org.hyperic.sigar.SigarException;
public class Test {
/**
* #param args
*/
public static void main(String[] args) {
Sigar sigar = new Sigar();
CpuInfo[] cpuinfo = null;
try {
cpuinfo = sigar.getCpuInfoList();
} catch (SigarException se) {
se.printStackTrace();
}
System.out.println("---------------------");
System.out.println("Sigar found " + cpuinfo.length + " CPU(s)!");
System.out.println("---------------------");
}
}
Any help would be appreciated.

I understood the problem!
I have to use the following JVM Argument:
-Djava.library.path="./lib"
in Run Configuration, Arguments tab, VM arguments in eclipse, while the contnet of sigar-bin/lib is in lib folder.

Sigar works via JNI. As such, the appropriate .so or .dll file needs to be in the path specified by the java.library.path property.
Check your sigar distribution - the zip file, I mean. Unzip it and copy the contents of
sigar-bin\lib to a location accessible by your Path, PATH, and LD_LIBRARY_PATH environment variables. Usually, only one file needs to be accessible per platform.
That should do the trick, if it doesn't, let me know and I'll see what I can do.

Related

How to Obtain Java Thread Dump in JRE 1.8.0_144

I've been trying to find ways to obtain the Thread Dump from a Java Application on my Windows server running on jre 1.8.0_144.
Non of the monitoring utilities like jcmd jstack jconsole are available in either the bin or lib folders of the Java environment directory.
I have come across several applications online that claim to perform the same task but haven't found a reliable one yet.
Changing the JRE version, unfortunately, has been ruled out as an option
There is a way if you are running with tools.jar available, that is running from a JDK instead of a stock JRE. However given that you don't have the jcmd, jstack, jconsole tools available, this is unlikely.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.lang.reflect.Method;
// You need to add tools.jar to the classpath to get this
import com.sun.tools.attach.VirtualMachine;
public class Main {
public static void main(String[] args) throws Exception {
// Here you will need to use JNI or JNA to get the current process' PID
// because the JRE version you are using doesn't have any other way.
long pid = getCurrentPid(); // you need to create this method
VirtualMachine vm = VirtualMachine.attach(""+pid);
Method m = vm.getClass().getMethod("remoteDataDump", Object[].class);
InputStream in = (InputStream) m.invoke(vm, new Object[] { new Object[0] } ); // awkward due to nested var-args
try (BufferedReader buffer = new BufferedReader(new InputStreamReader(in))) {
buffer.lines().forEach(System.out::println);
}
}
}

Using VC++ dll using JNA

I am using a BTICARD.DLL, which is the dll of Arinc429 card. I need to write wrapper class in Java for the functions like BTICard_CardOpen for example.
I Had written an interface below BTICardAPI.java:
package NLIPjt;
import com.sun.jna.win32.StdCallLibrary;
import com.sun.jna.Native;
// import com.sun.jna.ptr.IntByReference;
import com.sun.jna.Pointer;
public interface BTICardAPI extends StdCallLibrary {
BTICardAPI INSTANCE = (BTICardAPI) Native.loadLibrary("BTICARD", BTICardAPI.class);
int BTICard_CardOpen(Pointer LPHCARD, int cardnum);
}
and my Java implementation prog
BTICardTest.java:
package NLIPjt;
// import com.sun.jna.ptr.IntByReference;
import com.sun.jna.Pointer;
public class BTICardTest {
public static void main(String args[]) {
BTICardAPI BTI1 = BTICardAPI.INSTANCE;
int iErr;
int CardNo = 0;
Pointer CardHandle = null;
iErr = BTI1.BTICard_CardOpen(CardHandle, CardNo);
System.out.println("Error Value: " + iErr);
}
}
i get the following error in netbeans IDE:
Exception in thread "main" java.lang.UnsatisfiedLinkError: Error looking up function 'BTICard_CardOpen': The specified procedure could not be found.
at com.sun.jna.Function.<init>(Function.java:245)
at com.sun.jna.NativeLibrary.getFunction(NativeLibrary.java:566)
at com.sun.jna.NativeLibrary.getFunction(NativeLibrary.java:542)
at com.sun.jna.NativeLibrary.getFunction(NativeLibrary.java:528)
at com.sun.jna.Library$Handler.invoke(Library.java:228)
at com.sun.proxy.$Proxy0.BTICard_CardOpen(Unknown Source)
at NLIPjt.BTICardTest.main(BTICardTest.java:14)
Looking for a solution!!
According to the documentation you need to make the library available. There are three ways to do this.
Make your target library available to your Java program. There are
several ways to do this:
The preferred method is to set the jna.library.path system property to
the path to your target library. This property is similar to
java.library.path, but only applies to libraries loaded by JNA.
Change the appropriate library access environment variable before
launching the VM. This is PATH on Windows, LD_LIBRARY_PATH on Linux,
and DYLD_LIBRARY_PATH on OSX.
Make your native library available on your classpath, under the path
{OS}-{ARCH}/{LIBRARY}, where {OS}-{ARCH} is JNA's canonical prefix for
native libraries (e.g. win32-x86, linux-amd64, or darwin). If the
resource is within a jar file it will be automatically extracted when
loaded.

UnsatisfiedLinkError: no opencv_java249 in java.library.path

Running into some problems making a piece of code run on my mac.
Had someone write me an image analysis java app but I keep getting this error when trying to run it on netbeans.
run: Exception in thread "main" java.lang.UnsatisfiedLinkError: no
opencv_java249 in java.library.path at
java.lang.ClassLoader.loadLibrary(ClassLoader.java:1857) at
java.lang.Runtime.loadLibrary0(Runtime.java:870) at
java.lang.System.loadLibrary(System.java:1119) at
image.prossing.Test.main(Test.java:28) Java Result: 1 BUILD SUCCESSFUL
(total time: 0 seconds)
Have the netbeans project, and added the necessary jar files as libraries. The programmer told me to download the correct OpenCV version and copy the opencv.dll file to my java/jre/bin folder. But I cannot find the dll file or the java/jre folder.
I know most programming happens on windows for a reason. Hope someone can help me resolve this issue and run this application on my mac.
Here is the first part of the code, the part that is most probably creating the error:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package image.prossing;
/**
*
* #author Dumith Salinda
*/
import java.util.ArrayList;
import java.util.List;
import org.opencv.core.Core;
import static org.opencv.core.Core.FONT_HERSHEY_SIMPLEX;
import org.opencv.core.Mat;
import org.opencv.core.MatOfPoint;
import org.opencv.core.Point;
import org.opencv.core.Rect;
import org.opencv.core.Scalar;
import org.opencv.highgui.Highgui;
import org.opencv.imgproc.Imgproc;
public class Test {
public static void main(String[] args) {
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
Sorry if it's not that clear, let me know what info to add if something is missing or not clear.
Would truly appreciate any help you could give. Sincerely
Meir Warcel
Look into your OpenCV directory;
For an example this; (installed using brew install opencv3 --with-java --with-python3)
/usr/local/Cellar/opencv3/XXX/share/OpenCV/java
You will see;
libopencv_javaXXX.so opencv-XXX.jar
Now that you already have OpenCV's native library for Java (libopencv_javaXXX.so) compiled with you, the only thing left is, mac's dynamic library.
Link libopencv_javaXXX.so to libopencv_javaXXX.dylib;
ln -s libopencv_javaXXX.so libopencv_javaXXX.dylib
Now add /usr/local/Cellar/opencv3/XXX/share/OpenCV/java as Native Library Locations in IntelliJ or something similar in Eclipse.
Or add this to your JVM arguments;
-Djava.library.path=/usr/local/Cellar/opencv3/XXX/share/OpenCV/java
On a mac running OSX Yosemite, I dropped the libopencv_java2412.dylib file into /Library/Java/Extensions and it worked.
After you build opencv, the libopencv_java2412.dylib is generated in /build/lib.
After Spending a lots of time , and using different suggestions from StackOverflow I managed to get solution for windows. but I am adding a solution for mac as well. hope it should work.
Load your lib as per your system configuration.
private static void loadLibraries() {
try {
InputStream in = null;
File fileOut = null;
String osName = System.getProperty("os.name");
String opencvpath = System.getProperty("user.dir");
if(osName.startsWith("Windows")) {
int bitness = Integer.parseInt(System.getProperty("sun.arch.data.model"));
if(bitness == 32) {
opencvpath=opencvpath+"\\opencv\\x86\\";
}
else if (bitness == 64) {
opencvpath=opencvpath+"\\opencv\\x64\\";
} else {
opencvpath=opencvpath+"\\opencv\\x86\\";
}
}
else if(osName.equals("Mac OS X")){
opencvpath = opencvpath+"Your path to .dylib";
}
System.out.println(opencvpath);
System.load(opencvpath + Core.NATIVE_LIBRARY_NAME + ".dll");
} catch (Exception e) {
throw new RuntimeException("Failed to load opencv native library", e);
}
}
2.now use this method as per your need
public static void main(String[] args) {
loadLibraries();
}
Building on Harsh Vakharia's answer i tried installing OpenCV on my mac with macports:
sudo port install opencv +java
ls /opt/local/share/OpenCV/java
libopencv_java343.dylib opencv-343.jar
To use this library I was hoping to be able to modify the library path at runtime which was discussed in
Adding new paths for native libraries at runtime in Java
And ended up with the following helper class and unit test. The code is now part of the
Self Driving RC-Car open Source project in which I am a comitter.
JUnit Test
/**
* #see <a href=
* 'https://stackoverflow.com/questions/27088934/unsatisfiedlinkerror-no-opencv-java249-in-java-library-path/35112123#35112123'>OpenCV
* native libraries</a>
* #throws Exception
*/
#Test
public void testNativeLibrary() throws Exception {
if (debug)
System.out.println(String.format("trying to load native library %s",
Core.NATIVE_LIBRARY_NAME));
assertTrue(NativeLibrary.getNativeLibPath().isDirectory());
assertTrue(NativeLibrary.getNativeLib().isFile());
NativeLibrary.load();
}
NativeLibrary
package com.bitplan.opencv;
import java.io.File;
import java.lang.reflect.Field;
import java.util.Arrays;
import org.opencv.core.Core;
/**
* load OpenCV NativeLibrary properly
*/
public class NativeLibrary {
protected static File nativeLibPath = new File("../lib");
/**
* get the native library path
*
* #return the file for the native library
*/
public static File getNativeLibPath() {
return nativeLibPath;
}
/**
* set the native library path
*
* #param pNativeLibPath
* - the library path to use
*/
public static void setNativeLibPath(File pNativeLibPath) {
nativeLibPath = pNativeLibPath;
}
/**
* get the current library path
*
* #return the current library path
*/
public static String getCurrentLibraryPath() {
return System.getProperty("java.library.path");
}
/**
* Adds the specified path to the java library path
*
* #param pathToAdd
* the path to add
* #throws Exception
* #see <a href=
* 'https://stackoverflow.com/questions/15409223/adding-new-paths-for-native-libraries-at-runtime-in-java'>Stackoverflow
* question how to add path entry to native library search path at
* runtime</a>
*/
public static void addLibraryPath(String pathToAdd) throws Exception {
final Field usrPathsField = ClassLoader.class.getDeclaredField("usr_paths");
usrPathsField.setAccessible(true);
// get array of paths
final String[] paths = (String[]) usrPathsField.get(null);
// check if the path to add is already present
for (String path : paths) {
if (path.equals(pathToAdd)) {
return;
}
}
// add the new path
final String[] newPaths = Arrays.copyOf(paths, paths.length + 1);
newPaths[newPaths.length - 1] = pathToAdd;
usrPathsField.set(null, newPaths);
}
public static File getNativeLib() {
File nativeLib = new File(getNativeLibPath(),
"lib" + Core.NATIVE_LIBRARY_NAME + ".dylib");
return nativeLib;
}
/**
* load the native library by adding the proper library path
*
* #throws Exception
* - if reflection access fails (e.g. in Java9/10)
*/
public static void load() throws Exception {
addLibraryPath(getNativeLibPath().getAbsolutePath());
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
}
}
Exception is occurring from below line of code:
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
Your program is trying to load a native library by the name of argument in call to loadLibrary method, which it is not able to locate. Make sure that native library (opencv.dll) is placed at one of the locations present in java.library.path system property as JVM looks at these locations for loading any native library (which might not contain 'java/jre/bin').
You can print java.library.path in your program like below:
System.out.println(System.getProperty("java.library.path"));
You cannot just put Windows library (dll file) on Mac and have it running - you need to compile the library for Mac first (or get Mac version of the library).
Please see here for tips on how to do it:
.dll Equivalent on Mac OS X
How do third-party libraries work in Objective-C and Xcode?
How to use a Windows DLL with Java in Mac OS X?
Instead of struggling with manual installation of OpenCV libraries I suggest you use OpenCV Java library packaged by OpenPnP (https://github.com/openpnp/opencv) that includes all required DLL.
It does not require additonal steps except of adding it to your build automation tool configuration (Gradle in my case) and adding the following code to load the library:
System.loadLibrary(org.opencv.core.Core.NATIVE_LIBRARY_NAME);
Just add into the path the folder where your opencv_java249.dll is; it would be something like C:\bin\opencv\build\java\x32 or C:\bin\opencv\build\java\x64 depending of your machine architecture. The problem is that java.library.path is actually the path variable.
netebans right klick project chosew properti
chose run, working direktory, click Browser change to opencv folder, release/lib,

Exception in thread "main" java.lang.UnsatisfiedLinkError: jnidispatch (/com/sun /jna/win32-x86/jnidispatch.dll) not found in resource path

I have a small test program which runs perfectly in the JBuilder 6 debugger. When I make a .jar file and run it I get an error
>java -jar testadll.jar
Start of DLL test
Exception in thread "main" java.lang.UnsatisfiedLinkError: jnidispatch (/com/sun
/jna/win32-x86/jnidispatch.dll) not found in resource path
at com.sun.jna.Native.loadNativeLibraryFromJar(Native.java:708)
at com.sun.jna.Native.loadNativeLibrary(Native.java:685)
at com.sun.jna.Native.<clinit>(Native.java:109)
at testadll.TestThisDLL$PenniesLib.<clinit>(TestThisDLL.java:24)
at testadll.TestThisDLL.main(TestThisDLL.java:33)
I have searched my drive and there is no jnidispatch.dll on it.
The program is
package testadll;
import com.sun.jna.Library;
import com.sun.jna.Native;
//import com.sun.jna.NativeLong;
import com.sun.jna.Platform;
import com.sun.jna.win32.StdCallLibrary;
//import com.sun.jna.*;
public class TestThisDLL {
public interface PenniesLib extends StdCallLibrary {
PenniesLib INSTANCE = (PenniesLib) Native.loadLibrary(
"PenniesLib", PenniesLib.class);
int a();
}
public static void main( String args[] ) {
System.out.println("Start of DLL test");
//TestDLL t = new TestDLL();
//System.out.println("DLL loaded");
int value = PenniesLib.INSTANCE.a();
System.out.println("DLL response is " + String.valueOf(value));
}
}
You've apparently merged JNA's classes with your own jar file, but omitted its native support. Ensure that all files from the original jna.jar (not just class files) are copied to the new destination and that their original paths are preserved.
Specifically, your jar file must include com/sun/jna/win32-x86/jnidispatch.dll. If you want to include support for other platforms, you must include com/sun/jna/*/jnidispatch as well.
You should use a version of jna.jar, that supports 64 bit, for example
jna-4.1.0.jar or jna-3.4.0.jar.

java.awt.Desktop.open doesn’t work with PDF files?

It looks like I cannot use Desktop.open() on PDF files regardless of location. Here's a small test program:
package com.example.bugs;
import java.awt.Desktop;
import java.io.File;
import java.io.IOException;
public class DesktopOpenBug {
static public void main(String[] args)
{
try {
Desktop desktop = null;
// Before more Desktop API is used, first check
// whether the API is supported by this particular
// virtual machine (VM) on this particular host.
if (Desktop.isDesktopSupported()) {
desktop = Desktop.getDesktop();
for (String path : args)
{
File file = new File(path);
System.out.println("Opening "+file);
desktop.open(file);
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
If I run DesktopOpenBug with arguments c:\tmp\zz1.txt c:\tmp\zz.xml c:\tmp\ss.pdf (3 files I happen to have lying around) I get this result: (the .txt and .xml files open up fine)
Opening c:\tmp\zz1.txt
Opening c:\tmp\zz.xml
Opening c:\tmp\ss.pdf
java.io.IOException: Failed to open file:/c:/tmp/ss.pdf. Error message:
The parameter is incorrect.
at sun.awt.windows.WDesktopPeer.ShellExecute(Unknown Source)
at sun.awt.windows.WDesktopPeer.open(Unknown Source)
at java.awt.Desktop.open(Unknown Source)
at com.example.bugs.DesktopOpenBug.main(DesktopOpenBug.java:21)
What the heck is going on? I'm running WinXP, I can type "c:\tmp\ss.pdf" at the command prompt and it opens up just fine.
edit: if this is an example of Sun Java bug #6764271 please help by voting for it. What a pain. >:(
I never knew about this Desktop command, untill recently through this post:
would Java's Runtime.getRuntime().exec() run on windows 7?
Previously i have been using:
Runtime.getRuntime().exec("rundll32 SHELL32.DLL,ShellExec_RunDLL "+ myfile);
And it has always worked for me. If your method does not work, may be you can think about try this command.
If you switch the order of your arugments does that cause one of the other files to get that same error. I wonder if you need to trim the end of the path before calling the File constructor.
umm...yeah ignore that... check the documentation of Desktop.open. open throws an IO exception "if the specified file has no associated application or the associated application fails to be launched " ... also from the top of the page... "The mechanism of registereing, accessing, and launching the associated application is platform-dependent. "
code for the Desktop class: http://fuseyism.com/classpath/doc/java/awt/Desktop-source.html
The open method calls DesktopPeer.open.
DesktopPeer source: http://www.jdocs.com/javase/7.b12/java/awt/peer/DesktopPeer.html
DesktopPeer is implementation specific.
Here is source for a Windows-specific implementation:
http://www.java2s.com/Open-Source/Java-Document/6.0-JDK-Platform/windows/sun/awt/windows/WDesktopPeer.java.htm
open->ShellExecute->(Native)ShellExecute
Native ShellExecute is a wrapper for Win32 ShellExecute. Here is info on the function.
http://msdn.microsoft.com/en-us/library/bb762153(VS.85).aspx
My suggestion for a work around would be to write your own implmentation of the ShellExecute function. Here is source from someone who did it. http://www.heimetli.ch/shellexec.html

Categories

Resources