If I run the following in a terminal I get the expected 123
$ /bin/sh
$ FOO=123
$ echo $FOO
123
Now I try to do the following with Java's Runtime's exec():
String[] envp = { "FOO=123" };
String cmd = "echo $FOO";
Process p = Runtime.getRuntime().exec(cmd, envp);
java.io.BufferedReader reader =
new java.io.BufferedReader(
new java.io.InputStreamReader(p.getInputStream()
)
);
System.out.println(reader.readLine());
I expect to see 123 but instead I get $FOO.
What am I missing?
The following works under Windows.
String[] envp = { "FOO=123" };
String cmd = "cmd /c echo %FOO%";
Process p = Runtime.getRuntime().exec(cmd, envp);
p.waitFor();
java.io.BufferedReader reader =
new java.io.BufferedReader(
new java.io.InputStreamReader(p.getInputStream()
)
);
String line;
while((line = reader.readLine()) != null) {
System.out.println(line);
}
What am I missing?
Firstly,
$ FOO=123
sets a shell variable. It is local to the shell. If you want a variable to be in the environment that child processes see, you must export it.
$ export FOO=123
But that's not the problem.
The real issue is the command string "echo $FOO". The problem is that $FOO is shell syntax .... but when you run a command from Java using exec:
exec itself doesn't understand shell syntax, and
exec doesn't run the command in a shell.
So the parameter that is given to the echo command consists of the literal string $FOO ... and that is what it outputs.
There are three approaches to solving this:
Interpolate the variable in Java; e.g.
String[] cmd = {"echo", "123"};
Process p = Runtime.getRuntime().exec(cmd);
or by doing repeated search / replace for things that look like variables. (But this only deals with environment variable interpolation, not other forms of shell substitution.)
Assuming that you are doing something more complicated than echo, write the command that you are running to do its own interpolation of environment variables. (This is clunky ...)
Explicitly invoke the command in a shell; e.g.
String[] envp = {"FOO=123"};
String[] cmd = {"/bin/sh", "-c", "echo $FOO"};
Process p = Runtime.getRuntime().exec(cmd, envp);
Note that you can use any shell, and provide pretty much any shell-ism that can be expressed in a single shell command line.
Related
I'm trying to get the output of grep linux shell command in java by using process builder. But i got a stuck in this case. Please help me.
Thank in advice!
String[] args = new String[7];
args[0] = "/bin/bash";
args[1] = "-c";
args[2] = "grep";
args[3] = "-n";
args[4] = "-e";
args[5] = "KERNELVERSION";
args[6] = kernelFilePath.trim();
ProcessBuilder pb;
Process process = null;
try {
pb = new ProcessBuilder(args);
pb = pb.directory(new File(directory));
pb.inheritIO();
pb.redirectOutput(ProcessBuilder.Redirect.INHERIT);
pb.redirectError(ProcessBuilder.Redirect.INHERIT);
process = pb.start();
process.waitFor();
} catch (IOException | InterruptedException e) {
System.out.println("executeCmdWithOutput() exception : " + e.toString());
} finally {
if (process != null) {
process.destroy();
}
}
==> Error:
Usage: grep [OPTION]... PATTERN [FILE]...
Try 'grep --help' for more information.
I tried the command in bash and it worked fine:
grep -n -e KERNELVERSION ..../Makefile
Have you tried change the args[2] as full command?
Also, you can use pgrep, it does not require you to use pipe.
You don't need to explicitly run /bin/bash in order to execute the grep process. Just call it directly and ProcessBuilder will run it:
String[] args = {"grep", "-n", "KERNELVERSION", kernelFilePath.trim()};
Also, you don't need to use the -e option, unless there are multiple patterns that you are searching for.
If you really wanted to run grep in /bin/bash:
String[] args = {"/bin/bash", "-c", "grep -n KERNELVERSION " + kernelFilePath.trim()};
passes a single argument to bash containing the full command and arguments to execute.
I need to start a server using bash, so I had created an UNIX shell , but I am not able to execute it with Java from Eclipse.
I tried the following code which doesn't work :
Process proc = Runtime.getRuntime().exec(./startServer);
Here is content of the startServer file :
#!/bin/bash
cd /Users/sujitsoni/Documents/bet/client
npm start
You can try the following two options.
Option 1
Process proc = Runtime.getRuntime().exec("/bin/bash", "-c", "<Abosulte Path>/startServer");
Option 2
ProcessBuilder pb = new ProcessBuilder("/bin/bash", "-c", "<Absolute Path>/startServer");
pb.directory(new File("<Absolute Path>"));
Process proc = pb.start();
A couple Of things can go wrong:
The path to the file you have given might be wrong for eclipse it can take relative path but from the command line, it will take the absolute path.
error=13, Permission denied - If the script file doesn't have required permissions. In your scenario, that might not the case as you are not getting any error.
At last, you are executing the script by java program so the output of your script will not be printed out. In your scenario, this might be the case. You need to capture the output of script from BufferedReade and print it. ( In your case server might have started but you are not seeing the logs/output of the script.
See the code sample below for printing output.
public static void main(String[] args) throws IOException, InterruptedException {
Process proc = Runtime.getRuntime().exec("./startServer");
proc.waitFor();
StringBuffer output = new StringBuffer();
BufferedReader reader = new BufferedReader(new InputStreamReader(proc.getInputStream()));
String line = "";
while ((line = reader.readLine()) != null) {
output.append(line + "\n");
}
System.out.println(output);
}
When I run a bach script containing "echo $Path" command what it outputs when run by java is different from what it outputs when run from command line. It also affects other commands of my script. Why is this happening and how do I avoid?
Following is my function to run a bashscript
public static String executeCommands(File tempScript, Boolean deleteFile)
throws IOException, InterruptedException {
StringBuffer output = new StringBuffer();
try {
ProcessBuilder pb = new ProcessBuilder("bash", tempScript.toString());
pb.inheritIO();
Process process = pb.start();
process.waitFor();
BufferedReader reader =
new BufferedReader(new InputStreamReader(process.getInputStream()));
String line = "";
while ((line = reader.readLine()) != null) {
output.append(line + "\n");
}
return line;
} finally {
if (deleteFile == true)
tempScript.delete();
}
}
when the script contains "echo $PATH" in bashscript
output is
/usr/bin:/bin:/usr/sbin:/sbin
But when I run from commandline output is
/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/scala/scala-2.11.8/bin:/Users/<user>/Installations/activator-dist-1.3.10
When we run the command from terminal it reads the environment variables from .bashrc file, but it seems eclipse does not read environment variables from .bashrc.
launch eclipse with ./eclipse -DPATH=$PATH to read from bashrc
PATH variable from
1.terminal
user#ubuntu:~$ javac SS47.java
user#ubuntu:~$ java SS47
/home/user/perl5/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/home/user/apache-maven-3.3.3/bin:/home/user/apache-maven-3.3.3/bin:/opt/jdk/jdk1.8.0_60/bin:/opt/jdk/jdk1.8.0_60/jre/bin:/home/user/dsc-cassandra-2.1.6/bin:/home/user/hadoop-2.6.0/bin:/home/user/hadoop-2.6.0/sbin:/home/user/android/android-studio/bin:/home/user/android/android-sdk-linux/platform-tools:/home/user/elasticsearch-2.3.5/bin:/home/user/scala-2.11.8/bin::/home/user/apache-maven-3.3.3/bin
2.eclipse with out $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games
3.eclipse with PATH ./eclipse -DPATH=$PATH
/home/user/perl5/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/home/user/apache-maven-3.3.3/bin:/home/user/apache-maven-3.3.3/bin:/opt/jdk/jdk1.8.0_60/bin:/opt/jdk/jdk1.8.0_60/jre/bin:/home/user/dsc-cassandra-2.1.6/bin:/home/user/hadoop-2.6.0/bin:/home/user/hadoop-2.6.0/sbin:/home/user/android/android-studio/bin:/home/user/android/android-sdk-linux/platform-tools:/home/user/elasticsearch-2.3.5/bin:/home/user/scala-2.11.8/bin::/home/user/apache-maven-3.3.3/bin
As Elliott-Frisch pointed out in his comment, just call bash with the option -l which
Make[s] bash act as if it had been invoked as a login shell (see INVOCATION below). Source
This way you don't have to call Eclipse with different launch options etc. and it makes your solution independent, being able to be run without the non customized Eclipse or just bare-bone JRE.
So just edit your call ProcessBuilder like this:
ProcessBuilder pb = new ProcessBuilder("bash", "-l", tempScript.toString());
I am writing a Java program from which I am executing a shell script. The shell script is calling another shell script. I am trying to print output returned by child script to parent shell script.
Below is my code.
public class App
{
public static void main( String[] args ) throws IOException, InterruptedException
{
String command = new String("/home/Test.sh");
Runtime rt = Runtime.getRuntime();
Process process = rt.exec(command);
process.waitFor(1, TimeUnit.MINUTES);
BufferedReader reader = new BufferedReader(new InputStreamReader(
process.getInputStream()));
String s;
while ((s = reader.readLine()) != null) {
System.out.println("Script output: " + s);
}
Shell Script: Test.sh
#!/bin/bash
result=$( bash myscript.sh )
echo "$result"
myscript.sh
#!/bin/bash
echo "50"
I am getting empty output. I initial thought it might be because the Java process is not waiting for the shell script to finish. So added waitFor() but still no use. Can some one kindly help.
try this; this is not waiting problem.
#!/bin/bash
result=$( bash /home/myscript.sh ) # full path of myscript
echo "$result"
Also handle bash error as below;
public static void main(String[] args) throws IOException {
String command = new String("/tmp/1/Test.sh");
Runtime rt = Runtime.getRuntime();
Process process = rt.exec(command);
BufferedReader reader = new BufferedReader(new InputStreamReader(
process.getInputStream()));
String s;
while ((s = reader.readLine()) != null) {
System.out.println("Script output: " + s);
}
BufferedReader stdError = new BufferedReader(new
InputStreamReader(process.getErrorStream()));
System.out.println("Here is the standard error\n");
while ((s = stdError.readLine()) != null) {
System.out.println(s);
}
}
adduser tests;
1-
myscript.sh:
#!/bin/bash
adduser test
echo "50"
java console output;
Script output: 50
Here is the standard error of the command (if any):
adduser: Only root may add a user or group to the system.
2-
myscript.sh:
#!/bin/bash
sudo adduser test
echo "50"
java console output;
Script output: 50
Here is the standard error of the command (if any):
sudo: no tty present and no askpass program specified
3-
myscript.sh:
#!/bin/bash
echo -e "YOURPASSWORD=\n" | sudo -S adduser -shell /bin/bash --home /home/test test
echo -e "YOURPASSWORD\n" | sudo deluser --remove-home test
echo "OK"
java console output;
Script output: Adding user `test' ...
Script output: Adding new group `test' (1002) ...
Script output: Adding new user `test' (1001) with group `test' ...
Script output: Creating home directory `/home/test' ...
Script output: Copying files from `/etc/skel' ...
Script output: Try again? [y/N] Changing the user information for test
Script output: Enter the new value, or press ENTER for the default
Script output: Full Name []: Room Number []: Work Phone []: Home Phone []: Other []: Is the information correct? [Y/n] Looking for files to backup/remove ...
Script output: Removing files ...
Script output: Removing user `test' ...
Script output: Warning: group `test' has no more members.
Script output: Done.
Script output: OK
Here is the standard error of the command (if any):
[sudo] password for ..: Enter new UNIX password: Retype new UNIX password: passwd: Authentication token manipulation error
passwd: password unchanged
Use of uninitialized value $answer in chop at /usr/sbin/adduser line 563.
Use of uninitialized value $answer in pattern match (m//) at /usr/sbin/adduser line 564.
Use of uninitialized value $answer in chop at /usr/sbin/adduser line 589.
Use of uninitialized value $answer in pattern match (m//) at /usr/sbin/adduser line 590.
I have the following java code
ArrayList<String> argList = new ArrayList<>();
argList.add("Hello");
argList.add("World");
String[] args = argList.toArray(new String[argList.size()]);
Process p =Runtime.getRuntime().exec("echo '$1 $2' ", args);
result is $1 $2 but i want to print Hello World.
Can anybody help me?
Create a shell to use the parameter expansion:
ArrayList<String> command = new ArrayList<>();
command.add("bash");
command.add("-c");
command.add("echo \"$0\" \"$1\"");
command.addAll(argList);
Process p = Runtime.getRuntime().exec(command.toArray(new String[1]));
Output:
Hello World
You should use the exec(String[] args) method, instead:
String[] cmdArgs = { "echo", "Hello", "World!" };
Process process = Runtime.getRuntime().exec(cmdArgs);
BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line = null;
while ((line = in.readLine()) != null) {
System.out.println(line);
}
The problem is, that the first argument in the exec() method is not the script, but the name of the script.
If you want to use variables, like $1 and $2 you should do that in your script.
So, what you actually might want is:
String[] cmdArgs = { "myscript", "Hello", "World!" };
Process process = Runtime.getRuntime().exec(cmdArgs);
ArrayList<String> argList = new ArrayList<>();
argList.add("echo");
argList.add("Hello");
argList.add("World");
Process p =Runtime.getRuntime().exec(args);
This way the String[] will be passed as an argument to echo.
If you want to use $ then you will have to write a shell script.
Echo will print all arguments as such. In your case '$1 $2' is interpreted as normal string.. Since it will anyway print all args you could use some thing like below.
ProcessBuilder pb= new ProcessBuilder().command("/bin/echo.exe", "hello", "world\n");
Another option is co create a small script say mycommands.sh with appropriate contents
echo $#
echo $1 $2
#any such
You then invoke your script... like
ProcessBuilder pb= new ProcessBuilder().command("/bin/bash" , "-c", "<path to script > ", "hello", "world\n");
Note the use of ProcessBuilder. This is an improved api instead of Runtime.(especially for quoting etc)