About Process call the shell script process to block construction of the table

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:

  1. mport java.util.*;  
  2. import java.io. *;  
  3. class StreamGobbler extends Thread  
  4. {  
  5.     InputStream is;  
  6.     String type;  
  7.       
  8.     StreamGobbler(InputStream is, String type)  
  9.     {  
  10.         this.is = is;  
  11.         this.type = type;  
  12.     }  
  13.       
  14.     public void run()  
  15.     {  
  16.         try  
  17.         {  
  18.             InputStreamReader isr = new InputStreamReader(is);  
  19.             BufferedReader br = new BufferedReader(isr);  
  20.             String line=null;  
  21.             while ( (line = br.readLine()) != null)  
  22.                 System.out.println(type + ">" + line);      
  23.             } catch (IOException ioe)  
  24.               {  
  25.                 ioe.printStackTrace ();    
  26.               }  
  27.     }  
  28. }  
  1. public class ExecRunner  
  2. {  
  3.     public static void main(String args[])  
  4.     {  
  5.         if (args.length < 1)  
  6.         {  
  7.             System.out.println("USAGE: java GoodWindowsExec <cmd>");  
  8.             System.exit(1);  
  9.         }  
  10.           
  11.         try  
  12.         {              
  13.             String osName = System.getProperty("os.name" );  
  14.             String[] cmd = new String[3];  
  15.             if( osName.equals( "Windows NT" ) )  
  16.             {  
  17.                 cmd[0] = "cmd.exe" ;  
  18.                 cmd[1] = "/C" ;  
  19.                 cmd[2] = args[0];  
  20.             }  
  21.             else if( osName.equals( "Windows 95" ) )  
  22.             {  
  23.                 cmd[0] = "command.com" ;  
  24.                 cmd[1] = "/C" ;  
  25.                 cmd[2] = args[0];  
  26.             } else {  
  27.                 StringTokenizer st = new StringTokenizer(command, " ");  
  28.                 cmd = new String[st.countTokens()];  
  29.                 int token = 0;  
  30.                 while (st.hasMoreTokens()) {  
  31.                     String tokenString = st.nextToken();  
  32.                     // System.out.println(tokenString);  
  33.                     cmd[token++] = tokenString;  
  34.                 }  
  35.             }  
  36.               
  37.             Runtime rt = Runtime.getRuntime();  
  38.             System.out.println("Execing " + cmd[0] + " " + cmd[1]   
  39.                                + " " + cmd[2]);  
  40.             Process proc = rt.exec(cmd);  
  41.             // any error message?  
  42.             StreamGobbler errorGobbler = new   
  43.                 StreamGobbler(proc.getErrorStream(), "ERROR");              
  44.               
  45.             // any output?  
  46.             StreamGobbler outputGobbler = new   
  47.                 StreamGobbler(proc.getInputStream(), "OUTPUT");  
  48.                   
  49.             // kick them off  
  50.             errorGobbler.start();  
  51.             outputGobbler.start();  
  52.                                       
  53.             // any error???  
  54.             int exitVal = proc.waitFor();  
  55.             System.out.println("ExitValue: " + exitVal);          
  56.         } catch (Throwable t)  
  57.           {  
  58.             t.printStackTrace();  
  59.           }  
  60.     }  
  61. }  

Blocking method referenced address: https://tivan.iteye.com/blog/1045518

Guess you like

Origin blog.csdn.net/N_E_W_J_A_V_A/article/details/86647042