Including Native Library in Netbeans
Asked Answered
O

3

10

I am trying to read portable devices from java signed applet.... I found a jmtp library on http://code.google.com/p/jmtp/w/list to get access to portable devices but when i run it in netbeans it gives error

    Exception in thread "main" java.lang.UnsatisfiedLinkError: no jmtp in java.library.path
        at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1860)
        at java.lang.Runtime.loadLibrary0(Runtime.java:845)
        at java.lang.System.loadLibrary(System.java:1084)
        at jmtp.PortableDeviceManagerImplWin32.(PortableDeviceManagerImplWin32.java:38)
        at jmtp.PortableDeviceManager.(PortableDeviceManager.java:34)
        at jmtp.Jmtp.main(Jmtp.java:23)
    Java Result: 1
    

I searched and found that i have to include .dll file as native library in project of jmtp... I right-clicked on project and navigated to properties and then Selected "Run" and Selected VM Option as

-Djava.library.path="c:\jmtp\native\windows"
and placed that jmtp.dll file in c:\jmtp folder

But same error appears constantly my code is

    package jmtp;

import jmtp.PortableDevice;
import jmtp.PortableDeviceManager;
import jmtp.PortableDeviceObject;
import jmtp.PortableDeviceStorageObject;

public class Jmtp {

    public static void main(String[] args) {
        PortableDeviceManager manager = new PortableDeviceManager();
        PortableDevice device = manager.getDevices()[0]; 

        // Connect to my mp3-player
        device.open();
        System.out.println(device.getModel());
        System.out.println("---------------");

        // Iterate over deviceObjects
        for(PortableDeviceObject object : device.getRootObjects()) {

            // If the object is a storage object
            if(object instanceof PortableDeviceStorageObject) {
                PortableDeviceStorageObject storage = (PortableDeviceStorageObject)object;

                for(PortableDeviceObject o2 :  storage.getChildObjects()){
                    System.out.println(o2.getName());
                 }              
            }
        }
        manager.getDevices()[0].close();

    }
}

please, tell me what is the issue

Orlando answered 9/10, 2012 at 10:54 Comment(1)
You can also copy and paste the arguments directly from the bat files attached in the source archive.Perforation
O
26

Finally i found the solution, In order to include native library we need to add following steps in netbeans

     ==>Right click on the Project
     ==>Properties
     ==>Click on RUN
     ==>VM Options : -Djava.library.path="C:\Your Directory where Dll is present"
     ==>Ok
    
Orlando answered 10/10, 2012 at 9:0 Comment(3)
This has worked for me to run the project in Netbeans, but once the jar is built the same error is back. So it's not helping in the long run.Cyclops
Works for me, don't include the dll in the path, only the directory where it resides.Toein
How can we have this dll/settings in once outpur jar is build?Crunode
A
6

You can also load the library from within the program using this line:

System.loadLibrary("jmtp")

Place the folder containing the file jmtp.dll directly under the Java project.

Alternatively, I tried this and it works:

System.setProperty( "java.library.path", "libs" );

Whereas "libs" is the folder that contained the dll and is placed directly under the java project folder.

I noticed that the jmtp.dll gives the following exception with a 64-bit JDK:

Exception in thread "main" java.lang.UnsatisfiedLinkError: libs/jmtp (.\libs/jmtp.dll is not a valid Win32 application. )
    at java.lang.ClassLoader.loadLibraryWithPath(ClassLoader.java:1018)
    at java.lang.ClassLoader.loadLibraryWithClassLoader(ClassLoader.java:982)
    at java.lang.System.loadLibrary(System.java:506)
    at podcasts.Transferer.main(Transferer.java:28)

You will need to run the program on a 32-bit JDK for this to work.

Attention answered 26/10, 2013 at 1:3 Comment(0)
C
1

After struggling with this for a while, I've found a working solution to the problem.

I used System.out.println(System.getProperty("java.library.path")); within my code to find out what the actual value for java.library.path was on my system. It's at least a concatenation of the %PATH% variable for the system and user environment variables. So all I needed to the was add the location of my libraries to either of those and the problem went away.

Carpentry answered 17/6, 2016 at 14:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.