Page 1 of 2 12 LastLast
Results 1 to 20 of 22
  1. #1
    jojji is offline Member
    Join Date
    Jul 2010
    Posts
    11
    Rep Power
    0

    Default call c++ exe from java applet

    Hi ,

    I am trying to call c++ exe from java applet.For this purpose am using processbuilder.My code is as follows


    String line = "";
    String s = "C:\\Users\\madsaan\\Documents\\NetBeansProjects\\ CppApplication_2\\dist\\Debug\\Cygwin-Windows\\cppapplication_2.exe";
    ProcessBuilder pb = new ProcessBuilder(s);
    Process process = pb.start();
    InputStream is = process.getInputStream();
    BufferedReader br = new BufferedReader(new InputStreamReader(is));
    while ((line = br.readLine()) != null) {
    JOptionPane.showMessageDialog(Cytoscape.getDesktop (), line);
    }


    The c++ exe looks like this when started:


    1. Find the shortest path from s to t.
    2. Find the associating path from s to t within length l.
    3. Find the associating path from s to t within (1+beta) times of the shortest distance from s to t.
    4. Exit
    Please input the command (1-4):

    once u input the command ,for example 1 we get

    Please input s and t, separated by space:

    so how am i supposed to pass arguments line by line .
    I tried ProcessBuilder pb = new ProcessBuilder(s,"1","0 1");but all it does is print the following

    1. Find the shortest path from s to t.
    2. Find the associating path from s to t within length l.
    3. Find the associating path from s to t within (1+beta) times of the shortest distance from s to t.
    4. Exit

    how can i get it to accept all the arguments and give me the final output alone?

    Thanks.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,816
    Rep Power
    25

    Default

    how can i get it to accept all the arguments and give me the final output alone?
    To communicate with a process, you'd need to use the Streams available from the Process. Read from one and write to the other.
    If the program you are executing only gets its data from prompts and reads, then you have to read its prompts and write to its reads.

    Or is this an OS piping (> or <) thing where you can "prefeed" the reads by putting the answers in a file that the OS uses to respond to reads.
    Last edited by Norm; 07-27-2010 at 10:48 PM.

  3. #3
    jojji is offline Member
    Join Date
    Jul 2010
    Posts
    11
    Rep Power
    0

    Default

    Hi,

    Thanks for your reply.

    This is not a OS piping.I managed to get part of it working using input and outputstream
    The code is as follows

    ProcessBuilder pb = new ProcessBuilder(s);
    Process process = pb.start();
    final InputStream is = process.getInputStream();
    OutputStream out = process.getOutputStream();
    PrintWriter pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(out)));
    pw.println(1);

    new Thread(new Runnable() {

    public void run() {
    try {
    BufferedReader br = new BufferedReader(new InputStreamReader(is));
    String line;
    while ((line = br.readLine()) != null) {
    System.out.println(line);
    }
    } catch (java.io.IOException e) {
    }
    }
    }).start();
    pw.close();

    Right now am able to pass only the first argument.Am not able to pass the second set of arguments when it asks for

    Please input s and t, separated by space:

    Help!

    Thanks

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

    Default

    Just imagine you, innocently clicking a URL link; a HTML page loads with an applet; also just imagine that the ms windows command "format'' were written in C++. Your applet is able to start that program and it formats your entire hard disk. Would you be happy?

    kind regards,

    Jos

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,816
    Rep Power
    25

    Default

    able to pass only the first argument.Am not able to pass the second set of arguments
    Are you talking about arguments to the program? Arguments go on the command line following the command name.
    Or are you talking about responding to console prompts from the program?

    To figure out the logic and calling sequences, can you build a small application and use it to work out the techniques?

    You start the program in a Process and read its "prompt to the console" via an input stream
    You write a response "to the its input console" via an output stream
    Then read the next
    and write the next
    etc
    Last edited by Norm; 07-28-2010 at 11:04 PM.

  6. #6
    jojji is offline Member
    Join Date
    Jul 2010
    Posts
    11
    Rep Power
    0

    Default

    I tried that as follows

    try {

    String s = "C:\\Users\\madsaan\\Documents\\NetBeansProjects\\ CppApplication_2\\dist\\Debug\\Cygwin-Windows\\cppapplication_2.exe";
    ProcessBuilder pb = new ProcessBuilder(s);
    Process process = pb.start();

    OutputStream out = process.getOutputStream();
    PrintWriter pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(out)));
    pw.println(1);

    OutputStream out1 = process.getOutputStream();
    PrintWriter pw1 = new PrintWriter(new BufferedWriter(new OutputStreamWriter(out1)));
    pw1.println(1 + " " + 2);


    pw1.close();

    final InputStream is = process.getInputStream();
    new Thread(new Runnable() {

    public void run() {
    try {
    BufferedReader br = new BufferedReader(new InputStreamReader(is));
    String line;
    while ((line = br.readLine()) != null) {
    System.out.println(line);
    }
    } catch (java.io.IOException e) {
    }
    }
    }).start();

    } catch (IOException d) {
    d.printStackTrace();
    }
    }
    }


    it goes into an infinite loop .please help!.

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,816
    Rep Power
    25

    Default

    Please use code tags when you post your code to preserve formatting.

    it goes into an infinite loop
    What does the program printout while in the loop?

  8. #8
    jojji is offline Member
    Join Date
    Jul 2010
    Posts
    11
    Rep Power
    0

    Default

    am responding to the console prompts

  9. #9
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,816
    Rep Power
    25

    Default

    Can you explain your last post?

  10. #10
    jojji is offline Member
    Join Date
    Jul 2010
    Posts
    11
    Rep Power
    0

    Default

    Hi,i changed the code a little bit.(as follows)

    ProcessBuilder pb = new ProcessBuilder(s);
    Process process = pb.start();

    OutputStream out = process.getOutputStream();
    PrintWriter pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(out)));
    pw.println(1);
    pw.close();
    OutputStream out1 = process.getOutputStream();
    PrintWriter pw1 = new PrintWriter(new BufferedWriter(new OutputStreamWriter(out1)));
    pw1.println(1 + " " + 2);
    pw1.close();

    final InputStream is = process.getInputStream();
    new Thread(new Runnable() {

    public void run() {
    try {
    BufferedReader br = new BufferedReader(new InputStreamReader(is));
    String line;
    while ((line = br.readLine()) != null) {
    System.out.println(line);
    }
    } catch (java.io.IOException e) {
    }
    }
    }).start();



    Now it does not go into an infinite loop but it prints only the following


    1. Find the shortest path from s to t.
    2. Find the associating path from s to t within length l.
    3. Find the associating path from s to t within (1+beta) times of the shortest distance from s to t.
    4. Exit
    Please input the command (1-4): Please input s and t, separated by space:

    So,that means it taking in the first input(pw.println(1)) but after that if i run the program again it prints the same and does not take the next input (pw.println(1 + " " +2)).if it takes the second input then i will get the final output.Thanks for ur help.

  11. #11
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,816
    Rep Power
    25

    Default

    Please use code tags when you post your code to preserve formatting.

  12. #12
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,816
    Rep Power
    25

    Default

    What happens if you don't pre write the answers?
    Read the prompt
    Write an answer
    Read another prompt
    Write another answer
    etc

  13. #13
    jojji is offline Member
    Join Date
    Jul 2010
    Posts
    11
    Rep Power
    0

    Default

    Hi,

    Am not pre writing the answers.I read one prompt,print the input,read the second prompt and print the input.it works for the first prompt but not for the second.I have pasted the formatted code as below

    Java Code:
                   ProcessBuilder pb = new ProcessBuilder(s);
                    Process process = pb.start();
                   OutputStream out = process.getOutputStream();
                    PrintWriter pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(out)));
                    pw.println(1);
                    pw.close();
                    OutputStream out1 = process.getOutputStream();
                    PrintWriter pw1 = new PrintWriter(new BufferedWriter(new OutputStreamWriter(out1)));
                    pw1.println(1 + " " + 2);
                    pw1.close();
                    
    
                     final InputStream is = process.getInputStream();
                    new Thread(new Runnable() {
    
                        public void run() {
                            try {
                                BufferedReader br = new BufferedReader(new InputStreamReader(is));
                                String line;
                                while ((line = br.readLine()) != null) {
                                    System.out.println(line);
                                }
                            } catch (java.io.IOException e) {
                            }
                        }
                    }).start();
                    
                } catch (IOException d) {
                    d.printStackTrace();
                }
            }
        }
    }
    Last edited by jojji; 07-28-2010 at 11:53 PM.

  14. #14
    jojji is offline Member
    Join Date
    Jul 2010
    Posts
    11
    Rep Power
    0

    Default

    sorry.am not sure how to use code tags in this forum.

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

    Default

    Quote Originally Posted by jojji View Post
    sorry.am not sure how to use code tags in this forum.
    The FAQ's or the link in my signature (to the faq's) will show you. You don't have to repost your code but rather should edit your posts above.

  16. #16
    jojji is offline Member
    Join Date
    Jul 2010
    Posts
    11
    Rep Power
    0

    Default

    great.Thanks

  17. #17
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,816
    Rep Power
    25

    Default

    Am not pre writing the answers.
    It looks to me that you are writing the answer BEFORE (pre) reading the prompts
    Java Code:
      
                    pw.println(1);  // prefeed answer 1
                    pw.close();
                    OutputStream out1 = process.getOutputStream();
                    PrintWriter pw1 = new PrintWriter(new BufferedWriter(new OutputStreamWriter(out1)));
                    pw1.println(1 + " " + 2);  // prefeed answer 2

  18. #18
    jojji is offline Member
    Join Date
    Jul 2010
    Posts
    11
    Rep Power
    0

    Default

    I havent clearly understood what outputstream and inputstream exactly do.its very confusing actually.how do u think i should structure the code.please help

  19. #19
    jojji is offline Member
    Join Date
    Jul 2010
    Posts
    11
    Rep Power
    0

    Default

    Hi,

    I changed my code but am still getting the same result as before

    Java Code:
    ProcessBuilder pb = new ProcessBuilder(s);
                    Process process = pb.start();
                    
                    OutputStream out = process.getOutputStream();
                    PrintWriter pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(out)));
                    
    
                    OutputStream out1 = process.getOutputStream();
                   PrintWriter pw1 = new PrintWriter(new BufferedWriter(new OutputStreamWriter(out1)));
                   
                    
    
                     final InputStream is = process.getInputStream();
                    new Thread(new Runnable() {
    
                        public void run() {
                            try {
                                BufferedReader br = new BufferedReader(new InputStreamReader(is));
                                String line;
                                while ((line = br.readLine()) != null) {
                                    System.out.println(line);
                                }
                            } catch (java.io.IOException e) {
                            }
                        }
                    }).start();
                    
                      pw.println(1);
                    pw.close();
    
                     pw1.println(1 + " " + 2);
                    pw1.close();
                    
                } catch (IOException d) {
                    d.printStackTrace();
                }
            }
        }
    }

  20. #20
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,816
    Rep Power
    25

    Default

    What happens if there is only one output stream and it's not closed until all output has been written?

Page 1 of 2 12 LastLast

Similar Threads

  1. how to call Java Bean Ireport frm Java application
    By Prashant.surwade in forum Advanced Java
    Replies: 8
    Last Post: 05-06-2012, 04:39 PM
  2. How to call php script using Java
    By narik in forum New To Java
    Replies: 2
    Last Post: 01-22-2010, 06:08 PM
  3. How to Call a java service
    By omikeneil in forum Advanced Java
    Replies: 3
    Last Post: 10-17-2009, 04:30 AM
  4. how to call dll from java ??
    By Omarero in forum New To Java
    Replies: 3
    Last Post: 11-13-2008, 06:14 AM
  5. how to call a dll from java
    By katie in forum Advanced Java
    Replies: 3
    Last Post: 12-10-2007, 11:34 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
  •