• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

getting variable of c in java

 
Ranch Hand
Posts: 518
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I need to get a variabe which is in a c program through java.Is it possible.if so.please help me with some link.

Thanks
 
Marshal
Posts: 79943
396
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try googling for JNI which means Java Native Interface, see whether there is anything in that which can help.
 
preethi Ayyappan
Ranch Hand
Posts: 518
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thankyou for your response.i will look at that
 
Campbell Ritchie
Marshal
Posts: 79943
396
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're welcome. I don't know whether it will work, but the JNI is the usual interface between Java and C.
 
preethi Ayyappan
Ranch Hand
Posts: 518
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I have tried a sample from JNI and tried to get the out put of c through java.I am posting the java and c code:

JNIDemo.java


Nativecode.c


I compiled the java program and created header file too.Then i have created library like


cc -o libnativecode.so -shared -I/usr/java/jdk/include -I/usr/java/jdk/include/linux Nativecode.c


Now libNativecode.so file is created in the dir /usr/java/jdk/include/linux.
Then I set the path environment variable to point the library file like


export LD_LIBRARY_PATH=/usr/java/jdk/include/linux:$LD_LIBRARY_PATH:


Then i run the java file like java JNIDemo.I am getting the following error:


Exception in thread "main" java.lang.UnsatisfiedLinkError: libNativecode: libNativecode.so: cannot open shared object file: No such file or directory
at java.lang.Runtime._load(libgcj.so.7rh)
at java.lang.Runtime.loadLibrary(libgcj.so.7rh)
at java.lang.System.loadLibrary(libgcj.so.7rh)
at JNIDemo.<clinit>(JNIDemo.java:3)
at java.lang.Class.initializeClass(libgcj.so.7rh)


I think i am wrong in setting the library path.Please point out me where am i wrong and help me to solve this problem.

Thanks in Advance
 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
when you said
the library is looked for in the directories that are included in the PATH variable, commonly /usr/bin or /usr/sbin .

however, it seems you compiled the code like:

cc -o libNativeCode.so ... NativeCode.c

which might have left the library(.so file) in your source code directory(assuming you saved the source file in home directory) itself.

try using where AbsolutePathToLibrary is a String like "~/libNativeCode.so" or"./libNativeCode.so"
 
Author
Posts: 836
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You need to tell Java where to find your library. This should work:where PATH_TO_LIB is the path to the directory containing your library, which may be "." if it's in the current directory.
 
Every snowflake is perfect and unique. And every snowflake contains a very tiny ad.
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic