I am Running shell script using cygwin and java.
ProcessBuilder pb =new ProcessBuilder
("sh", "app.sh", "ib2", "12", "11", "AK-RD", "02.20", "D:\\cygwin\\bin\\test\\delta");
My script is running when i am hard coding parameters. I want to pass these parameters through text box values.
How to do this.
String cmmd[] = new String[8];
cmmd[0] ="\"sh\"";
cmmd[1] ="\"app.sh\"";
cmmd[2] ="\""+txt_threeltr.getText()+"\"";
cmmd[3] ="\""+txt_month_c.getText()+"\"";
cmmd[4] ="\""+txt_year_C.getText()+"\"";
cmmd[5] ="\""+txt_partNumber.getText()+"\"";
cmmd[6] ="\""+txt_version.getText()+"\"";
cmmd[7] ="\""+txt_destinationname.getText()+"\"";
ProcessBuilder pb =new ProcessBuilder(Arrays.toString(cmmd));
Or is there any other way to do this.
Since ProcessBuilder has a varargs string constructor, you can do this:
ProcessBuilder pb = new ProcessBuilder(cmmd);
Alternatively, don't construct an array. Create it like this:
ProcessBuilder pb = new ProcessBuilder ("sh",
"app.sh",
txt_threeltr.getText(),
txt_month_c.getText(),
txt_year_C.getText(),
txt_partNumber.getText(),
txt_version.getText(),
txt_destinationname.getText());
The ProcessBuilder has a vargs constructor that you can pass your array to. Pass the values exactly as input from the text boxes (no quotes), and it will take care of any necessary escaping for you.
Related
I want to start a .jar File out of my java program using the Process Builder like this:
ProcessBuilder pb = new ProcessBuilder("java", "-Xdebug", "-DpropKey1=value", "-DpropKey2=value", "MyJar.jar");
Process p = pb.start();
I am looking for a smart way to pass a high amount of the system properties my java program is using to the ProcessBuilder. Right at this moment I am doing it like this:
StringBuilder d1 = new StringBuilder(100);
d1.append("-DpropKey1=");
d1.append(System.getProperty("propKey1"));
String d1Str = d1.toString();
StringBuilder d2 = new StringBuilder(100);
d2.append("-DpropKey2=");
d2.append(System.getProperty("propKey2"));
String d2Str = d2.toString();
ProcessBuilder pb = new ProcessBuilder("java", "-Xdebug", d1Str, d2Str, "MyJar.jar");
Process p = pb.start();
But this way doesn't seem very smart to me. It's just I have a lot of system properties I want to pass out of the java programm (more than 10). It doesn't feel right to use a StringBuilder for every system property I want to pass.
Try this:
String[] params=...
new ProcessBuilder(params).start()
Params is an array of parameters as String.
Old:
Write a method, which does the trick. As argument it could get a Map<String, String>.
The key of an entry is the parameter name, and the value of an entry is the parameter value.
The method builds the String in a loop, which is iterating over the entry set of the map.
Sorry, I read false! This will not work this way with processbuilder!
I have a java code that execute a powershell script.My parameters are in a string array that I got from user.
String sentence = clientinp.readUTF();
String[] parts = sentence.split(",");
How should I put the parameters to the script every time I execute the code?
I tried this code:
String command = "powershell.exe $Add-DnsServerResourceRecordA -ZoneName -Name -IPv4Address -TimeToLive";
But I don't know how can I pass this array to powershell.What should I do?
Use a ProcessBuilder. You have to put each parameter (including path to the program) as items to an array or list and pass it to the constructor of ProcessBuilder.
for example:
String[] arguments = {"powershell.exe", "$Add-DnsServerResourceRecordA", "-ZoneName", "[your zone name]", "-Name", "[your name]", "-IPv4Address", "[your ipv4 address]", "-TimeToLive", "[your TTL]"};
ProcessBuilder processBuilder = new ProcessBuilder(arguments);
Process process = processBuilder.start();
As an alternative you can use Runtime.getRuntime().exec()
I need to run a shell command in Windows:
c:\Python27\python.exe c:\probabilistic_cracker\process.py dic2.txt
which is running fine in a command shell.
In Java I do this:
ProcessBuilder pb = new ProcessBuilder(Arrays.asList("c:\\Python27\\python", " c:\\probabilistic_cracker\\process.py"," dic2.txt"));
Process p = pb.start();
or this
ProcessBuilder pb = new ProcessBuilder("c:\\Python27\\python", " c:\\probabilistic_cracker\\process.py"," dic2.txt");
in both cases the result is
c:\Python27\python: can't open file ' c:\probabilistic_cracker\process.py': [Errno 22] Invalid argument
Your command is built correctly but the way you pass it to ProcessBuilder isn't, as stated in its documentation you pass the args directly the way they are, there's no need to add spaces since the ProcessBuilder will take care of that for you.
ProcessBuilder pb = new ProcessBuilder("c:\\Python27\\python", "c:\\probabilistic_cracker\\process.py","dic2.txt");
So just removing those whitespaces you have in the beginning of each argument string will do the trick.
I am working on a java program, where i need to invoke a bash script that takes a string as an argument. so I've written the code:
Process p = Runtime.getRuntime().exec("./script \"message send\"");
but it seems as if the terminal isn't recognizing the quotes (") as quotes, and referring to the term "message send" as two arguments: "message and send", and so the script is not invoked properly.
anyone have any idea what i can do?
You can perform this by using ProcessBuilder.
ProcessBuilder processBuilder = new ProcessBuilder();
p.command("cmd_to_run", "args_if_any");
p.start();
I'm having difficulty executing a batch file in Java that expects parameters. These parameters may contain spaces so I need to wrap them in quotes. I will also need to do the same thing for Linux because some of the parameters may contain special characters such as !.
Non-functional Windows code:
ProcessBuilder pb = new ProcessBuilder(
"cmd",
"/c",
"\"mybat.bat\"",
"\"param 1\"",
"\"param 2\"",
"\"param 3\""
);
Non-functional Linux code:
ProcessBuilder pb = new ProcessBuilder(
"bash",
"-c",
"'myshellscript.sh'",
"'param 1'",
"'param 2'",
"'param 3'"
);
I understand that I should be adding the parameters like the Windows example below, but this won't work with the spaces:
ProcessBuilder pb = new ProcessBuilder(
"cmd",
"/c",
"mybat.bat param 1 param 2 param 3"
);
How should this be done?
Windows:
ProcessBuilder pb = new ProcessBuilder(
"cmd", "/c", "mybat.bat",
"param 1", "param 2", "param 3");
Unix:
ProcessBuilder pb = new ProcessBuilder(
"sh", "mybat.sh",
"param 1", "param 2", "param 3");
No, you should not quote the args on *nix. Quoting is necessary on *nix in an interactive shell to prevent the shell misinterpreting them, but when launching a process directly a shell isn't involved. Hence no need to quote.
If you do include the quotes, the launched process will see them as part of its incoming arguments and do things like (for example) try to open filenames containing quotes.
You also do not want the "-c" argument to bash. That tells it to parse the next argument as a command line, but you're supplying a list of arguments. Remove the "-c" option and the excess quotes and it should work.
The proper Linux call would be:
ProcessBuilder pb = new ProcessBuilder(
"bash",
"myshellscript.sh",
"param 1",
"param 2",
"param 3"
);
Also not that if the file "myshellscript.sh" is executable and has the appropriate shebang line (e.g. "#!/bin/bash"), you do not need the "bash" argument either. This is preferrable because if the script is ever replaced with one written in a different language you won't have to update your calling app.
Likewise, on Windows, you shouldn't need the "cmd" and "/c" arguments. The process launcher / OS should handle launching the batch file (based on extension) automatically.