且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在java中执行命令行.exe文件

更新时间:2023-11-11 21:42:40

你的问题中有所有的部分。



如下所示应该可以工作:

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

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

也就是说,一个好主意,你应该从某个地方读他们;您的程序的参数,属性文件等。


  1. i want to convert an avi file to 3gp using java program.
  2. For this i am using "E.M. Total Video Converter Command Line 2.43" and the command for it is
    "C:\E.M. TVCC>TVCC -f E:\TestVideo\01.avi -o E:\OutputFiles\target.3gp"
  3. I got a program to execute command line exe file on site http://www.rgagnon.com/javadetails/java-0014.html which is:

Path to executable with spaces in them

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.

public class Test {
  public static void main(String[] args) throws Exception {
    Process p = Runtime.getRuntime().exec(
       "\"c:/program files/windows/notepad.exe\"");
    p.waitFor();
  }
}

If you need to pass arguments, it's safer to a String array especially if they contain spaces.

String[] cmd = { "myProgram.exe", "-o=This is an option" };
Runtime.getRuntime().exec(cmd);

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.

String fileName = "c:\\Applications\\My Documents\\test.doc";
String[] commands = {"cmd", "/c", "start", "\"DummyTitle\"",fileName};
Runtime.getRuntime().exec(commands);

***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:

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

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

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:

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();
    }
}

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.