Skip to main content

APIzation: Replication Package

[Q#2243993][A#2244077] how to execute command line .exe file in java

https://stackoverflow.com/q/2243993

You can include a path for the program to be executed. On the Win plateform, you need to put the path in quotes if the path contains spaces. If you need to pass arguments, it's safer to a String array especially if they contain spaces. If using the start command and the path of the file to be started contains a space then you must specified a title to the start command. ***Can anyone help me to put the above command in this code?***I dont know the syntax rules to put that command in the above code.Please help me. This is the exact java code i am using:

Answer

https://stackoverflow.com/a/2244077

You've got all the pieces in your question. It's just a matter of putting it all together. Something such as the following should work: That said, hard coding paths like this isn't a good idea, you should read them from somewhere; arguments to your program, a properties file, etc.

APIzation

public class Test {
    public static void main(String[] args) throws Exception {

        String[] cmd = { "C:\\E.M. TVCC\\TVCC.exe", "-f E:\\TestVideo\\01.avi", "-o E:\\OutputFiles\\target.3gp" };
        Process p = Runtime.getRuntime().exec(cmd);
        p.waitFor();
    }
}
package com.stackoverflow.api;

public class Human2244077 {

  public static int executeExecCommand(String s) {
    String[] cmd = { s };
    Process p = Runtime.getRuntime().exec(cmd);
    p.waitFor();
  }
}

package com.stackoverflow.api;

/**
 * how to execute command line .exe file in java
 *
 * @author APIzator
 * @see <a href="https://stackoverflow.com/a/2244077">https://stackoverflow.com/a/2244077</a>
 */
public class APIzator2244077 {

  public static void executeLine(String[] cmd) throws Exception {
    Process p = Runtime.getRuntime().exec(cmd);
    p.waitFor();
  }
}