19
  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();
    }
}
Ishan
  • 4,008
  • 32
  • 90
  • 153
  • It's already in Java code - what do you need help with ? – nos Feb 11 '10 at 11:20
  • Thank you for reply.i want to put this "C:\E.M. TVCC>TVCC -f E:\TestVideo\01.avi -o E:\OutputFiles\target.3gp" into the above java code. where and how to put it?? – Ishan Feb 11 '10 at 11:29

2 Answers2

24

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.

Glen
  • 21,816
  • 3
  • 61
  • 76
  • Thank you for reply.I tried what you had suggested,but it is not workking. Is there any syntax problem here?Because in the command there are spaces in between.how do you handle those? – Ishan Feb 11 '10 at 11:39
  • 2
    @user243680, If it's not working you need to tell us how it's not working. What errors are you getting? Does this compile? Does it run properly? Does it give any errors while running? You should edit your original question and add all of this information to it. – Glen Feb 11 '10 at 11:51
  • Sorry for this sir. It compiles and runs without any error.but does not produce any .3gp file.Any idea why is it so? – Ishan Feb 11 '10 at 12:05
  • @user243680 Try escaping the \ character. (replace every \ with \\\) I've updated my answer to show this. – Glen Feb 11 '10 at 12:17
  • @user243680, the code posted above works ok for me. The issue may be with your TVCC executable. Try running it manually and see what happens. Also, edit your answer and copy/paste in the exact java code you're running. – Glen Feb 12 '10 at 10:18
  • Thank you sir.TVCC works fine wen run manually.I have edited my question and posted the exact code i am using. – Ishan Feb 12 '10 at 10:52
3

In some cases you want to be able to do more like: - Kill the exe in case it hung. - Be able to abort the exe. - Get the exe output (to the standard output and the standard error) - Run it asynchronously. You can read on a solution at: http://developer4life.blogspot.co.il/2013/01/executing-command-line-executable-from.html