Use java script execution shell

There is a script tasks performed by system BPMN, a shell script is the task of the system task script, use the shell script is by the java.

ProcessBuilder class code for java.lang.ProcessBuilder. You can refer javaAPI or blog https://blog.csdn.net/u013256816/article/details/54603910

Of course, also possible () method exec java.lang.Runtime shell script class.

 1 package com.alphajuns;
 2 
 3 import org.junit.Test;
 4 
 5 import java.io.*;
 6 import java.util.ArrayList;
 7 import java.util.List;
 8 
 9 public class CMDCommandTest {
10 
11     @Test
12     public void run() throws IOException {
13         // 创建命令集合
14         List<String> commandList = new ArrayList<String>();
15         commandList.add ( "cmd" );
 16          commandList.add ( "/ C");   // Close after execution 
. 17          commandList.add ( "echo" );
 18 is          commandList.add ( "Hello" );
 . 19          commandList.add ( "cmd" );
 20 is          // ProcessBuilder is a process for creating an operating system type, its start () method is used for a start 
21 is          ProcessBuilder ProcessBuilder = new new ProcessBuilder (CommandList);
 22 is          // start the process 
23 is          process process = ProcessBuilder.start ();
 24          // parse the output 
25          String Result = convertStreamToStr(process.getInputStream());
26         System.out.println(result);
27     }
28 
29     public String convertStreamToStr(InputStream is) throws IOException {
30         if (is != null) {
31             Writer writer = new StringWriter();
32             char[] buffer = new char[1024];
33             try {
34                 Reader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
35                 int n;
36                 while ((n = reader.read(buffer)) != -1) {
37                     writer.write(buffer, 0, n);
38                 }
39             } catch (UnsupportedEncodingException e) {
40                 e.printStackTrace();
41             } catch (IOException e) {
42                 e.printStackTrace();
43             } finally {
44                 is.close();
45             }
46             return writer.toString();
47         } else {
48             return "";
49         }
50     }
51 }

Attach Junit Test Results

 

Guess you like

Origin www.cnblogs.com/alphajuns/p/11373512.html