Pass parameter from array to powershell java program - java

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()

Related

run python script inside Java

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.

Unable to run UNIX command from Java program

I am trying to create a java program that takes some user-input variables and passes them to a perl script (it actually finds a certain string within the perl script and replaces it with the user-input variables). Here is the code:
String sedMain = "sed -e ";
String sedFirstLine = "'s/AAA/"+newFirstLine+"/' -e ";
String sedNewCntr = "'s/BBB/"+newCntr+"/' -e ";
String sedNewSpacing = "'s/SPACE/"+newSpacing+"/' -e ";
String sedNewDmax = "'s/MAX/"+newDmax+"/'";
String sedFile = " /filepath/myperlscript.pl > /filepath/myNEWperlscript.pl";
String sedCommand=sedMain+sedFirstLine+sedNewCntr+sedNewSpacing+sedNewDmax+sedFile;
System.out.println("SED COMMAND: "+sedCommand);
String testRun = "touch /filepath/hello.txt";
Process runSedCommand;
runSedCommand = Runtime.getRuntime().exec(sedCommand);
I am using an IDE, and when the sed command is printed to the console, it looks correct. I copied the sed command from the console and ran it from the terminal, and it worked. I wrote the string "testRun" to see if there was a problem with the Process in Java, and it created the file "hello.txt". For some reason though, my program is not creating the output perl file "myNEWperlscript.pl". I am very confused as to why this is not working. Can anyone help out?
exec() takes a String[] with the program name and paramaters as its elements, but you are concatenating everything together into a single String and so effectively loosing the arguments.
Try something like this:
String[] cmd = {"sed", "first argument", "second argument"};
Runtime.getRuntime().exec(cmd);
use the
exec(String[] cmdarray)
signature.
the command is sed, -e is a parameter, 's/AAA/\n/' is another parameter and so on. So you will have
String[] command = new String[] {"sed", "-e", "s/AAA/\n/", "next parameter without single quotes", , "next parameter without quotes..."}
Runtime.getRuntime.exec(command);
This is the only way your parameters will get well formatted on it's way to the shell, otherwise weird sruff can happen as any quotes after the first token on the string will be considered to be just one parameter and so quotes can be escaped and things like that

java Runtime.exec to run shell script

I am using Runtime.getRuntime().exec() to run a shell script from Java code. The code works fine when I pass the parameter as string
Runtime.getRuntime().exec("sh test.sh")
Since I have to pass additional arguments which are paths with spaces, so I replaced String with String array.
String[] cmd = {"sh test.sh", "/Path/to my/resource file"};
Runtime.getRuntime().exec(cmd)
I also tried with
String[] cmd = {"sh test.sh"};
Runtime.getRuntime().exec(cmd)
But neither of them worked. It's throwing an exception:
java.io.IOException: Cannot run program "sh test.sh":
java.io.IOException: error=2, No such file or directory
Why is the same script file when passed as String worked and when used with String array is throwing exception? How can I make this work with string array as argument to Runtime.exec()?
First string became the command. There is no file 'sh test.sh' to be executed.
Change
String[] cmd = {"sh test.sh", "/Path/to my/resource file"};
to
String[] cmd = {"sh", "test.sh", "/Path/to my/resource file"};
(In general use process builder API)

File copy using getRuntime().exec()

I am trying to copy a file. Here is the source. Note, des is string variable containing the URL.
Process process = Runtime.getRuntime().
exec("cmd.exe\t/c\tcopy\t"+source+"\t"+des);
Can anyone tell me why it does not work?
I think you should use FileUtils.copyFile() but anyways try this.
String[] command = new String[5];
command[0] = "cmd";
command[1] = "/c";
command[2] = "copy";
command[3] = "test.java";
command[4] = "D:";
Process p = Runtime.getRuntime().exec (command);
Instead of passing your command as a single string construct an array and than pass it to exec.
I tried this
String command = "cmd /c copy test.java D:";
worked fine for me.
Advice:
Use ProcessBuilder to construct the Process.
That automatically takes care of '2' - break the command into parts.
Merge the output streams (not entirely necessary, but makes it simpler to ..).
Consume (and display) the output streams.
But in general, read and implement all the recommendations of When Runtime.exec() won't.
Runtime.exec, I believe, send the string to the command processor cmd.exe. So this is running cmd.exe, running another cmd.exe inside it, and passing your arguments. I don't have a Windows machine to test it on (thank Gods) but I think there are arguments to cmd.exe to tell it to run the arguments as a command line.
Why not just use FileUtils.copyFile()?

How to write command in process Builder

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.

Categories

Resources