cmd in java

Discussion in 'Programming' started by aguswgs, May 2, 2011.

  1. #1
    My problem is :

    I execute an exe file from java using the runtime.getRuntime() ,

    But my problem is that the exe is running in the background , and I want when I run my program it opens that exe so I can see what's happening

    anyone got any ideas?

    thanks in advance
     
    aguswgs, May 2, 2011 IP
  2. om39a

    om39a Peon

    Messages:
    287
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #2
    I cant understand your question. Can you be little more specific? or PM me !!


    You can run .exe files using this piece of code

    Runtime rt = Runtime.getRuntime();
    try {
    rt.exec(Path:\\TheFile.exe);
    } catch (IOException e) {
    e.printStackTrace();
    }
    
    HTML:
     
    om39a, May 2, 2011 IP
  3. jkl6

    jkl6 Peon

    Messages:
    70
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    You can't actually see the program running but you can capture any System.out's that it may have.

    
    Runtime systemShell = Runtime.getRuntime();
    Process output = systemShell.exec("<PATH TO EXE>");
    
    // open reader to get output from process
    BufferedReader br = new BufferedReader (new InputStreamReader(output.getInputStream()));
    String line = null;
    
    while((line = br.readLine()) != null ) {
    [INDENT]if(line.length()!=0)[/INDENT]
    [INDENT][INDENT]System.out.println(line);	// display process output  [/INDENT][/INDENT]
    }          
    
    int exitVal = output.waitFor();             // get process exit value
    if(exitVal==0) 
    [INDENT]System.out.println("GOOD STATUS");[/INDENT]
    else 
    [INDENT]System.out.println("BAD STATUS");[/INDENT]
    
    
    
    Code (markup):
     
    jkl6, May 3, 2011 IP