Invoking CURL command from Java code throws Exception - java

I am pretty new to Java and trying to fetch some values from Hashicorp vault -
When I run the curl command manually it works and returns values but when I try to run the same via code, I am running into issues.
curl POST -H "X-Vault-Namespace: dev/rel-box-dev-seed" --data '{"param1":"3f3a-094-193a-cj2e-l6ekn516a","param2":"647a-9f3-934fd3-227e-lkrae24be37"}' https://vault.abc.com/v1/auth/role/login
My Java code is as follows -
import java.io.*;
import java.util.ArrayList;
import java.util.List;
public class JavaRunCommand {
public static void main(String[] args) {
String s = null;
try {
List<String> commands = new ArrayList<String>();
commands.add("curl");
commands.add("POST");
commands.add("-H");
commands.add("\\\"X-Vault-Namespace: dev/rel-box-dev-seed\\\"");
commands.add("--data");
commands.add("'{\"param1\":\"3f3a-094-193a-cj2e-l6ekn516a\",\"param2\":\"647a-9f3-934fd3-227e-lkrae24be37\"}'");
commands.add("https://vault.abc.com/v1/auth/role/login");
ProcessBuilder pb = new ProcessBuilder(commands);
Process p = pb.start();
BufferedReader stdInput = new BufferedReader(new
InputStreamReader(p.getInputStream()));
BufferedReader stdError = new BufferedReader(new
InputStreamReader(p.getErrorStream()));
// read the output from the command
System.out.println("Standard output of the command:\n");
while ((s = stdInput.readLine()) != null) {
System.out.println(s);
}
// read any errors from the attempted command
System.out.println("Standard error of the command (if any):\n");
while ((s = stdError.readLine()) != null) {
System.out.println(s);
}
System.exit(0);
}
catch (IOException e) {
System.out.println("Exception happened - here's what I know: ");
e.printStackTrace();
System.exit(-1);
}
}
}
When I run this code, I am getting the following error -
curl: (92) HTTP/2 stream 1 was not closed cleanly: PROTOCOL_ERROR (err 1)
Does anyone know what I am doing wrong? Or if there is a better way to do curl operation?

My recommendation here is to watch this traffic across the wire with something like Fiddler, Wireshark, or tcpdump. Then, compare these two requests. There is some difference in how you are preparing them (maybe with quotes, as Luke Woodward suggested, or maybe some other difference.) Inspecting the two requests, and comparing them for differences, will help you understand your problem in this instance as well as help you improve request comparison in the future, in general.

Have you tried removing the quotes around the arguments? A ProcessBuilder will quote the arguments for you, so you don't need to quote them again:
List<String> commands = new ArrayList<String>();
commands.add("curl");
commands.add("POST");
commands.add("-H");
commands.add("X-Vault-Namespace: dev/rel-box-dev-seed");
commands.add("--data");
commands.add("{\"param1\":\"3f3a-094-193a-cj2e-l6ekn516a\",\"param2\":\"647a-9f3-934fd3-227e-lkrae24be37\"}");
commands.add("https://vault.app.ford.com/v1/auth/approle/login");

Related

java process builder add path to environment not working

I have a problem with adding a path to the environment of a process using processbuider. I have no clue why the process is ignoring the environment. Here is my example:
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Map;
public class main {
public static void main(String [ ] args) {
try {
String s = null;
ProcessBuilder pb = new ProcessBuilder("/bin/bash", "-c", "fsl");
Map<String, String> env;
env = pb.environment();
env.put("FSLDIR", "/usr/local/fsl/bin/");
Process p = pb.start();
BufferedReader stdInput = new BufferedReader(new
InputStreamReader(p.getInputStream()));
BufferedReader stdError = new BufferedReader(new
InputStreamReader(p.getErrorStream()));
System.out.println("Process p:");
// read the output from the command
while ((s = stdInput.readLine()) != null) {
System.out.println(s);
}
while ((s = stdError.readLine()) != null) {
System.out.println(s);
}
//////////*********\\\\\\\\\\\
ProcessBuilder pb2 = new ProcessBuilder("/usr/local/fsl/bin/fsl");
s = null;
Process p2 = pb2.start();
stdInput = new BufferedReader(new
InputStreamReader(p2.getInputStream()));
stdError = new BufferedReader(new
InputStreamReader(p2.getErrorStream()));
System.out.println("Process p2:");
// read the output from the command
while ((s = stdInput.readLine()) != null) {
System.out.println(s);
}
while ((s = stdError.readLine()) != null) {
System.out.println(s);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
Output:
Process p:
/bin/bash: fsl: command not found
Process p2:
DISPLAY is not set. Please set your DISPLAY environment variable!
And you see FSL wants some more variables to be set. That is why p2 is not an option.
As Jdamian said, Bash searches the directories in the PATH environment variable to find binaries - Bash does not look for your FSLDIR variable, nor will it treat the values of arbitrary variables as executables.
The first thing you should always do when running into issues with a process library (e.g. ProcessBuilder in Java, subprocess in Python), is try to replicate the issue directly in the shell. If you run fsl or /bin/bash -c fsl in your shell you'll likely see the same error (if you don't you're not running your Java binary the same way as your shell) which confirms the issue is not related to Java.
Once you've confirmed that it's just a question of how to fix it. If you intend for fsl to be always available add its containing directory to your PATH in your ~/.bashrc file:
export PATH="$PATH:/usr/local/fsl/bin"
If you just want it available in your Java binary, modify the PATH in-process:
// notice there's no need for `bash -c`
ProcessBuilder pb = new ProcessBuilder("fsl");
pb.environment().put("PATH",
"/usr/local/fsl/bin" + File.pathSeparator + System.getenv("PATH"));
In practice however, your code will often be much more maintainable and easier to work with if you don't modify the PATH and instead simply always invoke external processes by their absolute path, like your second example:
ProcessBuilder pb = new ProcessBuilder("/usr/local/fsl/bin/fsl");
These two commands are (roughly) equivalent, but the latter is far more clear, and less likely to introduce confusing bugs.

How to start Rserve automatically from Java?

I am writing a Java application in IntelliJ IDE. The application used Rserve package to connect to R and perform some functions. When I want to run my code for the first time, I have to launch R in the command line and start the Rserve as a daemon, which looks something like this:
R
library(Rserve)
Rserve()
After doing this, I can easily access all the function in R without any errors. However, since this Java code would be bundled as an executable file, so is there a way that Rserve() is invoked automatically as soon as the code is run so that I have to skip this manual step of starting Rserve using the command line?
Here is the code for the Class I wrote to get Rserve working from Java
public class InvokeRserve {
public static void invoke() {
String s;
try {
// run the Unix ""R CMD RServe --vanilla"" command
// using the Runtime exec method:
Process p = Runtime.getRuntime().exec("R CMD RServe --vanilla");
BufferedReader stdInput = new BufferedReader(new
InputStreamReader(p.getInputStream()));
BufferedReader stdError = new BufferedReader(new
InputStreamReader(p.getErrorStream()));
// read the output from the command
System.out.println("Here is the standard output of the command:\n");
while ((s = stdInput.readLine()) != null) {
System.out.println(s);
}
// read any errors from the attempted command
System.out.println("Here is the standard error of the command (if any):\n");
while ((s = stdError.readLine()) != null) {
System.out.println(s);
}
// System.exit(0);
}
catch (IOException e) {
System.out.println("exception happened - here's what I know: ");
e.printStackTrace();
System.exit(-1);
}
}
}
I know this question has been asked a long back . I think You have the answer. But the below answer may help others. That's why I am posting my answer.
answer:- Instead of going again and again to the R console to start Rserve. One thing you can do is you can write a java program to start Rserve.
Below code you can use in a java program to start Rserve.
https://www.sitepoint.com/community/t/call-linux-command-from-java-application/3751. This is the link where you will get the code to run a linux command from java.I have changed the command only and posting below.
package javaapplication13;
import java.io.*;
public class linux_java {
public static void main(String[] args) {
try {
String command ="R CMD Rserve";
BufferedWriter out = new BufferedWriter(new FileWriter(
new File(
"/home/jayshree/Desktop/testqavhourly.tab"), true));
final Process process = Runtime.getRuntime().exec(command);
BufferedReader buf = new BufferedReader(new InputStreamReader(
process.getInputStream()));
String line;
while ((line = buf.readLine()) != null) {
out.write(line);
out.newLine();
}
buf.close();
out.close();
int returnCode = process.waitFor();
System.out.println("Return code = " + returnCode);
} catch (Exception e) {
e.printStackTrace();
}
}
}

Why can't I get the output of curl command?

I am trying to get the output of curl command in java.
I am able to execute the same curl command manually through terminal and obtain the output but when I try to execute through java code as below, the output obtained is null.
I could use Apache HttpClient for the same, but I want to try and use this curl cli.
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Arrays;
public class MyCurlClient {
public static void main(String[] args) {
MyCurlClient obj = new MyCurlClient();
// in mac oxs
// String command = "ping -c 5 " + domainName;
String command = "curl "
+ "'http://localhost:8080/auth/login' -H 'Origin: http://localhost:9000' --data-binary '{\"username\":\"policy-engine\",\"password\":\"openstack\"}' --compressed";
String output = obj.executeCommand(command);
System.out.println(output);
}
private String executeCommand(String command) {
StringBuffer output = new StringBuffer();
Process p;
try {
p = Runtime.getRuntime().exec(command);
p.waitFor();
BufferedReader reader = new BufferedReader(new InputStreamReader(
p.getInputStream()));
System.out.println(reader.readLine()); // value is NULL
String line = "";
while ((line = reader.readLine()) != null) {
System.out.println(line);
output.append(line + "\n");
}
} catch (Exception e) {
e.printStackTrace();
}
return output.toString();
}
}
process.waitFor() waits until the process is terminated.
Therefore, you won't see any output from it. You need to capture the output before the process terminates.
Causes the current thread to wait, if necessary, until the process represented by this Process object has terminated. This method returns immediately if the subprocess has already terminated. If the subprocess has not yet terminated, the calling thread will be blocked until the subprocess exits.
You can fix your code by removing the process.waitFor() line.
You may want to try using an absolute path to the curl command, e.g.
command = "/usr/bin/curl " + ...
It's possible Java is not able to find the curl binary and that is why it doesn't work.
If this doesn't fix your problem, verify that the curl command is working at all by doing:
String[] command = new String[]{"curl", "http://localhost:8080/auth/login",
"-H", "Origin: http://localhost:9000", "--data-binary",
"{\"username\":\"policy-engine\",\"password\":\"openstack\"}", "--compressed"};
private String executeCommand(String... command) {
ProcessBuilder builder = new ProcessBuilder(command);
builder.redirectOutput(new File("curloutput.txt"));
p.start();
}
And see if the curl command actually outputs anything to the file. If it does, then this is an InputStream timing issue. If it does not, then something is wrong with the curl command itself.
Using the String[] along with removing the single quotes from the curl command solved the issue.
Uf only String[] is used(with single quotes) then you get the expected response but along with it, you also see "unauthorized" as output.
Result is -->
{"username":"policy-engine","token":"dc7e017f-d5a3-4b72-a1c2-066880e775c7"}unauthorized
but when both String[] is used (without single quotes) the response is clean and as expected.
Result is -->
{"username":"policy-engine","token":"dc7e017f-d5a3-4b72-a1c2-066880e775c7"}
Please remove the quotes in th curl command , For Eg -
String command = "curl http://localhost:8080/auth/login -H Origin: shttp://localhost:9000 --data-binary {\"username\":\"policy-engine\",\"password\":\"openstack\"} --compressed";

Java: is there a way to run a system command and print the output during execution?

I have a python script and it takes a long time to finish. I would like to run it from Java, but also output the script's output while it is executing, so that I can tell if it is properly running.
I've searched and only found examples where we output the output after the system command has finished, rather than during its execution.
Any way to do it while the script is running?
Here's what I have
public void doSomething() throws IOException {
String[] callAndArgs = {"python", "/hi.py"};
Process p = Runtime.getRuntime().exec(callAndArgs);
BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));
BufferedReader stdError = new BufferedReader(new InputStreamReader(p.getErrorStream()));
String s;
while ((s = stdInput.readLine()) != null) {
System.out.println(s);
}
while ((s = stdError.readLine()) != null) {
System.out.println(s);
}
}
i managed to get it working like this (Note it requires java7):
package test;
import java.lang.ProcessBuilder.Redirect;
public class Test {
public static void main(String... args) throws Exception {
ProcessBuilder pb = new ProcessBuilder("python","/home/foobar/Programming/test/src/test/test.py");
pb.redirectOutput(Redirect.INHERIT);
Process p = pb.start();
p.waitFor();
}
}
python (note i flush on python to make it work using sys.stdout.flush())
import time,sys
c =0
while c<=50:
time.sleep(1)
print("----")
c = c +1
sys.stdout.flush()
Note if you don't want to flush in a loop you can use this:
ProcessBuilder pb = new ProcessBuilder("python","-u","/home/foobar/Programming/NetBeansProjects/test/src/test/test.py");
Redirect.INHERIT
Indicates that subprocess I/O source or destination will be the same as those of the current process. This is the normal behavior of most operating system command interpreters (shells).
I've searched and only found examples where we output the output after
the system command has finished, rather than during its execution.
That's weird, because your example should be dumping the output as the command is executing.
Instead of using BufferedReader, you could try reading directly from the InputStream instead as the required conditions for readLine might not be being met until after the process exits.
I'd also recommend that you use a ProcessBuilder over Process directly, as, apart from anything else, it allows you to redirect the output from the error stream into the input stream, allowing you to read just one stream instead of two...
This might also be an issue with Python and how it flushes it output buffers...
For example, rather then waiting for the BufferedReader to decide when to return, try printing each character from the stream as it occurs/is reported
ProcessBuilder pb = new ProcessBuilder("test.py");
pb.redirectError();
Process p = pb.start();
InputStream is = null;
try {
is = p.getInputStream();
int in = -1;
while ((in = is.read()) != -1) {
System.out.print((char)in);
}
} finally {
try {
is.close();
} catch (Exception e) {
}
}
Update
Doing a little reading, Python seems to be buffering its out out before sending it to the stdout. I don't think you can fix this on the a Java side, but need to alter either the way Python is run or the script works.
See How to flush output of Python print? for more details
I'm suspecting that you are writing to stderr, which you can't see because you are blocking on stdin. Use a ProcessBuilder instead of doing exec. This way, you can redirect stderr and stdin into a single stream.
Here is an example:
import java.io.*;
public class Test {
public static void main(String... args) throws IOException {
ProcessBuilder pb =
new ProcessBuilder("test.py");
pb.redirectErrorStream(true);
Process proc = pb.start();
Reader reader = new InputStreamReader(proc.getInputStream());
BufferedReader bf = new BufferedReader(reader);
String s;
while ((s = bf.readLine()) != null) {
System.out.println(s);
}
}
}
Alternatively you can spawn threads to read from stdin/stderr respectively.
Another thing to look for is output buffering by python. You can see if this is the cause by doing:
import sys
sys.stdout.flush()
after you write to stdout
Don't use #readLine as the conditional in your while loop. Instead wrap your inputStream in a scanner and use #hasNextLine()
Scanner in = new Scanner(p.getInputStream());
while (in.hasNextLine()) {
System.out.println(in.nextLine());
}

Unable to execute a simple "whoami" unix command using java program on the server

Im facing this strange issue of not being able to execute a simple "whoami" unix command on a AIX server. I have a webapplication that is deployed on an AIX server. Now I want to see under which WAS user my webapplication is currently running. So I added the below code:
public String whoami() throws Exception {
Process p = Runtime.getRuntime().exec("whoami");
BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line;
String output = "";
while ((line = in.readLine()) != null) {
//System.out.println(line);
output += line;
}
in.close();
p.destroy();
return output;
}
}
The above code is added in a jar file which is referred by a JSP. The JSP has to receive the output of the code above and it displays the WAS User name. But when i deploy the webapplication on the server and try to observe the output, im getting an error message like
Error 500: access denied (java.io.FilePermission <> execute)
However, When I remove the above code and run my webapplication, everything runs fine. What wron am i doing here. Did I miss doing anything? Please help. This is the first time im working on UNIX
It looks like your web server has been configured with a Java security policy that prohibits executing external applications.
See http://java.sun.com/developer/onlineTraining/Programming/JDCBook/appA.html for information about Java Security Policies, and the documentation for your web server.
You will need to supply (or edit) a policy file to contain something like:
grant {
permission java.io.FilePermission
"/usr/bin/whoami", "execute";
};
Just out of curiosity
Have you considered to use:
user.name
System property in Java?
AFAIK whoami is a shell command and Runtime#exec() executes programs only.
you can try Runtime.getRuntime().exec(new String[]{"sh","-c","whoami"}) to call sh and let it execute whoami
another thing: do you need to destroy the process after reading?
You can use the ProcessBuilder class instead of getRuntime().exec("whoami").
Here is sample code
import java.io.*;
import java.util.*;
public class DoProcessBuilder {
public static void main(String args[]) throws IOException {
if (args.length <= 0) {
System.err.println("Need command to run");
System.exit(-1);
}
Process process = new ProcessBuilder(args).start();
InputStream is = process.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line;
System.out.printf("Output of running %s is:", Arrays.toString(args));
while ((line = br.readLine()) != null) {
System.out.println(line);
}
}
}

Categories

Resources