Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

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

Threaded View

  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.


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