I'm trying to periodically send post requests including a pic to a site. It works for the first times but then stucks. What am I doing wrong?
p = r.exec("curl --form api_key=<key> --form api_secret=<secret> "
+ "--form upload=#record.jpg -m 20 "
+ "http://api.face.com/faces/detect.json");
BufferedReader br =
new BufferedReader(new InputStreamReader(p.getInputStream()));
String line = "", text = "";
while ((line = br.readLine()) != null) {
text += line + System.getProperty("line.separator");
}
System.out.println(text);
My guess is it printing an error you cannot see. (I suggest you use ProcessBuilder to redirect error messages to std out) or the process is hanging.
Try flushing and closing the bufferedreader before doing a new request. I never used curl, but could it be that you have to wait until the process is done before running a new one?
Related
I have a requirement, where I will be executing a batch file in java. On completion, the batch script waits for the user input. Then I have to pass two commands, each command followed by the enter key. Please find my code below,
File batchFile = new File("batch file path");
ProcessBuilder builder = new ProcessBuilder();
builder.redirectErrorStream(true);
builder.command(batchFile.getAbsolutePath());
Process process = builder.start();
BufferedReader reader = new BufferedReader(new InputStreamReader(
process.getInputStream()));
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(
process.getOutputStream()));
String s;
loop:
while ((s = reader.readLine()) != null) {
System.out.println("$: " + s);
if (s.contains("last line before user input. so i am breaking here")) {
break loop;
}
}
writer.write("Command1");
writer.newLine();
writer.flush();
writer.write("Command2");
writer.newLine();
writer.flush();
writer.write("/close");
writer.newLine();
writer.flush();
writer.close();
while ((s = reader.readLine()) != null) {
System.out.println("$: " + s);
}
Issue:
Though I am sending input through the bufferedwriter, the process is not getting the input.Can someone give me any pointers to fix this issue. The program runs till the last while loop. When it enters the last while loop statement, it is getting hanged. It looks like the process is still waiting for the user input, so reader.readLine() in the while statement waits infintely. I have been searching for a solution for the whole day, but still no luck. Any help is appreciated.
we used the following codes to merge 2 files with same header and columns:
p = Runtime.getRuntime().exec(new String[]{"bash", "-c", "tail -n +2 " + file1 + " >> " + file2});
p.waitFor();
BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line = "";
while ((line = reader.readLine())!= null) {}
However, we have weird issue that one file content is missing after merging and it doesn't have any exception or failures from the logs, we have hundreds of such merges everyday and the code never have issues before. File size is between 1-2G(we have much bigger files without merge issues) and we don't have space issues when it runs either.
Does anyone have any clue on how this could happen?
I'm running a few shell commands in my android application via exec command.
I want to check what was the result of last command, like if any error has occurred or the command has executed successfully.
e.g.
Process p = Runtime.getRuntime().exec("chmod 755 " + myFile.getAbsolutePath() + "/fileName");
Now chmod does not print any result, so how can I check if the permissions has been modified or not.
If I try to print the output of process object, like:
BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line = "";
StringBuffer output = new StringBuffer();
while ((line = reader.readLine()) != null) {
output.append(line + "\n");
}
Log.e("output: ", output.toString());
Then debugger never goes inside the while loop & nothing is logged.
Please suggest.
Thank You
After processing the outputStream, wait for process exit status and compare with 0 (success).
int retVal = p.waitFor();
if (retVal != 0){
Log.e("Error", "process returned exit status: "+retVal);
}
http://developer.android.com/reference/java/lang/Process.html#waitFor%28%29
I have a function in java which is being executet on my raspberry pi and should send a signal to toggle the targeted sockets state to on / off.
Thats my current function:
public static void rcswitch(int housecode,int unitcode, int onoff) throws InterruptedException, IOException {
String housestring = Integer.toString(housecode);
String unitstring = Integer.toString(unitcode);
String onoffstring = Integer.toString(onoff);
ProcessBuilder builder = new ProcessBuilder("/bin/bash", "-c", "sudo /home/pi/rcswitch-pi/send", housestring, unitstring, onoffstring);
Process proc = builder.start();
BufferedReader reader =
new BufferedReader(new InputStreamReader(proc.getInputStream()));
String line = "";
while((line = reader.readLine()) != null) {
System.out.print(line + "\n");
}
}
However, it doesn't seem like the terminal is receiving the command as it does not output anything. It should show something like "command received" and execute it then. When I normally execute the /send command in the terminal it works just fine. In eclipse it just works fine and throws the expected error.
Thanks for your answers :)
It is most likely that an error has occured while executing the command. Keep in mind that Process#getInputStream() does not include standard error stream of the process. You should use Process#getErrorStream(). Something like:
BufferedReader reader = new BufferedReader(new InputStreamReader(proc.getErrorStream()));
String line = null;
while((line = reader.readLine()) != null) {
System.out.print(line + "\n");
}
I'm trying to run /usr/bin/perl -e 'for(my $i=0;$i<1000;$i++){print 1x1000;print STDERR 2x1000;}' (which works in terminal) with my program.
ProcessBuilder pb = new ProcessBuilder(go); //go is the command
process = pb.start();
BufferedReader incommandbuf = new BufferedReader(new InputStreamReader(process.getInputStream()),1024*1000);
BufferedReader errcommandbuf = new BufferedReader(new InputStreamReader(process.getErrorStream()),1024*1000);
stdString = "";
while ((line = incommandbuf.readLine()) != null)
{
stdString += line + "\n";
}
String errorstrtemp = "";
while ((line = errcommandbuf.readLine()) != null)
{
errorstrtemp += line + "\n";
}
If I try to run this it hangs on while ((line = incommandbuf.readLine()) != null). The program runs if I change the command to /usr/bin/perl -e 'for(my $i=0;$i<64;$i++){print 1x1000;print STDERR 2x1000;}'. If it goes up to 65 and higher it doesn't work. At first I thought I just have to change the size of the my BufferedReaders but it didn't help. Any clue on what is causing this? I will provide any additional info if needed.
Thanks.
You are reading one stream at a time. When the other stream fills up the buffer, your Process will stop waiting for you to read it. The solution is to either read the streams in different threads or use ProcessBuilder.redirectErrorStream