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
How to Pass a C++ Lambda to a C-Callback That Expects a Function Pointer and a Context
Why Does the Size of a Class Depends on the Order of the Member Declaration? and How
When Do I Use Fabs and When Is It Sufficient to Use Std::Abs
Visual Studio: Link:Fatal Error Lnk1181: Cannot Open Input File
What Does 'Using Std::Swap' Inside the Body of a Class Method Implementation Mean
Using 'Const' in Class's Functions
Name of Process for Active Window in Windows 8/10
Opencv to Use in Memory Buffers or File Pointers
Why Can't I Store References in a 'Std::Map' in C++
C++11: What Happens If You Don't Call Join() for Std::Thread
Opencv Grouprectangles - Getting Grouped and Ungrouped Rectangles
Lambda Expression VS Functor in C++
Use of Typename Keyword with Template Function Parameters
Convert Const Char* to Wstring