1616
1717package org .intellij .erlang .utils ;
1818
19+ import com .intellij .execution .ExecutionException ;
20+ import com .intellij .execution .configurations .GeneralCommandLine ;
1921import com .intellij .openapi .util .text .StringUtil ;
2022import org .jetbrains .annotations .NotNull ;
2123
@@ -30,26 +32,31 @@ private ExtProcessUtil() {
3032 }
3133
3234 @ NotNull
33- public static ExtProcessOutput execAndGetFirstLine (@ NotNull String cmd , long timeout ) {
35+ public static ExtProcessOutput execAndGetFirstLine (long timeout , @ NotNull String ... command ) {
3436 try {
35- final Process cmdRunner = Runtime . getRuntime (). exec ( cmd );
37+ final Process cmdRunner = new GeneralCommandLine ( command ). createProcess ( );
3638 ExecutorService singleTreadExecutor = Executors .newSingleThreadExecutor ();
37- Future <ExtProcessOutput > cmdRunnerFuture = singleTreadExecutor .submit (new Callable <ExtProcessOutput >() {
38- @ Override
39- public ExtProcessOutput call () throws Exception {
40- cmdRunner .waitFor ();
41- String stdOut = readLine (cmdRunner .getInputStream ());
42- String stdErr = readLine (cmdRunner .getErrorStream ());
43- return new ExtProcessOutput (stdOut , stdErr );
44- }
45- });
4639 try {
47- return cmdRunnerFuture .get (timeout , TimeUnit .MILLISECONDS );
48- } catch (Exception e ) { // Suppress
40+ Future <ExtProcessOutput > cmdRunnerFuture = singleTreadExecutor .submit (new Callable <ExtProcessOutput >() {
41+ @ Override
42+ public ExtProcessOutput call () throws Exception {
43+ cmdRunner .waitFor ();
44+ String stdOut = readLine (cmdRunner .getInputStream ());
45+ String stdErr = readLine (cmdRunner .getErrorStream ());
46+ return new ExtProcessOutput (stdOut , stdErr );
47+ }
48+ });
49+
50+ try {
51+ return cmdRunnerFuture .get (timeout , TimeUnit .MILLISECONDS );
52+ } catch (Exception e ) { // Suppress
53+ }
54+
55+ cmdRunnerFuture .cancel (true );
56+ } finally {
57+ singleTreadExecutor .shutdown ();
4958 }
50- cmdRunnerFuture .cancel (true );
51- singleTreadExecutor .shutdown ();
52- } catch (IOException e ) { // Suppress
59+ } catch (ExecutionException e ) { // Suppress
5360 }
5461 return new ExtProcessOutput ("" , "" );
5562 }
0 commit comments