Results 1 to 9 of 9
  1. #1
    RichWade is offline Member
    Join Date
    Nov 2010
    Posts
    33
    Rep Power
    0

    Default NetBeans - Path to Native Library?

    I am using NetBeans (6.9.1) on Windows 7. I need to use a native library (DLL) that was built in "C". To get started, I have followed the steps described here:
    Java Tips - Simple example of using the Java Native Interface

    This uses a very simple example program that calls the native function print() to display the text "Hello World". This works fine from the console commands, but I am having trouble doing the same thing in NetBeans. My problem seems to be with setting the native library path. Below is the error I get when I run the program in NetBeans:

    Exception in thread "main" java.lang.UnsatisfiedLinkError: javaapplication1.Main.print()V
    at javaapplication1.Main.print(Native Method)
    at javaapplication1.Main.main(Main.java:28)
    Java Result: 1

    As I understand it, this indicates that the DLL with the native method cannot be found.

    I have tried tinkering with the Project Properties, Run, VM Options to set the Java Library Path:
    -Djava.library.path="."

    I'm still missing something here - nothing I have tried has worked so far.

    How should I be configuring NetBeans in order to find my native DLL? Any suggestions are appreciated.

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

  3. #3
    RichWade is offline Member
    Join Date
    Nov 2010
    Posts
    33
    Rep Power
    0

    Default

    I have tried putting the DLL at C:\, same result.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    So, I assume that you have used javah to create C header file and then using this have written your C/CPP file to go with it and act as bridge between Java and the DLL, right?

  5. #5
    RichWade is offline Member
    Join Date
    Nov 2010
    Posts
    33
    Rep Power
    0

    Default

    Correct - I have generated the header file and built the C file to DLL using the appropriate JNI syntax. All this works with javac, java commands from the console prompt. My problem is with getting NetBeans to do the same thing.

  6. #6
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by RichWade View Post
    I have tried putting the DLL at C:\, same result.
    No I mean in System32.

  7. #7
    RichWade is offline Member
    Join Date
    Nov 2010
    Posts
    33
    Rep Power
    0

    Default

    Yes, I tried putting it in \System32, same result.

  8. #8
    RichWade is offline Member
    Join Date
    Nov 2010
    Posts
    33
    Rep Power
    0

    Default

    A bit more information - it seems as if NetBeans IS finding the DLL when it is in the project folder, but the problem is related to calling the print() method in the DLL.

    This is the "C" code defining the print() method:
    JNIEXPORT void JNICALL
    Java_Main_print(JNIEnv *env, jobject obj)
    {
    printf("Hello World!\n");
    return;
    }

    As I understand it the "_Main_" part of the method name puts this in the "Main" class.

    In my NetBeans project i have the following:
    package javaapplication1;

    import java.util.Scanner;

    public class Main {

    /**
    * @param args the command line arguments
    */
    private native void print();

    public static void main(String[] args) {
    // TODO code application logic here
    ....


    NetBeans added the package statement:
    package javaapplication1;

    I did not have a package statement when using Notepad from the console for the initial test (which worked). Does the package statement add a namespace issue that needs a corresponding change in the definition of the method in the "C" code for the DLL?

  9. #9
    RichWade is offline Member
    Join Date
    Nov 2010
    Posts
    33
    Rep Power
    0

    Default

    Looks like my guess was correct - got it to work! I changed the C function to the following:

    JNIEXPORT void JNICALL
    Java_javaapplication1_Main_print(JNIEnv *env, jobject obj)
    {
    printf("Hello World!\n");
    return;
    }

    I still don't fully understand the JNI function declaration, but it seems like this puts the function in the Java:javaapplication1:Main namespace (and would not work until we declared the namespace completely).

    In the long run I want to encapsulate a complete "C" API for use from Java. Is there a way to define the package/namespace in a generic fashion so that other applications can use the native library without renaming all the functions to match the end-users namespace?

Similar Threads

  1. I want to put the native library in library path in mac os x
    By Hussain Ali in forum Advanced Java
    Replies: 2
    Last Post: 02-24-2010, 08:55 AM
  2. setting class-path & Library Path in ubantu
    By programmer_007 in forum Eclipse
    Replies: 18
    Last Post: 02-22-2010, 01:31 PM
  3. java.library.path problems
    By deepthought in forum New To Java
    Replies: 1
    Last Post: 02-15-2010, 09:06 AM
  4. URGENT: How to include a library in Java class path?
    By gsmurthy30 in forum Advanced Java
    Replies: 1
    Last Post: 09-15-2008, 09:39 AM
  5. Loading a library (for native methods)
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-23-2007, 08:45 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •