I am trying to run two commands through exec() but it seems as if the commands are not correctly parsed.
I have the following code of line:
cmd = "scp -rp /mnt/backups/updateimage/images root#"+Arr.get(i)+":/usr/site/html ; ssh Arr.get(i)+" /usr/site/html/images/untar1.sh";
p = Runtime.getRuntime().exec(cmd);
Any idea how can I format my cmd string so that exec interprets it correctly ?
Thanks
Execution of multiple, semi-colon delimited commands is feature provided by shells, but you are executing the scp command.
If you want to use a shell, you should specify it as the command to be executed, with the actual commands as its arguments.
Related
Running the following Groovy code is giving me error : unmatched '
Process process = "zsh -c 'ls -l'".execute()
However, the following works fine Process process = "zsh -c ls".execute().
How to invoke a zsh command which takes multiple flags?
Never ever use String.execute() - it will split on whitespace and is only sane for very simple commands. The attempt in quoting is in vain in any case, because no shell is used here to parse the string.
Always use List.execute() instead. E.g.
["zsh", "-c", "ls -1"].execute()
I have got this requirement where I need to execute a list of unix commands and then against each command I should store its result.
Eg.
Commands - cd dir1, pwd, ls
My output should contain -
cd dir1-
pwd-dir1
ls-a, b, c, d(assume these are present under dir1 directory)
I tried using JSch library and tried using shell and exec channel types.
But this does not allow me to send a command and read output and then send another and read output.
What I did as of now is
Using exec send all the commands and an echo command after each command and then read till that echo for that command output.
Using shell I sent commands but it waits for an exit command other wise it goes to infinite wait.
Also how can we get the error for individual commands
Please suggest if there is any way to achieve this.
If the commands are independent, open a new "exec" channel for each.
If the later commands depend on previous commands, there's no nice solution.
You have actually already found the best way:
What I did as of now is Using exec send all the commands and an echo command after each command and then read till that echo for that command output.
See also JSch Shell channel execute commands one by one testing result before proceeding.
I want to get rid of a .bat file in java and have the code post directly to CMD.
I have tried multiple variances of the below but i'm not getting it right.
The .bat file contains the following:
CD C:\"Program Files (x86)"\"UiPath Platform"\UIRobot.exe -file C:\ProgramData\UiPath\Projects\DM9\Main.xaml
I would like Java to post this directly to CMD instead.
Currently my code looks like:
String test = in.readUTF();
if (test.equals("Start"))
{
String[] command = {"cmd.exe", "/C", "Start", "C:Unipath\\start.bat"};
Process child = Runtime.getRuntime().exec(command);
}
Any advice?
Thanks in advance.
You haven't actually specified the problem, but I can run a batch file no problem without the cmd.exe argument. i.e. batch file
echo off
echo %1
can be run using
String[] command = {"test.bat", "HELLO"};
Process proc = Runtime.getRuntime().exec(command);
So I suspect your problem lies either with the cmd.exe argument or with the fact that your batch command doesn't seem to be valid
CD C:\"Program Files (x86)"\"UiPath Platform"\UIRobot.exe -file C:\ProgramData\UiPath\Projects\DM9\Main.xaml
Is this a Change Directory command with three arguments, a filename, a -file then another filename. Have you tested the batch file by itself?
Refer to this link for detailed example: Run Dos commands using JAVA
Courtesy of #akshay-pethani's answer in below question.
How do I execute Windows commands in Java?
I m using Linux.
I want to call a small executable application from my java command line which is called "wmic". It needs an input query. Output are stored in text file in the specific directory.
When I use the command in Linux Terminal
echo "Hello World" >> /home/kannan/hello.txt
the output is stored in hello.txt file.
but when i call this command from java
Process p = Runtime.getRuntime().exec("echo \"Hello World\" >> /home/kannan/hello1.txt");
the output is not created any hello1.txt file
Please any one help me.
Thanks in Advance.
Use a ProcessBuilder. It makes it easy to redirect output of a command to file as shown below:
new ProcessBuilder("echo", "hello").redirectOutput(new File("output.txt")).start();
If you want to append to the output file:
new ProcessBuilder("echo", "hello").redirectOutput(Redirect.appendTo(new File("output.txt"))).start();
What you are executing is bash command (echo). Your java program do not work as bash interpreter
To execute any script which requires bash or shell scripting features, your need to execute that interpreter
To solve your problem you can follow below steps
1. Write your string into temp .sh file. Lets call it temp.sh
2. execute below using Runtime.getRuntime().exec
Process p = Runtime.getRuntime().exec("bash temp.sh");
bash will try to execute any command in temp.sh
I have an Stand alone Application which runs Shell Script(with parameters)in Ubuntu.
ProcessBuilder pb1 = new ProcessBuilder("sh","deltapackage_app.sh","part_value","pathtofile");
Process process1 = pb1.start();
I am taking parameter through GUI.
Now same thing i want to implement in web application where i can take inputs form web page and send it to server and then server will execute the shell script with parameters.
Can any one suggest me the best way of doing this. What things should i use to do this.
I Know i have to learn many things about server. Or can i use same code with Browser based Application.
Consider the following line of code:
Process p = Runtime.getRuntime().exec("/bin/sh -c /bin/ls > ls.out");
This is intended to execute a Bourne shell and have the shell execute
the ls command, redirecting the output of ls to the file ls.out. The
reason for using /bin/sh is to get around the problem of having stdout
redirected by the Java internals. Unfortunately, if you try this
nothing will happen. When this command string is passed to the exec()
method it will be broken into an array of Strings with the elements
being "/bin/sh", "-c", "/bin/ls", ">", and "ls.out". This will fail,
as sh expects only a single argument to the "-c" switch. To make this
work try:
String[] cmd = {"/bin/sh", "-c", "/bin/ls > out.dat"};
Process p = Runtime.getRuntime().exec(cmd);
Since the command line is already a series of Strings, the strings
will simply be loaded into the command array by the exec() method and
passed to the new process as is. Thus the shell will see a "-c" and
the command "/bin/ls > ls.out" and execute correctly.
http://www.ensta-paristech.fr/~diam/java/online/io/javazine.html