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();
}
}
Type 'javac MyFirstJavaProgram. java' and press enter to compile your code. If there are no errors in your code, the command prompt will take you to the next line (Assumption: The path variable is set). Now, type ' java MyFirstJavaProgram ' to run your program.
Type "start [filename.exe]" into Command Prompt, replacing "filename" with the name of your selected file. Replace "[filename.exe]" with your program's name. This allows you to run your program from the file path.
Launch4j is a cross-platform tool for wrapping Java applications distributed as jars in lightweight Windows native executable files.
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With