• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

LearnLib / automatalib / 16696482329

02 Aug 2025 06:25PM UTC coverage: 92.155% (+0.04%) from 92.114%
16696482329

push

github

mtf90
try alternative path computation

16587 of 17999 relevant lines covered (92.16%)

1.71 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

96.3
/commons/util/src/main/java/net/automatalib/common/util/process/ProcessUtil.java
1
/* Copyright (C) 2013-2025 TU Dortmund University
2
 * This file is part of AutomataLib <https://automatalib.net>.
3
 *
4
 * Licensed under the Apache License, Version 2.0 (the "License");
5
 * you may not use this file except in compliance with the License.
6
 * You may obtain a copy of the License at
7
 *
8
 *     http://www.apache.org/licenses/LICENSE-2.0
9
 *
10
 * Unless required by applicable law or agreed to in writing, software
11
 * distributed under the License is distributed on an "AS IS" BASIS,
12
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
 * See the License for the specific language governing permissions and
14
 * limitations under the License.
15
 */
16
package net.automatalib.common.util.process;
17

18
import java.io.IOException;
19
import java.io.OutputStream;
20
import java.io.Reader;
21
import java.io.Writer;
22
import java.util.Arrays;
23
import java.util.List;
24
import java.util.function.Consumer;
25

26
import net.automatalib.common.util.IOUtil;
27
import net.automatalib.common.util.process.InputStreamConsumer.CopyConsumer;
28
import net.automatalib.common.util.process.InputStreamConsumer.DelegatingConsumer;
29
import org.checkerframework.checker.nullness.qual.Nullable;
30

31
/**
32
 * Utility class for invoking system processes.
33
 */
34
public final class ProcessUtil {
35

36
    private ProcessUtil() {
37
        // prevent instantiation
38
    }
39

40
    /**
41
     * Runs the given set of command line arguments as a system process and returns the exit value of the spawned
42
     * process. Discards any output of the process.
43
     *
44
     * @param commandLine
45
     *         the list of command line arguments to run
46
     *
47
     * @return the exit code of the process
48
     *
49
     * @throws IOException
50
     *         if an exception occurred while reading the process' outputs
51
     * @throws InterruptedException
52
     *         if an exception occurred during process exception
53
     */
54
    public static int invokeProcess(String[] commandLine) throws IOException, InterruptedException {
55
        return invokeProcess(Arrays.asList(commandLine));
2✔
56
    }
57

58
    /**
59
     * Runs the given set of command line arguments as a system process and returns the exit value of the spawned
60
     * process. Discards any output of the process.
61
     *
62
     * @param commandLine
63
     *         the list of command line arguments to run
64
     *
65
     * @return the exit code of the process
66
     *
67
     * @throws IOException
68
     *         if an exception occurred while reading the process' outputs
69
     * @throws InterruptedException
70
     *         if an exception occurred during process exception
71
     */
72
    public static int invokeProcess(List<String> commandLine) throws IOException, InterruptedException {
73
        return invokeProcess(commandLine, null, OutputStream.nullOutputStream(), OutputStream.nullOutputStream());
2✔
74
    }
75

76
    /**
77
     * Runs the given set of command line arguments as a system process and returns the exit value of the spawned
78
     * process. Additionally, allows one to supply an input stream to the invoked program. Discards any output of the
79
     * process.
80
     *
81
     * @param commandLine
82
     *         the list of command line arguments to run
83
     * @param input
84
     *         the input passed to the program
85
     *
86
     * @return the exit code of the process
87
     *
88
     * @throws IOException
89
     *         if an exception occurred while reading the process' outputs, or writing the process' inputs
90
     * @throws InterruptedException
91
     *         if an exception occurred during process exception
92
     */
93
    public static int invokeProcess(String[] commandLine, Reader input) throws IOException, InterruptedException {
94
        return invokeProcess(Arrays.asList(commandLine), input);
2✔
95
    }
96

97
    /**
98
     * Runs the given set of command line arguments as a system process and returns the exit value of the spawned
99
     * process. Additionally, allows one to supply an input stream to the invoked program. Discards any output of the
100
     * process.
101
     *
102
     * @param commandLine
103
     *         the list of command line arguments to run
104
     * @param input
105
     *         the input passed to the program
106
     *
107
     * @return the exit code of the process
108
     *
109
     * @throws IOException
110
     *         if an exception occurred while reading the process' outputs, or writing the process' inputs
111
     * @throws InterruptedException
112
     *         if an exception occurred during process exception
113
     */
114
    public static int invokeProcess(List<String> commandLine, Reader input) throws IOException, InterruptedException {
115
        return invokeProcess(commandLine, input, OutputStream.nullOutputStream(), OutputStream.nullOutputStream());
2✔
116
    }
117

118
    /**
119
     * Runs the given set of command line arguments as a system process and returns the exit value of the spawned
120
     * process. Outputs of the process (normal and error) are passed to the respective {@code consumer}s.
121
     *
122
     * @param commandLine
123
     *         the list of command line arguments to run
124
     * @param stdOutConsumer
125
     *         the consumer for the program's standard output
126
     * @param stdErrConsumer
127
     *         the consumer for the program's error output
128
     *
129
     * @return the exit code of the process
130
     *
131
     * @throws IOException
132
     *         if an exception occurred while reading the process' outputs
133
     * @throws InterruptedException
134
     *         if an exception occurred during process exception
135
     */
136
    public static int invokeProcess(String[] commandLine,
137
                                    Consumer<String> stdOutConsumer,
138
                                    Consumer<String> stdErrConsumer) throws IOException, InterruptedException {
139
        return invokeProcess(Arrays.asList(commandLine), stdOutConsumer, stdErrConsumer);
2✔
140
    }
141

142
    /**
143
     * Runs the given set of command line arguments as a system process and returns the exit value of the spawned
144
     * process. Outputs of the process (normal and error) are passed to the respective {@code consumer}s such that each
145
     * line results in a separate call to {@link Consumer#accept(Object)}.
146
     *
147
     * @param commandLine
148
     *         the list of command line arguments to run
149
     * @param stdOutConsumer
150
     *         the consumer for the program's standard output
151
     * @param stdErrConsumer
152
     *         the consumer for the program's error output
153
     *
154
     * @return the exit code of the process
155
     *
156
     * @throws IOException
157
     *         if an exception occurred while reading the process' outputs
158
     * @throws InterruptedException
159
     *         if an exception occurred during process exception
160
     */
161
    public static int invokeProcess(List<String> commandLine,
162
                                    Consumer<String> stdOutConsumer,
163
                                    Consumer<String> stdErrConsumer) throws IOException, InterruptedException {
164
        return invokeProcess(commandLine, null, stdOutConsumer, stdErrConsumer);
2✔
165
    }
166

167
    /**
168
     * Runs the given set of command line arguments as a system process and returns the exit value of the spawned
169
     * process. Additionally, allows one to supply an input stream to the invoked program. Outputs of the process
170
     * (normal and error) are passed to the respective {@code consumer}s such that each line results in a separate call
171
     * to {@link Consumer#accept(Object)}.
172
     *
173
     * @param commandLine
174
     *         the list of command line arguments to run
175
     * @param input
176
     *         the input passed to the program
177
     * @param stdOutConsumer
178
     *         the consumer for the program's standard output
179
     * @param stdErrConsumer
180
     *         the consumer for the program's error output
181
     *
182
     * @return the exit code of the process
183
     *
184
     * @throws IOException
185
     *         if an exception occurred while reading the process' outputs, or writing the process' inputs
186
     * @throws InterruptedException
187
     *         if an exception occurred during process exception
188
     */
189
    public static int invokeProcess(String[] commandLine,
190
                                    Reader input,
191
                                    Consumer<String> stdOutConsumer,
192
                                    Consumer<String> stdErrConsumer) throws IOException, InterruptedException {
193
        return invokeProcess(Arrays.asList(commandLine), input, stdOutConsumer, stdErrConsumer);
2✔
194
    }
195

196
    /**
197
     * Runs the given set of command line arguments as a system process and returns the exit value of the spawned
198
     * process. Additionally, allows one to supply an input stream to the invoked program. Outputs of the process
199
     * (normal and error) are passed to the respective {@code consumer}s such that each line results in a separate call
200
     * to {@link Consumer#accept(Object)}.
201
     *
202
     * @param commandLine
203
     *         the list of command line arguments to run
204
     * @param input
205
     *         the input passed to the program
206
     * @param stdOutConsumer
207
     *         the consumer for the program's standard output
208
     * @param stdErrConsumer
209
     *         the consumer for the program's error output
210
     *
211
     * @return the exit code of the process
212
     *
213
     * @throws IOException
214
     *         if an exception occurred while reading the process' outputs, or writing the process' inputs
215
     * @throws InterruptedException
216
     *         if the process is interrupted prior to finishing
217
     */
218
    public static int invokeProcess(List<String> commandLine,
219
                                    @Nullable Reader input,
220
                                    Consumer<String> stdOutConsumer,
221
                                    Consumer<String> stdErrConsumer) throws IOException, InterruptedException {
222
        return invokeProcess(commandLine,
2✔
223
                             input,
224
                             new DelegatingConsumer(stdOutConsumer),
225
                             new DelegatingConsumer(stdErrConsumer));
226
    }
227

228
    /**
229
     * Runs the given set of command line arguments as a system process and returns the exit value of the spawned
230
     * process. Additionally, allows one to supply an input stream to the invoked program. Outputs of the process
231
     * (normal and error) are passed to the respective {@code consumer}s.
232
     *
233
     * @param commandLine
234
     *         the list of command line arguments to run
235
     * @param input
236
     *         the input passed to the program
237
     * @param stdOutConsumer
238
     *         the consumer for the program's standard output
239
     * @param stdErrConsumer
240
     *         the consumer for the program's error output
241
     *
242
     * @return the exit code of the process
243
     *
244
     * @throws IOException
245
     *         if an exception occurred while reading the process' outputs, or writing the process' inputs
246
     * @throws InterruptedException
247
     *         if the process is interrupted prior to finishing
248
     */
249
    public static int invokeProcess(String[] commandLine,
250
                                    Reader input,
251
                                    OutputStream stdOutConsumer,
252
                                    OutputStream stdErrConsumer) throws IOException, InterruptedException {
253
        return invokeProcess(Arrays.asList(commandLine), input, stdOutConsumer, stdErrConsumer);
×
254
    }
255

256
    /**
257
     * Runs the given set of command line arguments as a system process and returns the exit value of the spawned
258
     * process. Additionally, allows one to supply an input stream to the invoked program. Outputs of the process
259
     * (normal and error) are passed to the respective {@code consumer}s.
260
     *
261
     * @param commandLine
262
     *         the list of command line arguments to run
263
     * @param input
264
     *         the input passed to the program
265
     * @param stdOutConsumer
266
     *         the consumer for the program's standard output
267
     * @param stdErrConsumer
268
     *         the consumer for the program's error output
269
     *
270
     * @return the exit code of the process
271
     *
272
     * @throws IOException
273
     *         if an exception occurred while reading the process' outputs, or writing the process' inputs
274
     * @throws InterruptedException
275
     *         if the process is interrupted prior to finishing
276
     */
277
    public static int invokeProcess(List<String> commandLine,
278
                                    @Nullable Reader input,
279
                                    OutputStream stdOutConsumer,
280
                                    OutputStream stdErrConsumer) throws IOException, InterruptedException {
281
        return invokeProcess(commandLine, input, new CopyConsumer(stdOutConsumer), new CopyConsumer(stdErrConsumer));
2✔
282
    }
283

284
    private static int invokeProcess(List<String> commandLine,
285
                                     @Nullable Reader input,
286
                                     InputStreamConsumer stdOutConsumer,
287
                                     InputStreamConsumer stdErrConsumer) throws IOException, InterruptedException {
288

289
        final ProcessBuilder processBuilder = new ProcessBuilder(commandLine);
2✔
290
        final Process process = processBuilder.start();
2✔
291

292
        final Thread stdOutThread = new StreamGobbler(process.getInputStream(), stdOutConsumer);
2✔
293
        final Thread stdErrThread = new StreamGobbler(process.getErrorStream(), stdErrConsumer);
2✔
294

295
        // consume process outputs to prevent blocking from full buffers
296
        stdOutThread.start();
2✔
297
        stdErrThread.start();
2✔
298

299
        writeProcessInput(process, input);
2✔
300

301
        try {
302
            final int exitVal = process.waitFor();
2✔
303

304
            // Handle situation where the process ends before the threads finish
305
            stdOutThread.join();
2✔
306
            stdErrThread.join();
2✔
307

308
            return exitVal;
2✔
309
        } finally {
310
            // cleanup
311
            process.destroy();
2✔
312
        }
313
    }
314

315
    private static void writeProcessInput(Process process, @Nullable Reader input) throws IOException {
316
        if (input != null) {
2✔
317
            try (OutputStream processInput = process.getOutputStream();
2✔
318
                 Writer writer = IOUtil.asBufferedUTF8Writer(processInput)) {
2✔
319
                input.transferTo(writer);
2✔
320
            }
321
        }
322
    }
2✔
323

324
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc