1
1
/*
2
- * Copyright (c) 2015, 2018 , Oracle and/or its affiliates. All rights reserved.
2
+ * Copyright (c) 2015, 2019 , Oracle and/or its affiliates. All rights reserved.
3
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
4
*
5
5
* This code is free software; you can redistribute it and/or modify it
24
24
import java .io .*;
25
25
import java .lang .module .ModuleDescriptor ;
26
26
import java .lang .reflect .Method ;
27
- import java .nio .file .*;
27
+ import java .nio .file .Files ;
28
+ import java .nio .file .Path ;
29
+ import java .nio .file .Paths ;
28
30
import java .util .*;
29
31
import java .util .function .Consumer ;
30
32
import java .util .jar .JarEntry ;
31
33
import java .util .jar .JarFile ;
32
34
import java .util .jar .JarInputStream ;
33
35
import java .util .jar .Manifest ;
34
36
import java .util .regex .Pattern ;
37
+ import java .util .spi .ToolProvider ;
35
38
import java .util .stream .Collectors ;
36
39
import java .util .stream .Stream ;
37
40
59
62
*/
60
63
61
64
public class Basic {
65
+
66
+ private static final ToolProvider JAR_TOOL = ToolProvider .findFirst ("jar" )
67
+ .orElseThrow (()
68
+ -> new RuntimeException ("jar tool not found" )
69
+ );
70
+ private static final ToolProvider JAVAC_TOOL = ToolProvider .findFirst ("javac" )
71
+ .orElseThrow (()
72
+ -> new RuntimeException ("javac tool not found" )
73
+ );
74
+
62
75
static final Path TEST_SRC = Paths .get (System .getProperty ("test.src" , "." ));
63
76
static final Path TEST_CLASSES = Paths .get (System .getProperty ("test.classes" , "." ));
64
77
static final Path MODULE_CLASSES = TEST_CLASSES .resolve ("build" );
@@ -933,13 +946,14 @@ static Result jarWithStdin(File stdinSource, String... args) {
933
946
}
934
947
Stream .of (args ).forEach (commands ::add );
935
948
ProcessBuilder p = new ProcessBuilder (commands );
936
- if (stdinSource != null )
949
+ if (stdinSource != null ) {
937
950
p .redirectInput (stdinSource );
951
+ }
938
952
return run (p );
939
953
}
940
954
941
955
static Result jar (String ... args ) {
942
- return jarWithStdin ( null , args );
956
+ return run ( JAR_TOOL , args );
943
957
}
944
958
945
959
static Path compileModule (String mn ) throws IOException {
@@ -1027,10 +1041,8 @@ static void javac(Path dest, Path... sourceFiles) throws IOException {
1027
1041
static void javac (Path dest , Path modulePath , Path ... sourceFiles )
1028
1042
throws IOException
1029
1043
{
1030
- String javac = getJDKTool ("javac" );
1031
1044
1032
1045
List <String > commands = new ArrayList <>();
1033
- commands .add (javac );
1034
1046
if (!TOOL_VM_OPTIONS .isEmpty ()) {
1035
1047
commands .addAll (Arrays .asList (TOOL_VM_OPTIONS .split ("\\ s+" , -1 )));
1036
1048
}
@@ -1048,7 +1060,13 @@ static void javac(Path dest, Path modulePath, Path... sourceFiles)
1048
1060
}
1049
1061
Stream .of (sourceFiles ).map (Object ::toString ).forEach (x -> commands .add (x ));
1050
1062
1051
- quickFail (run (new ProcessBuilder (commands )));
1063
+ StringWriter sw = new StringWriter ();
1064
+ try (PrintWriter pw = new PrintWriter (sw )) {
1065
+ int rc = JAVAC_TOOL .run (pw , pw , commands .toArray (new String [0 ]));
1066
+ if (rc != 0 ) {
1067
+ throw new RuntimeException (sw .toString ());
1068
+ }
1069
+ }
1052
1070
}
1053
1071
1054
1072
static Result java (Path modulePath , String entryPoint , String ... args ) {
@@ -1094,9 +1112,13 @@ static boolean jarContains(JarInputStream jis, String entryName)
1094
1112
return false ;
1095
1113
}
1096
1114
1097
- static void quickFail (Result r ) {
1098
- if (r .ec != 0 )
1099
- throw new RuntimeException (r .output );
1115
+ static Result run (ToolProvider tp , String [] commands ) {
1116
+ int rc = 0 ;
1117
+ StringWriter sw = new StringWriter ();
1118
+ try (PrintWriter pw = new PrintWriter (sw )) {
1119
+ rc = tp .run (pw , pw , commands );
1120
+ }
1121
+ return new Result (rc , sw .toString ());
1100
1122
}
1101
1123
1102
1124
static Result run (ProcessBuilder pb ) {
0 commit comments