Problem Description: We have a need to build the table function call shell script by java. There are two ways in which the calling process,
1.Process class, Process ps = Runtime.getRuntime () exec (command);
2.ProcessBuilder:
ProcessBuilder hiveProcessBuilder = new ProcessBuilder(command);
hiveProcessBuilder.start();
Both can be used, in which I am using a back, but there is a problem, the method is called up, and can not generate scripts directly, but later found just restart the service, table setup is completed, the analysis found that it should be transferred from the this process is blocked, and then follow this line of thought found online saying it was very possible, the process should be handled by its own output stdout and stderr, that is, we do not know the results of the implementation of the existing error output (stderr), or an existing standard output (stdout). You can not tell in the end the first output, it may not be able to read the output, but has been blocked. For example: You first deal with the standard output (stdout), but the result of the process is the first error output (stderr), have been waiting for error output (stderr) had been removed, only to the standard output (stdout), thus creating a blockage .
Solution:
- mport java.util.*;
- import java.io. *;
- class StreamGobbler extends Thread
- {
- InputStream is;
- String type;
- StreamGobbler(InputStream is, String type)
- {
- this.is = is;
- this.type = type;
- }
- public void run()
- {
- try
- {
- InputStreamReader isr = new InputStreamReader(is);
- BufferedReader br = new BufferedReader(isr);
- String line=null;
- while ( (line = br.readLine()) != null)
- System.out.println(type + ">" + line);
- } catch (IOException ioe)
- {
- ioe.printStackTrace ();
- }
- }
- }
- public class ExecRunner
- {
- public static void main(String args[])
- {
- if (args.length < 1)
- {
- System.out.println("USAGE: java GoodWindowsExec <cmd>");
- System.exit(1);
- }
- try
- {
- String osName = System.getProperty("os.name" );
- String[] cmd = new String[3];
- if( osName.equals( "Windows NT" ) )
- {
- cmd[0] = "cmd.exe" ;
- cmd[1] = "/C" ;
- cmd[2] = args[0];
- }
- else if( osName.equals( "Windows 95" ) )
- {
- cmd[0] = "command.com" ;
- cmd[1] = "/C" ;
- cmd[2] = args[0];
- } else {
- StringTokenizer st = new StringTokenizer(command, " ");
- cmd = new String[st.countTokens()];
- int token = 0;
- while (st.hasMoreTokens()) {
- String tokenString = st.nextToken();
- // System.out.println(tokenString);
- cmd[token++] = tokenString;
- }
- }
- Runtime rt = Runtime.getRuntime();
- System.out.println("Execing " + cmd[0] + " " + cmd[1]
- + " " + cmd[2]);
- Process proc = rt.exec(cmd);
- // any error message?
- StreamGobbler errorGobbler = new
- StreamGobbler(proc.getErrorStream(), "ERROR");
- // any output?
- StreamGobbler outputGobbler = new
- StreamGobbler(proc.getInputStream(), "OUTPUT");
- // kick them off
- errorGobbler.start();
- outputGobbler.start();
- // any error???
- int exitVal = proc.waitFor();
- System.out.println("ExitValue: " + exitVal);
- } catch (Throwable t)
- {
- t.printStackTrace();
- }
- }
- }
Blocking method referenced address: https://tivan.iteye.com/blog/1045518