I use the following code to get output of terminal command into my java program.
check = CharStreams.toString(new InputStreamReader(java.lang.Runtime.getRuntime().exec(command).getInputStream()));
when command = "ifconfig" this works fine. but when command = "ldapwhoami -D \"cn=admin,dc=example,dc=com\" -w password", the variable check is empty. When i run the same command in the terminal myself i get the desired output. What am i doing wrong here? please help.
You are not escaping special characters in your command string.
If the command needs double quotes, it should be:
command = "ldapwhoami -D \"cn=admin,dc=example,dc=com\" -w password"
Note the \" to insert double quotes inside string.
Also try in this way:
Process p = Runtime.getRuntime().exec(command);
BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line = null;
while ((line = in.readLine()) != null) {
System.out.println(line);
}
Other option is to use java.util.Scanner.
Related
How can I call a shell script through java code?
I have writtent the below code.
I am getting the process exit code as 127.
But it seems my shell script in unix machine is never called.
String scriptName = "/xyz/downloads/Report/Mail.sh";
String[] commands = {scriptName,emailid,subject,body};
Runtime rt = Runtime.getRuntime();
Process process = null;
try{
process = rt.exec(commands);
process.waitFor();
int x = process.exitValue();
System.out.println("exitCode "+x);
}catch(Exception e){
e.printStackTrace();
}
From this post here 127 Return code from $?
You get the error code if a command is not found within the PATH or the script has no +x mode.
You can have the code below to print out the exact output
BufferedReader stdInput = new BufferedReader(new InputStreamReader(process.getInputStream()));
String s= null;
while ((s = stdInput.readLine()) != null) {
System.out.println(s);
}
BufferedReader stdOut = new BufferedReader(new InputStreamReader(process. getErrorStream()));
String s= null;
while ((s = stdOut.readLine()) != null) {
System.out.println(s);
}
If you are getting an exit code, then your script is executed. There is a command that you are running inside of "Mail.sh", which is not succeccfully executed and returning a status code of 127.
There could be some paths that are explicitly set in your shell, but is not available to the script, when executed outside of the shell.
Try this...
Check if you are able to run /xyz/downloads/Report/Mail.sh in a shell terminal. Fix the errors if you have any.
If there are no errors when you run this in a terminal, then try running the command using a shell in your java program.
String[] commands = {"/bin/sh", "-c", scriptName,emailid,subject,body};
(Check #John Muiruri's answer to get the complete output of your command. You can see where exactly your script is failing, if you add those lines of code)
I've had trouble passing parameters with spaces to a shell script with the following JAVA code:
List<String> parameterList = new ArrayList<String>();
parameterList.add(executable);
parameterList.add(inputFile);
parameterList.add(outputPath);
ProcessBuilder pb = new ProcessBuilder(parameterList);
pb.redirectErrorStream();
Process p = pb.start();
BufferedReader reader = new BufferedReader(newInputStreamReader(p.getInputStream()));
#SuppressWarnings("unused")
String line = null;
while ((line = reader.readLine()) != null) {
}
The problem comes when passing my parameter to the 2nd add to the list in the input file variable.
If I use my bat file, I can get the string with spaces, say "my data.xml" using the variable %~1. However, in LINUX, I cannot use %~1 instead there is "$1" which does not accept strings with spaces. Is there a similar approach I can use that will allow me to pass parameters with spaces to a shell script?
EDIT:
In the console the command should go like this:
run.sh "/scratch/input/my data.xml" /scratch/output/
Is there a way to have the above command be called from JAVA verbatim?
Thanks!
I'm running a shell script using:
Runtime.getRuntime().exec(command);
Everything works fine, exept for the output. So, this script
echo "opening gedit..."
gedit
Opens gedit, but when running from Java I don't get any output. What is the problem?
String line;
Process p = Runtime.getRuntime().exec(...);
BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
while ((line = input.readLine()) != null) {
System.out.println(line);
}
input.close();
As mentioned in Printing Runtime exec() OutputStream to console
Execute Command worked well in Terminal, but not in Java code.
String cmd = "find -name javax.jar";
Process process = Runtime.getRuntime().exec(cmd);
BufferedReader reader = new BufferedReader(new InputStreamReader(
process.getInputStream()));
String line = "";
while ((line = reader.readLine()) != null)
{
System.out.println("" + line);
}
System.out.println("Line : "+line);
When you spawn a process with
Runtime.getRuntime().exec(cmd);
the process is started from the same working directory as the Java process. If Java was run from a different working directory than you ran the find -name javax.jar in console, you will see different results.
i think you may try to add the path of find.
like find /var/tmp -name
I am trying to execute originate command in specific directory "/usr/local/freeswitch/bin", In bin I have to run executable file fs_cli by ./fs_cli command, In fs_cli I have to execute following command
originate loopback/1234/default &bridge(sofia/internal/1789)
Its working fine from terminal, The same command can be executed from bin
./fs_cli -x "originate loopback/1234/default &bridge(sofia/internal/1789)"
I tried folowing java program to do the above task
Process pr = Runtime.getRuntime().exec("./fs_cli -x \"originate loopback/1234/default &bridge(sofia/internal/1789#192.168.0.198)\"");
BufferedReader br = new BufferedReader(new InputStreamReader(pr.getInputStream()));
String str = null;
while ((str = br.readLine()) != null) {
System.out.println(str);
}
I have creted symbolic link of fs_cli and placed in current location
The above program is showing following output
Output
-ERR "originate Command not found!
As far as I am concerned whwn above command is working fine with terminal it should be the same from java, So it shows I am wrong somewhere
Please help me to sort out this problem.
Use ProcessBuilder and supply a directory path
ProcessBuilder pb = new ProcessBuilder(
"./fs_cli",
"-x",
"originate loopback/1234/default &bridge(sofia/internal/1789#192.168.0.198)");
pb.directory(new File("..."));
Process pr = pb.start();
BufferedReader br = new BufferedReader(new InputStreamReader(pr.getInputStream()));
String str = null;
while ((str = br.readLine()) != null) {
System.out.println(str);
}
Where possible, you should provide the command arguments as separate Strings, this will pass each as a separate argument to the process and take care of those arguments that need to be escaped by quotes for you (unless it's expecting the quotes, then you should include them anyway)
The other way is:
ProcessBuilder processBuilder = new ProcessBuilder( "/bin/bash", "-c", "cd /usr/local/freeswitch/bin && ./fs_cli -x \"originate loopback/1234/default &bridge(sofia/internal/1789)\"" );
processBuilder.start();