How to Call an External Program with Parameters

How to call an external program with parameters?

When you call CreateProcess(), System(), etc., make sure you double quote your file name strings (including the command program filename) in case your file name(s) and/or the fully qualified path have spaces otherwise the parts of the file name path will be parsed by the command interpreter as separate arguments.

system("\"d:some path\\program.exe\" \"d:\\other path\\file name.ext\"");

For Windows it is recommended to use CreateProcess(). It has messier setup but you have more control on how the processes is launched (as described by Greg Hewgill). For quick and dirty you can also use WinExec().
(system() is portable to UNIX).

When launching batch files you may need to launch with cmd.exe (or command.com).

WinExec("cmd \"d:some path\\program.bat\" \"d:\\other path\\file name.ext\"",SW_SHOW_MINIMIZED);

(or SW_SHOW_NORMAL if you want the command window displayed ).

Windows should find command.com or cmd.exe in the system PATH so in shouldn't need to be fully qualified, but if you want to be certain you can compose the fully qualified filename using CSIDL_SYSTEM (don't simply use C:\Windows\system32\cmd.exe).

How can I execute an external program with parameters in PowerShell?

If you want to run a program, just type its name and parameters:

notepad.exe C:\devmy\hi.txt

If you want to run an exe and redirect stdin to it which your example seems to be an attempt of, use:

Get-Content c:devmy\hi.txt | yourexe.exe 

If you need to specify the full path to the program then you need to use ampersand and quotes otherwise powershell thinks you are defining a plain string:

&"C:\Program Files (x86)\Notepad++\notepad++.exe"

How Can I execute external program with arguments in Java

There is a exec method specifically overloaded for your purposes. It takes as an argument String array which holds the command and the arguments for the command. That seems to be exactly what you need. Please look at the API

How to execute external application with arguments and receive result

I have solved this problem by making a console application in .net and using it with java process-builder.

Using QProcess to call an external program, with arguments

Name of executable would be starting with ".\" if it is in local folder to be system-agnostic. Linux shell and PowerShell require that.

">" - output redirection isn't an argument of process, that won't work. You have to redirect output channel to your or a secondary process.

p.setStandardOutputFile("log_file.txt")

Executing in java code an external program that takes arguments

Perhaps you should use waitFor() to obtain the result code. This means that the dump of the standard output must be done in another thread:

String path;
String[] params = new String [3];

params[0] = "D:\\prog.exe";
params[1] = picA+".jpg";
params[2] = picB+".jpg";

try {
final Process p = Runtime.getRuntime().exec(params);
Thread thread = new Thread() {
public void run() {
String line;
BufferedReader input =
new BufferedReader
(new InputStreamReader(p.getInputStream()));

while ((line = input.readLine()) != null)
System.out.println(line);

input.close();
} catch (IOException e) {System.out.println(" procccess not read"+e);}
};
thread.start();
int result = p.waitFor();
thread.join();
if (result != 0) {
System.out.println("Process failed with status: " + result);
}


Related Topics



Leave a reply



Submit