Bhavesh Shah
Bhavesh Shah

Reputation: 3389

My Java Program hangs after calling a shell command

I have written a small program to start to Hive Server. Command to start to Hive Server is in shell file. When I call the shell file to start Hive Server it tends to start it and get Hang. Is there any problem in program?

Code:

            try
        {
            String cmd = "/home/hadoop/sqoop-1.3.0-cdh3u1/bin/StartServer.sh"; // this is the command to execute in the Unix shell

            // create a process for the shell
            ProcessBuilder pb = new ProcessBuilder("bash", "-c", cmd);
            pb.redirectErrorStream(true); // use this to capture messages sent to stderr
            Process shell = pb.start();
            InputStream shellIn = shell.getInputStream(); // this captures the output from the command
            // wait for the shell to finish and get the return code
            // at this point you can process the output issued by the command

            // for instance, this reads the output and writes it to System.out:
            int c;
            while ((c = shellIn.read()) != -1) 
            {
                System.out.write(c);
            }

            // close the stream
            shellIn.close();
        }
        catch(Exception e)
        {
            e.printStackTrace();
            e.printStackTrace(pw);
            pw.flush();
            System.exit(1);
        }

Please let me know this. Is something I missed in the program?

Thanks.

Upvotes: 0

Views: 1624

Answers (2)

Andrzej Doyle
Andrzej Doyle

Reputation: 103777

It looks like your program is doing what you've told it to do.

The first few lines should indeed start the Hive server. The lines after that, read from the standard output of the server process, and echo each character to your Java process' console. Your Java process sits in a loop (making blocking I/O calls) for as long as the Hive server's output stream exists.

That is, your Java process will sit in a loop, echoing output, for as long as the Hive server is running.

Is this what you want it to do? If so, then it obviously won't be able to exit. If not, then there's no reason for you to read from the server's input stream at all, and your Java program can exit after it has started the server process. Alternatively, if you want to listen for output and do other things in your Java process, you'll need to use multiple threads in order to do two things at once.

Upvotes: 2

AlexR
AlexR

Reputation: 115328

As far as I can see you are starting server, i.e. application that starts and does not terminates soon. It remains running. This means that the STDOUT of this applcation (the server) is not closed (unless you are killing the server).

Method shellIn.read() is blocking. It reads the next byte from input stream and returns when the byte is read or when stream is closed.

So, in your case the stream is never closed, therefore your program got stuck: it is waiting forever for the input (and probably reading it).

To solve this problem you need separate thread: either in java or in OS. You can run your server from separate java thread or compose command line to run server in background (for example using trailing &).

Upvotes: 1

Related Questions