Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: calling native c code from java using JNI

  1. #1
    Junior Member
    Join Date
    Feb 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default calling native c code from java using JNI

    hi guys im fed up with jni couldnt even execute a simple helloworld program.. here is what i did as per the instruction given in a JNI book..

    STEP 1:
    Creating HelloWorld.java
    class HelloWorld {
    	private native void print();
     
    	public static void main(String[] args) {
    		new HelloWorld().print();
    	}
     
    	static {
    		System.loadLibrary("HelloWorld.dll");
    	}
    }
    compile HelloWorld.java to get HelloWorld.class

    STEP 2
    Creating a jni style header

    command: javah -jni HelloWorld will generate a header file HelloWorld.h

    STEP 3:
    Creating dll and lib files
    #include <jni.h>
    #include <stdio.h>
    #include "HelloWorld.h"
     
    JNIEXPORT void JNICALL Java_HelloWorld_print(JNIEnv *env, jobject obj)
    {
    	printf("Hello World!\n");
    	return;
    }
     
    int main(int argc, char **argv)
    {
    	// dummy
    	return 0;
    }
    compile with file along with the header in Visual Studio in a dll project will generate dll files

    STEP 4

    now i ve created a java class file header file and the library HelloWorld.dll

    when i execute the java HelloWorld.class it throws the the error

    Exception in thread "main" java.lang.UnsatisfiedLinkError: no HelloWorld.dll in
    java.library.path
    at java.lang.ClassLoader.loadLibrary(Unknown Source)
    at java.lang.Runtime.loadLibrary0(Unknown Source)
    at java.lang.System.loadLibrary(Unknown Source)
    at HelloWorld.<clinit>(HelloWorld.java:9)
    Could not find the main class: HelloWorld. Program will exit.


    guys please help me i dont know how to fix and i followed lot of other tutorials and i get the same error
    Last edited by helloworld922; February 4th, 2011 at 02:32 AM.


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: calling native c code from java using JNI

    Java's smart enough to figure out the extension of HelloWorld for you, and actually requires that you don't give it. The reason you don't want to put the extension on there is because different OS's use different extensions (for example, windows use .dll, linux use .so, etc.).

    static {
            System.loadLibrary("HelloWorld");
        }

  3. #3
    Junior Member
    Join Date
    Feb 2011
    Posts
    2
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: calling native c code from java using JNI

    Just on the off chance someone has this same issue under linux I would like to point out that your library needs to be lib[whatever].so and when you do the system.loadlibrary() you put in [whatever] instead of the loadlibrary function excluding the lib and .so. I ran into this issue not long ago and it drove me nuts until I figured it out.

  4. The Following User Says Thank You to FearTheCron For This Useful Post:

    helloworld922 (February 28th, 2011)

  5. #4
    Junior Member
    Join Date
    Nov 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: calling native c code from java using JNI

    Nex Gen Media Server is best known as a multi-purpose media streaming server to deliver live and stored video to a variety of devices. The same media server can be embedded into a mobile application to facilitate real time video communication...

    For More Info :- Java Outsourcing, Java Development, Hire Java Developers - Aegis

    Thanks.

Similar Threads

  1. calling c code from java
    By sara in forum Java Native Interface
    Replies: 3
    Last Post: April 6th, 2013, 09:53 PM
  2. Resize a window without native decorators
    By supertreta in forum AWT / Java Swing
    Replies: 0
    Last Post: January 11th, 2011, 02:06 PM
  3. Calling exe files from Java
    By linuxrockers in forum What's Wrong With My Code?
    Replies: 3
    Last Post: March 26th, 2010, 04:20 AM
  4. Java Code Help - Calling Method
    By KevinGreen in forum Object Oriented Programming
    Replies: 5
    Last Post: September 18th, 2009, 12:55 AM
  5. [SOLVED] How to call string in another class in java?
    By tazjaime in forum What's Wrong With My Code?
    Replies: 4
    Last Post: April 23rd, 2009, 09:31 AM

Tags for this Thread