Results 1 to 4 of 4
  1. #1
    java_aim is offline Member
    Join Date
    Oct 2011
    Posts
    15
    Rep Power
    0

    Default Calling a c/c++ function from a java program.

    Dear all friends:

    I have been trying to call a c/c++ function from a java program. The c function is simply printing "Hello World!".
    I could obtain libHelloNative.so by compiling HelloNative.c and using the command:
    gcc -shared -o libHelloNative.so HelloNative.o

    Yet I got the error message by executing the running command: java -cp . HelloNativeTest

    Exception in thread "main" java.lang.UnsatisfiedLinkError: no HelloNative in java.library.path
    at java.lang.ClassLoader.loadLibrary(ClassLoader.java :1681)
    at java.lang.Runtime.loadLibrary0(Runtime.java:840)
    at java.lang.System.loadLibrary(System.java:1047)
    at HelloNative.<clinit>(HelloNative.java:7)
    at HelloNativeTest.main(HelloNativeTest.java:4)

    Actually I am currently using "eclipse" to compile and run the program. I'm not sure what's happening.
    Perhaps, it's about finding and linking the *.so library file. Could anyone tell me how to successfully call the c/c++ function from the java program?

    Thank you in advance.
    Aim.

  2. #2
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,712
    Rep Power
    5

    Default Re: Calling a c/c++ function from a java program.

    The error seems to be referring to "HelloNative", while your SO file apparently is called "libHelloNative". Somewhere you have a typo in a name.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  3. #3
    java_aim is offline Member
    Join Date
    Oct 2011
    Posts
    15
    Rep Power
    0

    Default Re: Calling a c/c++ function from a java program.

    I should tell you guys that I can now solve the issue. The solution is to use this command:

    java -Djava.library.path=. -cp . HelloNativeTest

    That's all. It's all working fine.
    Have fun.

  4. #4
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,712
    Rep Power
    5

    Default Re: Calling a c/c++ function from a java program.

    That may be a difference between Linux/Mac and Windows - on Windows you don't need to add the current working directory to the library path since Windows itself will already look there by default. I guess this is not true on *nix based operating systems.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

Similar Threads

  1. Calling a Function
    By skmightymouse in forum New To Java
    Replies: 16
    Last Post: 04-24-2012, 10:39 PM
  2. Replies: 3
    Last Post: 01-26-2012, 11:20 PM
  3. Calling function in Javascript- from other function
    By jdigger in forum New To Java
    Replies: 1
    Last Post: 02-27-2011, 09:00 PM
  4. Calling the Function again
    By Swetz55 in forum New To Java
    Replies: 2
    Last Post: 12-19-2009, 11:10 AM
  5. Calling Java Function through JSP
    By Vikrant Pawar in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 06-06-2008, 06:28 AM

Posting Permissions

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