Running Pig command using JAVA and UNIX - java

I am trying to run a UNIX command through JAVA. My shell command call a pig script.When i do this excersize in unix terminal it works fine, but through java I didn't get any output.
PFB the code JAVA:
public String executeCommand(String command) {
StringBuffer output = new StringBuffer();
Process p;
try {
System.out.println("process started");
p = Runtime.getRuntime().exec(command);
//p.waitFor();
System.out.println("before reader");
BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
//String line = "";
System.out.println("After reader ");
while (reader.readLine() != null) {
System.out.println("inside while loop *******************************");
output.append(reader.readLine() + "\n");
System.out.println(reader.readLine());
}
System.out.println("while loop endedr");
} catch (Exception e) {
e.printStackTrace();
System.out.println("inside exception");
}
return output.toString();
}
pig_command :
time pig -param indir="/home/********/INDIR/archived/Bookings/20160620*/part*" -param outdir="/home/*********/OUTDIR/HOURLYBOOKING/20160620/05" -stop_on_failure -x mapreduce /home/directory/pig/pigscript.pig
How i am running java program :
java -cp /home/directory/java_jar.jar com.Example.Tester pig_command
Problem: though the output is getting generated successfully in HDFS the code in the above while loop prints nothing(which should print the job logs).

each call to readline() goes to next line in stream. while loop in your code is omitting two lines in each readline() call (System.out.println and output.append...) so if output of your pig script is only one or two lines then nothing will be printed on console or appended on StringBuffer (output) from while loop.
To fix this... your implementation of
//String line = "";
System.out.println("After reader ");
while (reader.readLine() != null) {
System.out.println("inside while loop *******************************");
output.append(reader.readLine() + "\n");
System.out.println(reader.readLine());
}
should be:
String line = null;
System.out.println("After reader ");
while ((line = reader.readLine()) != null) {
//System.out.println("inside while loop *******************************");
output.append(line + "\n");
System.out.println(line);
}

Related

How to return value from Python script to Java using ProcessBuilder?

I am trying to get return value from python script into Java using ProcessBuilder. I am expecting the value "This is what I am looking for" in Java. Can anyone point me as to what is wrong in below logic?
I am using python3 and looking to have this done using java standard libraries.
test.py code
import sys
def main33():
return "This is what I am looking for"
if __name__ == '__main__':
globals()[sys.argv[1]]()
Java code
String filePath = "D:\\test\\test.py";
ProcessBuilder pb = new ProcessBuilder().inheritIO().command("python", "-u", filePath, "main33");
Process p = pb.start();
int exitCode = p.waitFor();
BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line = "";
line = in.readLine();
while ((line = in.readLine()) != null){
line = line + line;
}
System.out.println("Process exit value:"+exitCode);
System.out.println("value is : "+line);
in.close();
output
Process exit value:0
value is : null
When you spawn a process from another process, they can only (mostly rather) communicate through their input and output streams. Thus you cannot expect the return value from main33() in python to reach Java, it will end its life within Python runtime environment only. In case you need to send something back to Java process you need to write that to print().
Modified both of your python and java code snippets.
import sys
def main33():
print("This is what I am looking for")
if __name__ == '__main__':
globals()[sys.argv[1]]()
#should be 0 for successful exit
#however just to demostrate that this value will reach Java in exit code
sys.exit(220)
public static void main(String[] args) throws Exception {
String filePath = "D:\\test\\test.py";
ProcessBuilder pb = new ProcessBuilder()
.command("python", "-u", filePath, "main33");
Process p = pb.start();
BufferedReader in = new BufferedReader(
new InputStreamReader(p.getInputStream()));
StringBuilder buffer = new StringBuilder();
String line = null;
while ((line = in.readLine()) != null){
buffer.append(line);
}
int exitCode = p.waitFor();
System.out.println("Value is: "+buffer.toString());
System.out.println("Process exit value:"+exitCode);
in.close();
}
You're overusing the variable line. It can't be both the current line of output and all the lines seen so far. Add a second variable to keep track of the accumulated output.
String line;
StringBuilder output = new StringBuilder();
while ((line = in.readLine()) != null) {
output.append(line);
.append('\n');
}
System.out.println("value is : " + output);

communicating with the process while executing a shell command with java

I'm going to execute a shell command from java and i need to pass arguments to the output stream while executing the command..
following is the shell command
./darknet detect cfg/yolo-voc.2.0.cfg backup/yolo-voc_20000.weights
when executing this command it is yielding for the path of the image file in the terminal i can provide the path of the image as follows
Loading weights from backup/yolo-voc_21000.weights...Done!
Enter Image Path:
when executing from the terminal i can provide the path there.
I managed to execute this command withing the java process and also i can get an output when i provide an image uri with the command. here is the code
public static void execCommand(String command) {
try {
Process proc = Runtime.getRuntime().exec(command);
// Read the output
BufferedReader reader = new BufferedReader(new InputStreamReader(proc.getInputStream()));
String line = "";
//reader.readLine();
while ((line = reader.readLine()) != null) {
System.out.print(line + "\n");
s.add(line);
}
// proc.waitFor();
} catch (IOException e) {
System.out.println("exception thrown: " + e.getMessage());
}
}
but what I want is provide the image path at the runtime not beginning of the execution of the command..
tried with writing to the output stream as below still no luck
public static void execCommand(String command) {
try {
Process proc = Runtime.getRuntime().exec(command);
// Read the output
BufferedReader reader = new BufferedReader(new InputStreamReader(proc.getInputStream()));
String line = "";
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(proc.getOutputStream()));
writer.append("data/test2.jpg");
writer.newLine();
//reader.readLine();
while ((line = reader.readLine()) != null) {
System.out.print(line + "\n");
s.add(line);
}
// proc.waitFor();
} catch (IOException e) {
System.out.println("exception thrown: " + e.getMessage());
}
}
You need to call writer.flush() in order to actually output something to the underlining InputStream
Therefore your code should look like:
public static void execCommand(String command) {
try {
Process proc = Runtime.getRuntime().exec(command);
// Read the output
BufferedReader reader = new BufferedReader(new
InputStreamReader(proc.getInputStream()));
String line = "";
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(proc.getOutputStream()));
writer.append("data/test2.jpg");
writer.newLine();
// **** add flush here ****
writer.flush();
// and remember to close your resource too
writer.close();
//reader.readLine();
while ((line = reader.readLine()) != null) {
System.out.print(line + "\n");
s.add(line);
}
// ***** close your reader also ****
reader.close();
// proc.waitFor();
} catch (IOException e) {
System.out.println("exception thrown: " + e.getMessage());
}
}

ProcessBuilder not executing command with wildcard [duplicate]

This question already has answers here:
Can't run program with ProcessBuilder, runs fine from command line
(2 answers)
Closed 5 years ago.
I am trying to write an utility java program that calls a perl script in unix box and shows the output of the script. The issue is when I am executing command2,
String[] command2 = {"/archive/scripts/grep.pl", "/apps/ws/logs/api.log"};
the output is coming correctly. But when i am using command1,
String[] command1 = {"/archive/scripts/grep.pl", "/apps/ws/logs/*"};
i am getting the below exception:
Can't open /apps/ws/logs/*: No such file or directory at /archive/scripts/grep.pl line 160.
Below is the full code for your reference:
StringBuffer output = new StringBuffer();
try {
ProcessBuilder processBuilder = new ProcessBuilder(command1);
LOGGER.debug("Command: "+processBuilder.command());
Process process = processBuilder.start();
process.waitFor();
BufferedReader br;
if (process.exitValue() == 0) {
LOGGER.debug("Inside if block. Exit value: "+process.exitValue());
String line;
br = new BufferedReader(new InputStreamReader(process.getErrorStream()));
while ((line = br.readLine()) != null) {
output.append(line + "\n");
}
} else {
LOGGER.debug("Inside Else block. Exit value: "+process.exitValue());
br = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line;
while ((line = br.readLine()) != null) {
output.append(line + "\n");
}
}
} catch (Exception e) {
LOGGER.debug("Exception thrown"+e.getStackTrace());
output.append(e.getStackTrace().toString());
e.printStackTrace();
}
return output.toString();
I am not able to understand what is the issue. Is there any way to accomplish this.
Use a command shell to interpret the wildcard
String[] command1 = {"bash", "-c", "/archive/scripts/grep.pl /apps/ws/logs/*"};

BufferedReader not working as expected

The below code is not getting executed completely after this line " bufferedReader.readLine(); ". The Program works fine when i execute the system command with
out mentioning IPAddress of the remote PC.
class Test
{
public static void main(String arg[])
{
Process p;
Runtime runTime;
String process = null;
try {
runTime = Runtime.getRuntime();
p = runTime.exec("sc \\xx.xx.xx.xx query gpsvc"); // For Windows
InputStream inputStream = p.getInputStream();
InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
String line = bufferedReader.readLine();
process = "&";
while (line != null) {
line = bufferedReader.readLine();
process += line + "&";
}
StringTokenizer st = new StringTokenizer(proc, "&");
System.out.println("token size "+st.countTokens());
while (st.hasMoreTokens()) {
String testData = st.nextToken();
bufferedReader.close();
inputStreamReader.close();
inputStream.close();
}
} catch (IOException e) {
System.out.println("Exception arise during the read Processes");
e.printStackTrace();
}
}
}
Check your command inside exec method
p = runTime.exec("sc \\xx.xx.xx.xx query gpsvc");
The syntax is wrong here and if you execute this from command prompt, you will be prompted with the below question.
Would you like to see help for the QUERY and QUERYEX commands? [ y | n ]:
And the program wouldn't return until you enter y or n. Since the program is not terminating, you wouldn't be able to read the console output and that's the reason your program is getting stuck on String line = bufferedReader.readLine();

Printing multiple lines returned by BufferedReader

I'm writing a program that basically sends linux command through java and then prints back the output. It works fine if the output is one line only but for multiple lines output I can't figure out what I'm doing wrong. For example to check the memory usage I use the "free" command but it only returns lines 1 and 3. Here is my code:
if (clinetChoice.equals("3"))
{
String command = "free";
Process process = Runtime.getRuntime().exec(command);
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
System.out.println("You Chose Option Three");
String line;
while ((line = reader.readLine()) != null)
{
output += line;
System.out.println(line);
line = reader.readLine();
}
}
When I run this it only returns:
total used free share buffers cached
-/+ buffers/cache: 6546546 65464645
Client Code:
while ((fromServer = input.readLine()) != null)
{
System.out.println("Server: " + fromServer);
if (fromServer.equals("Bye"))
break;
System.out.print("Enter your choice: ");
fromClient = stdIn.readLine().trim();
if(fromClient.equals("1"))
{
System.out.println("Client: " + fromClient);
output.println(fromClient);
}
if(fromClient.equals("2"))
{
System.out.println("Client: " + fromClient);
output.println(fromClient);
}
if(fromClient.equals("3"))
{
System.out.println("Client: " + fromClient);
output.println(fromClient);
}
if(fromClient.equals("4"))
{
System.out.println("Client: " + fromClient);
output.println(fromClient);
break;
}
}
You're calling readLine in both your loop test and the body of the loop. So for every iteration of the loop, readLine is called twice, and one of the results is discarded: it isn't printed or added to output. This matches the results that you describe.
This loop should be sufficient:
while ((line = reader.readLine()) != null)
{
output += line + System.getProperty("line.separator");
System.out.println(line);
}
If you're just trying to print the entire output once, and since you're collecting the output in your output variable, you can move the println out of the loop:
while ((line = reader.readLine()) != null)
{
output += line + System.getProperty("line.separator");
}
System.out.println(output);
Simply use this... You are calling the readLine() twice....
while ((line = reader.readLine()) != null)
{
System.out.println(line);
}
If you want to assign the data to output varible..then do this inside the while loop..
output = output + line;
I should point out that in addition to the comments re. using readline() twice, you should strictly consume stdout/stderr simultaneously. Otherwise you run the risk of blocking the process output since you're not consuming it. See this SO answer for more info.

Categories