Results 1 to 15 of 15
  1. #1
    sara12345 is offline Member
    Join Date
    Dec 2009
    Posts
    59
    Rep Power
    0

    Default calling c code from java

    hello everyone
    I hope that someone can help me
    I have tried to run c code in java using Runtime but I got the following error:
    Debug assertion failed
    file:fprintf
    line:56
    althogh the c code is working correctly using visual c++ 6.0 and does not generate error and I'm able to run it using dll but I'm unable to run it using java

    Hlppppppppppppppp please
    thanks in advance

  2. #2
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    8

    Default

    Java Native Interface (JNI) Wikipedia
    Java Native Interface (JNI) Technical
    Article about Java native methods

    I haven't done this myself, but I've worked on projects that did. I hope the links help get you going.

  3. #3
    tim's Avatar
    tim
    tim is offline Senior Member
    Join Date
    Dec 2007
    Posts
    435
    Rep Power
    8

    Default

    Hi sara!

    I did some researching on JNI and it's a real pain to use. Java does not interface well with native API. Interfacing Java with C++ is difficult since Java imposes some requirements on the C++ DLL. So, you may need to write some code to do the adapting in the DLL. Steve's link to the Wikipedia article shows you how messy this can get. Doing this with C# is even harder.

    Good luck
    Tim
    Eyes dwelling into the past are blind to what lies in the future. Step carefully.

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

    Default

    Quote Originally Posted by tim View Post
    Hi sara!

    I did some researching on JNI and it's a real pain to use. Java does not interface well with native API.
    That is definitely not true; it's more likely that you don't know its 'mechanics';
    you can't directly call any C (or C++) function from Java; you need a little wrapper function for that; the 'javah' utillity generates the declarations of those wrappers given a Java class (with 'native' method declarations).

    You implement those wrappers; most of the time they just call the raw C (or C++) functions; that's all there is to it. Compile your C/C++ stuff, build a shared object (dynamic link library) out of it and put that library where it can be reached.

    From the Java side you have to load that library (System.loadLibrary( ... )) and you're in business; I don't consider that rocket science.

    kind regards,

    Jos

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    but debugging one of these hybrid beasts can sure be a pain in the tuchus.

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

    Default

    Quote Originally Posted by Fubarable View Post
    but debugging one of these hybrid beasts can sure be a pain in the tuchus.
    Sure, but don't debug both the C/C++ code and the Java code at the same time; I normally build/test/debug the C/C++ stuff first and when I'm convinced it works I link it to my Java code. The JNI part is just a 'pass along' piece of code and printf() is my debugger there. GDB can do it too but I'm no fan of all the different windows on my screen ;-)

    kind regards,

    Jos

  7. #7
    sara12345 is offline Member
    Join Date
    Dec 2009
    Posts
    59
    Rep Power
    0

    Default

    ok thanks alot for these nice replies
    but can somebody explain more to me because I don't know much about c programming I just take a program from the web and I want to execute it in my java code so I don't want to call any function of c code I just want to execute the c code and read the output file of c code in java
    I'm waiting for more explanation
    thanks alot

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

    Default

    Quote Originally Posted by sara12345 View Post
    ok thanks alot for these nice replies
    but can somebody explain more to me because I don't know much about c programming I just take a program from the web and I want to execute it in my java code so I don't want to call any function of c code I just want to execute the c code and read the output file of c code in java
    I'm waiting for more explanation
    thanks alot
    If you're talking about a 'complete program' have a look at the API for the ProcessBuilder, Process classes or the Runtime.exec( ... ) methods. They run complete programs for you. You don't need JNI for that. The errors you mentioned in your OP should and can be fixed.

    kind regards,

    Jos

  9. #9
    sara12345 is offline Member
    Join Date
    Dec 2009
    Posts
    59
    Rep Power
    0

    Default

    thanks for the fast reply
    yes I mean to run the whole program I have used the following method :

    import java.io.*;

    public class CommandExection {

    public CommandExection(String commandline) {

    try {
    String line;
    Process p = Runtime.getRuntime().exec(commandline);
    BufferedReader input =
    new BufferedReader
    (new InputStreamReader(p.getInputStream()));

    while ((line = input.readLine()) != null) {
    System.out.println(line);
    }

    input.close();
    } catch (Exception err) {
    err.printStackTrace();
    }
    }

    public static void main(String argv[]) {
    new CommandExection("c:\\CompChamps.exe");
    }

    but As I said I'm un able to run the c code from java, I don't know why?? when I have fixed the printf error the c program execute from the visual c++ basic but when I run it from dll it doesn't work and also it doesn't work I don't know why???

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

    Default

    Quote Originally Posted by sara12345 View Post
    but As I said I'm un able to run the c code from java, I don't know why?? when I have fixed the printf error the c program execute from the visual c++ basic but when I run it from dll it doesn't work and also it doesn't work I don't know why???
    What do you mean by 'run it from dll'? You can't run anything that is in a dll. You can run .exe and .bat files but no .dll files. Basically, you can run anything that you can run from the command line interpreter.

    kind regards,

    Jos

  11. #11
    sara12345 is offline Member
    Join Date
    Dec 2009
    Posts
    59
    Rep Power
    0

    Default

    sorry I have noticed that the dll is not working it either produces an error saying printf line:56 and when I fix the error it also doesn't work but it works correctly from visual c++ tool
    can some body help me please why is this???
    thanks alot

  12. #12
    sara12345 is offline Member
    Join Date
    Dec 2009
    Posts
    59
    Rep Power
    0

    Default

    sorry you are right I mean .exe ( the application file) I'm so sorry

  13. #13
    sara12345 is offline Member
    Join Date
    Dec 2009
    Posts
    59
    Rep Power
    0

    Default

    I mean the application file (.exe) of c code doesn't work but the c code works from the tool visual c++ 6.0
    can you explain to me why??

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

    Default

    Quote Originally Posted by sara12345 View Post
    I mean the application file (.exe) of c code doesn't work but the c code works from the tool visual c++ 6.0
    can you explain to me why??
    So your first task is to build a proper .exe file. That visual C++ tool might be of help. We can't help you without seeing the (relevant) code. This is not a Java problem, maybe a C/C++ forum can help you further.

    kind regards,

    Jos

  15. #15
    sara12345 is offline Member
    Join Date
    Dec 2009
    Posts
    59
    Rep Power
    0

Similar Threads

  1. Replies: 9
    Last Post: 12-21-2010, 03:37 PM
  2. help calling 1 .java from another.
    By New-to-this in forum New To Java
    Replies: 1
    Last Post: 08-14-2009, 05:22 AM
  3. calling the servelet code when the page is loaded
    By shivakumari in forum Java Servlet
    Replies: 1
    Last Post: 04-01-2009, 12:39 PM
  4. How to return to next line of code after calling a public class?
    By devdevi@comcast.net in forum New To Java
    Replies: 1
    Last Post: 02-02-2009, 07:46 PM
  5. All JSP expert / Is there JSP code for calling & sending messages? please help me
    By java student in forum JavaServer Pages (JSP) and JSTL
    Replies: 2
    Last Post: 12-02-2008, 11:51 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
  •