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

    Default calling c program from java. hlp please

    hello everyone
    I have question
    I need to call c program from java
    I don't have problem with application or exe file in c it is working but when I call it from java it doesn't work there is no error but it doesn't work because the c program the console must be appeared so that I can enter the input file and then it produces the output file but in java I cann't see the console so please can you tell me the problem
    I have used the following code to call c from java:
    Java Code:
      String line;
    	           String output1 = "";
    	          
    	                 Process p = Runtime.getRuntime().exec("cpp.exe");
    	                 BufferedReader input = new BufferedReader
    	                       (new InputStreamReader(p.getInputStream()));
    	                 while ((line = input.readLine()) != null) {
    	                       output1 += (line + '\n');
    	                 }
    	                 input.close();

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

    Default

    I have said the console sorry it is not console it must take the input file name from the black screen when I press application file

  3. #3
    collin389 is offline Senior Member
    Join Date
    Nov 2009
    Posts
    235
    Rep Power
    5

    Default

    When you run it, do you see another console pop up, I'm not sure what you are trying to accomplish. Please explain better.

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

    Default

    no I didn't see any console except java console. I'm trying to call whole c program in java. the c program must take input file name in the black screen and must return output file. when I run java using the above code I didn't see any black screen how can I enter the input file name???
    can you please tell me what is the problem????
    thanks

  5. #5
    collin389 is offline Senior Member
    Join Date
    Nov 2009
    Posts
    235
    Rep Power
    5

    Default

    I think what you are saying is that you run the java from a console and it produces the output of the c program, but you cannot input anything. If that is correct the problem is that you do not get the c programs Output Stream, only the InputStream. What you can do is use a Scanner of system.in on your java program and send that to the c exe via a bufferedWriter of p.getOutputSteam()

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

    Default

    no. the c program must works as follows:
    it must take an input file from black screeen and it must return output file.
    when I call c program from java I didn't get any thing. the black screen doesn't appear to take input file and also there is no output file I don't know why
    although I have run c program from visual c++ it runs fine and also I have run it from command prompt and it works but I don't know what is the problem with java

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    You can hook onto both the C program's (executable program) standard input and output stream from your Java program, just do it in two different threads so one doesn't block the other: Java Programming [Archive] - standard input into a different process

    as Collin already has suggested.

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

    Default

    thanks for the reply but it also doesn't work I mean the command line must be appeared so that I can enter the input file name and then it produces output file
    but I didn't get any thing
    I have write
    Java Code:
      Runtime rt = Runtime.getRuntime();
    	         Process proc = rt.exec("cp1.exe");
    	         OutputStream os = proc.getOutputStream();
    	         DataOutputStream dos = new DataOutputStream(os);
    	         dos.writeUTF("s");
    	         os.close();
    can you tell me what is wrong ??

  9. #9
    raqman is offline Member
    Join Date
    Sep 2009
    Posts
    37
    Rep Power
    0

    Default

    Hi.
    Please refer to the url below:
    Calling C Code from Java
    Regards,
    raqman.
    Last edited by raqman; 01-03-2010 at 05:50 PM.
    RAQ Report: free Java reporting tool.

  10. #10
    Varun12345 is offline Member
    Join Date
    Jan 2010
    Posts
    4
    Rep Power
    0

    Cool

    how can i call a c++ program in java??

    anyone have idea?

    i tried but have a linking error

  11. #11
    Varun12345 is offline Member
    Join Date
    Jan 2010
    Posts
    4
    Rep Power
    0

    Default how call a c++ progaram in java

    i hava a java progaram
    class HelloWorld {
    public native void displayHelloWorld();

    static {

    System.loadLibrary("hello");
    }

    public static void main(String[] args) {
    System.out.println("\n Hey now calling display method\n");
    new HelloWorld().displayHelloWorld();
    }
    }


    also a c program


    #include<jni.h>
    #include"HelloWorld.h"
    #include<stdlib.h>
    #include<iostream.h>
    #include<conio.h>

    JNIEXPORT void JNICALL
    Java_HelloWorld_displayHelloWorld(JNIEnv *env, jobject obj)
    {
    printf("\n Hello compiling C file !!!! \n " );
    return;
    }


    using as

    java helloworld.java

    javah -jni helloworld

    gcc -I c:\jdk\include -g -c helloworldImp.c -o libhello.so

    but this gave many errors in jni.h

    how can it be used pls tell me ........
    thanks in advance...
    ;)

Similar Threads

  1. urgent:calling c program from java
    By rach in forum New To Java
    Replies: 6
    Last Post: 01-19-2010, 11:37 AM
  2. help calling 1 .java from another.
    By New-to-this in forum New To Java
    Replies: 1
    Last Post: 08-14-2009, 04:22 AM
  3. Calling a JFrame window from a command line program.
    By new_2_java in forum New To Java
    Replies: 7
    Last Post: 11-09-2008, 03:40 AM
  4. calling a java application from another
    By kratoras in forum New To Java
    Replies: 1
    Last Post: 06-14-2008, 02:54 AM
  5. Calling mainframe IMS program from Java
    By babu95 in forum New To Java
    Replies: 2
    Last Post: 12-21-2007, 01:08 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
  •