This discussion is archived
7 Replies Latest reply: Feb 13, 2012 8:02 AM by 916747 RSS

JNI hides library functions?

916747 Newbie
Currently Being Moderated
I am working on a project which involves making a C API accessible to java programs. I have most of the capabilities working. Unfortunately, the two callback functions needed from the API are not fully working. I have the callback setup working properly (I am still working out some GlobalRef stuff and trying to understand AttachCurrentThread) in the situation that I do the callback from the main Java thread. I am having two main issues:

1) The callback is supposed to happen when a particular report comes in over USB from a microcontroller. Currently, the program is setup to call an intermediate C function which does some things then executes a CallVoidMethod(.....) into the java callback function. Currently, I don't think I even get into the intermediate C function, as I set a global in the function (and when I read it out in java, it never gets changed). I am more of a C guy, so the whole multi-thread world is pretty new to me. I feel like there was an important step here that I am missing.

2) When I make the C API dll with the wrapper functions, the only functions I can access after that are the java JNI functions. Why is that? How can I fix that? I am using cygwin/mingw32-make and a make file, but here is the gcc command executed:
gcc -mwin32 - shared -o API.dll *.o -lhid -lkernel32 -luser32 -lsetupapi -lwinmm -Wl,--kill-at

I have tried to read as much as I can about the Java Native functions and shared libraries, but nothing I have read so far indicates that Java would/should obscure the normal API functions.

Thanks for taking the time to read this and any help is greatly appreciated.
  • 1. Re: JNI hides library functions?
    jschellSomeoneStoleMyAlias Expert
    Currently Being Moderated
    ymgamal wrote:
    I am working on a project which involves making a C API accessible to java programs. I have most of the capabilities working. Unfortunately, the two callback functions needed from the API are not fully working. I have the callback setup working properly (I am still working out some GlobalRef stuff and trying to understand AttachCurrentThread) in the situation that I do the callback from the main Java thread. I am having two main issues:

    1) The callback is supposed to happen when a particular report comes in over USB from a microcontroller. Currently, the program is setup to call an intermediate C function which does some things then executes a CallVoidMethod(.....) into the java callback function. Currently, I don't think I even get into the intermediate C function, as I set a global in the function (and when I read it out in java, it never gets changed). I am more of a C guy, so the whole multi-thread world is pretty new to me. I feel like there was an important step here that I am missing.
    Add logging to the C code. The logger writes to a file at various points, based on the calls you make to the logger code. You then follow the process flow via the output in the file.

    You can use log4c but it is easy enough to write your own. Link for log4c follows. (The origination for that library is from log4j which is a java library.)

    http://log4c.sourceforge.net/

    >
    2) When I make the C API dll with the wrapper functions, the only functions I can access after that are the java JNI functions. Why is that? How can I fix that? I am using cygwin/mingw32-make and a make file, but here is the gcc command executed:
    gcc -mwin32 - shared -o API.dll *.o -lhid -lkernel32 -luser32 -lsetupapi -lwinmm -Wl,--kill-at
    What do you mean by "access"?

    Make will print the actual compilation lines so instead of guessing what it is using for a compilation turn on the make option that does the output and examine it in detail.
  • 2. Re: JNI hides library functions?
    EJP Guru
    Currently Being Moderated
    Re: JNI hides library functions?
    JNI cannot hide anything like that.
    2) When I make the C API dll with the wrapper functions, the only functions I can access after that are the java JNI functions.
    Meaning what? Compile errors? Link errors? Runtime errors?
  • 3. Re: JNI hides library functions?
    916747 Newbie
    Currently Being Moderated
    I don't know how to quote stuff on this forum....

    First, let me add some details about what I mean with the dll:

    When I run the gcc command to make the dll WITHOUT the C wrapper file, I get all the normal functions I expect. When I run the gcc command WITH the C wrapper file, I only get the wrapper functions as external or shared or whatever the term is that indicates they can be called by an outside function. I used "dll export viewer" on the two dlls and this is what I got:
    http://i277.photobucket.com/albums/kk74/ymgamal/dllExport.png



    jschell:
    I am already using print statements to see where I am at. None in the C code get called, unless I explicitly call a JNI function to it. This just further reinforces, that the normal dll C functions are not shared outside of the dll.

    EJP:
    -I figured that was probably the case, but I wasn't really sure. This was further reinforced as I ran our linux makefile and everything works, with exception that linux really hates a sound control function in the dll and throws a SIGSEGV error.
    -I get undefined reference errors to all the non-wrapper C functions when I try to compile some of the other software.

    It seems the issue is with the gcc command rather than a java/jni issue. If you guys have any ideas/suggestions on that front, I would greatly appreciate it.

    Edited by: ymgamal on Feb 13, 2012 6:22 AM
  • 4. Re: JNI hides library functions?
    gimbal2 Guru
    Currently Being Moderated
    I am already using print statements to see where I am at. None in the C code get called, unless I explicitly call a JNI function to it.
    I am really confused by this. How else would the C code be invoked? This is about JNI so it is about a Java application invoking native code. Of course you need to do an explicit JNI controlled call (IE. call a method in a Java class which is marked as native) to get the native code to do anything.

    In any case, you mention that you have other applications that need to share the same DLL. That seems strange to me; what I expect is that the one DLL is specific to your Java application, since it is basically determined by JNI / Javah how the outside interface is going to be (which functions will be exported, under which name, using which parameter/ return types, etc.). So how would you use that same DLL in other code?

    If you are attempting to append the JNI stuff to an existing DLL module, I wouldn't do that. I would keep your existing DLL exactly as it is and link to that DLL in your JNI wrapper DLL. So you get two DLLs - the DLL with your preexisting logic which other applications also use, and a DLL with the JNI wrapper functions which do basically not so much more as do calls into your primary DLL as a "man in the middle".


    Phew, tough subject :/ I hope any of that made sense.
  • 5. Re: JNI hides library functions?
    916747 Newbie
    Currently Being Moderated
    Yes, I need the dll to be accessible by non-java programs/other programs in general. I think the issue is coming from declspec declaration for SWIGEXPORT/JNIEXPORT, but I haven't been able to get the correct configurations as of yet for gcc/-Wl.

    Not a bad idea, due to my unfamiliarity with java/wrapper stuff, I assumed what they meant by "the wrapper functions must be in the dll" as a literal (I am not blonde, I swear ;)).

    I will look into keeping the java dll seperate from the original dll, so if you have any good links to start with, that would be greatly appreciated.
  • 6. Re: JNI hides library functions?
    gimbal2 Guru
    Currently Being Moderated
    ymgamal wrote:
    I will look into keeping the java dll seperate from the original dll, so if you have any good links to start with, that would be greatly appreciated.
    If you really are a "c guy", you shouldn't be having to ask this. Don't make it any more difficult than it needs to be. You have "a DLL", no matter where it comes from or what it is used for, that needs to interface with "another DLL". How do you effectively do that? Whatever the case, Java does not exist in that problem description so don't look for solutions relating to Java.
  • 7. Re: JNI hides library functions?
    916747 Newbie
    Currently Being Moderated
    I guess you are right, I work with microcontrollers and embedded systems. Thats where I get my C, not from windows C and dlls.

    Anyways, appreciate the help.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points