Results 1 to 14 of 14
  1. #1
    qin__23 is offline Member
    Join Date
    Apr 2012
    Posts
    5
    Rep Power
    0

    Exclamation JNI, C DLL and glassfish

    Hello all,

    Here's my issue:

    I've got some java code running in glassfish application server (EJBs) and I need to call a C DLL from this java code.

    But I don't have access to the source of this DLL, just the DLL itself.

    So can I call C DLL functions from java in an application server environment, and if so, how do i do this?

    Thanks!

    Rik

  2. #2
    jlczuk is offline Senior Member
    Join Date
    Apr 2012
    Location
    New York State of Confusion, USA
    Posts
    137
    Blog Entries
    1
    Rep Power
    0

    Default Re: JNI, C DLL and glassfish

    You certainly can, if you know the interfaces to the DLL. You would use Java Native Methods (JNI as you've correctly identified in your subject). You can learn more about how to do that from the JNI Documentation and from this JNI Tutorial.

  3. #3
    Sierra is offline AN21XX
    Join Date
    Mar 2012
    Location
    Munich
    Posts
    297
    Rep Power
    3

    Default Re: JNI, C DLL and glassfish

    I suggest you google JNA which is a API for the easy use of JNI with "dll" and "so" libraries. I used it to access third party dlls to work with microcontrollers and it works really well for me.

  4. #4
    jlczuk is offline Senior Member
    Join Date
    Apr 2012
    Location
    New York State of Confusion, USA
    Posts
    137
    Blog Entries
    1
    Rep Power
    0

    Default Re: JNI, C DLL and glassfish

    Cool, learn something new every day! Thank you.

  5. #5
    qin__23 is offline Member
    Join Date
    Apr 2012
    Posts
    5
    Rep Power
    0

    Default Re: JNI, C DLL and glassfish

    Thanks all.

    One further question though: I decided to go with JNI, and the problem i've run into is that when I exported the name of the functions in the dll using regasm,
    they contained "?" and "@" characters, which can't be declared as method names in java.

    Any way around this?
    Thanks.

    Rik

  6. #6
    Sierra is offline AN21XX
    Join Date
    Mar 2012
    Location
    Munich
    Posts
    297
    Rep Power
    3

    Default Re: JNI, C DLL and glassfish

    Did you try to import them without the special characters?

    EDIT: Note that you can get into trouble with .NET dlls as they are somehow not compatible with the calling conventions.

  7. #7
    qin__23 is offline Member
    Join Date
    Apr 2012
    Posts
    5
    Rep Power
    0

    Default Re: JNI, C DLL and glassfish

    Yep.

    I don't believe the dll was created in .NET. I'm pretty sure it was coded in C.

    So the method that I'm trying to use is GetDefaultReadername, so that's what I called it in java.

    But regasm displays it as ?GetDefaultReadername@@YAHPAD@Z and that's not allowed in java.

    So the dll loads file, but I get the following error when I run it:

    UnsatisfiedLinkError: GetDefaultReadername
    at jnitest.GetDefaultReadername(Native Method)
    .....

    So my guess would be that it can't find the GetDefaultReadername method in the dll, because the name is different.

    Any help would be appreciated.

    Thanks.

    Rik

  8. #8
    jlczuk is offline Senior Member
    Join Date
    Apr 2012
    Location
    New York State of Confusion, USA
    Posts
    137
    Blog Entries
    1
    Rep Power
    0

    Default Re: JNI, C DLL and glassfish

    So you have coded your JNI to call ?GetDefaultReadername@@YAHPAD@Z. My first inclination would be to code it to just call GetDefaultReadername and see if that works.

  9. #9
    qin__23 is offline Member
    Join Date
    Apr 2012
    Posts
    5
    Rep Power
    0

    Default Re: JNI, C DLL and glassfish

    Actually i've coded my JNI to call GetDefaultReadername and i'm shown that error.

    So i'd actually like to call the ?GetDefaultReadername@@YAHPAD@Z but I'm pretty sure it won't compile with that method name.

    Any ideas?

    Thanks.

    Rik

  10. #10
    Sierra is offline AN21XX
    Join Date
    Mar 2012
    Location
    Munich
    Posts
    297
    Rep Power
    3

    Default Re: JNI, C DLL and glassfish

    Well usually if the dll was coded in C it does not contain such names or characters (EDIT: maybe you compiled a debug version of that dll?). The function should have been exported with the extern "C" declaration if you use a C++ compiler.
    Regasm is a .NET framework tool, as far as I could see it will not change the dll itself, so there has to be an error somewhere, either it is no C dll or it is showing incorrect names.

    Maybe you could use dllexp to open your dll, it will display the correct names:
    DLL Export Viewer - view exported functions list in Windows DLL

    See the names there and post the result.
    Last edited by Sierra; 04-28-2012 at 12:03 AM.

  11. #11
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,436
    Blog Entries
    7
    Rep Power
    20

    Default Re: JNI, C DLL and glassfish

    If you use JNI you can't directly call your C functions from Java; if you have a Java definition such as:

    Java Code:
    native void foo(int param);
    The JVM expects a C function like this:

    Java Code:
    void foo_some_Java_mumbo_jumbo(more_jumbo* jvm, jint param) {
    ...
    }
    while your real C function just looks like this:

    Java Code:
    void foo() {
    ...
    }
    You have to call the last function from that second function so that makes the second function a wrapper for the last function. Summarizing: Java calls that second function (the one with the funny name); the second function calls the third function (the one that does the real work), so everything will be happy.

    If you use JNA you can skip that wrapper function, i.e. you can call the third function directly from Java; if I'm not mistaken JNA only runs on MS Windows boxes while JNI runs on all OSes.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  12. #12
    jlczuk is offline Senior Member
    Join Date
    Apr 2012
    Location
    New York State of Confusion, USA
    Posts
    137
    Blog Entries
    1
    Rep Power
    0

    Default Re: JNI, C DLL and glassfish

    I'll remind Rik of this original post:
    Quote Originally Posted by jlczuk View Post
    You certainly can, if you know the interfaces to the DLL. You would use Java Native Methods (JNI as you've correctly identified in your subject). You can learn more about how to do that from the JNI Documentation and from this JNI Tutorial.

  13. #13
    qin__23 is offline Member
    Join Date
    Apr 2012
    Posts
    5
    Rep Power
    0

    Default Re: JNI, C DLL and glassfish

    Thanks for all the responses.

    So do I need to do that 2-step process as JosAH posted, or can I go directly from java to the dll?

    I don't know if the DLL was exported using the extern "C" declaration, as I don't have the source. I may have to contact the original author.

    I did use DLL Export viewer, and it gave me the same output as regasm.

    Glassfish will probably be running in a Linux distro, so JNA may be out of the question.

    Thanks.

    Rik

  14. #14
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,436
    Blog Entries
    7
    Rep Power
    20

    Default Re: JNI, C DLL and glassfish

    Quote Originally Posted by qin__23 View Post
    So do I need to do that 2-step process as JosAH posted, or can I go directly from java to the dll?
    If you're using JNI you have to use that 'two step' approach as I outlined in my previous reply.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Glassfish v3 Problem
    By jaikumar in forum Java Servlet
    Replies: 0
    Last Post: 02-19-2012, 02:01 PM
  2. glassfish and hibernate
    By videanuadrian in forum Advanced Java
    Replies: 0
    Last Post: 09-13-2011, 06:51 AM
  3. GlassFish v3
    By ashin in forum NetBeans
    Replies: 2
    Last Post: 07-19-2011, 09:55 AM
  4. glassfish problem
    By ondra in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 02-21-2010, 01:20 AM
  5. Glassfish
    By ronguilmet in forum New To Java
    Replies: 0
    Last Post: 11-15-2009, 09:13 PM

Posting Permissions

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