11
11
import io .github .fvarrui .javapackager .model .Platform ;
12
12
13
13
/**
14
- * JDK utils
14
+ * JDK utils
15
15
*/
16
16
public class JDKUtils {
17
-
17
+
18
18
/**
19
19
* Converts "release" file from specified JDK or JRE to map
20
+ *
20
21
* @param jdkPath JDK directory path
21
22
* @return Map with all properties
22
- * @throws FileNotFoundException release file not found
23
23
* @throws IOException release file could not be read
24
24
*/
25
- private static Map <String , String > getRelease (File jdkPath ) throws FileNotFoundException , IOException {
25
+ private static Map <String , String > getRelease (File jdkPath ) throws IOException {
26
26
Map <String , String > propertiesMap = new HashMap <>();
27
27
File releaseFile = new File (jdkPath , "release" );
28
28
if (!releaseFile .exists ()) {
29
- throw new FileNotFoundException ( "release file not found: " + releaseFile );
29
+ return propertiesMap ;
30
30
}
31
31
Properties properties = new Properties ();
32
32
properties .load (new FileInputStream (releaseFile ));
33
33
properties .forEach ((key , value ) -> propertiesMap .put (key .toString (), value .toString ().replaceAll ("^\" |\" $" , "" )));
34
34
return propertiesMap ;
35
35
}
36
-
36
+
37
37
/**
38
- * Checks if the platform specified in the "release" file matches the required platform
38
+ * Checks if the platform specified in the "release" file matches the required
39
+ * platform
40
+ *
39
41
* @param platform
40
42
* @param jdkPath
41
43
* @return true if JDK is for platform
@@ -46,38 +48,46 @@ private static boolean checkPlatform(Platform platform, File jdkPath) throws Fil
46
48
Map <String , String > releaseMap = getRelease (jdkPath );
47
49
String osName = releaseMap .get ("OS_NAME" );
48
50
switch (platform ) {
49
- case linux : return "Linux" .equalsIgnoreCase (osName );
50
- case mac : return "Darwin" .equalsIgnoreCase (osName );
51
- case windows : return "Windows" .equalsIgnoreCase (osName );
52
- default : return false ;
51
+ case linux :
52
+ return "Linux" .equalsIgnoreCase (osName );
53
+ case mac :
54
+ return "Darwin" .equalsIgnoreCase (osName );
55
+ case windows :
56
+ return "Windows" .equalsIgnoreCase (osName );
57
+ default :
58
+ return false ;
53
59
}
54
60
}
55
-
61
+
56
62
/**
57
63
* Checks if a JDK is for platform
64
+ *
58
65
* @param platform Specific platform
59
66
* @param jdkPath Path to the JDK folder
60
67
* @return true if is valid, otherwise false
61
- * @throws FileNotFoundException Path to JDK not found
68
+ * @throws FileNotFoundException Path to JDK not found
62
69
* @throws IOException Error reading JDK "release" file
63
70
*/
64
71
public static boolean isValidJDK (Platform platform , File jdkPath ) throws FileNotFoundException , IOException {
65
- return checkPlatform (platform , jdkPath );
72
+ return jdkPath != null && jdkPath . isDirectory () && checkPlatform (platform , jdkPath );
66
73
}
67
-
74
+
68
75
/**
69
76
* Checks if a JRE is for platform
77
+ *
70
78
* @param platform Specific platform
71
- * @param jrePath Path to the JRE folder
79
+ * @param jrePath Path to the JRE folder
72
80
* @return true if is valid, otherwise false
73
81
* @throws IOException Error reading JDK "release" file
74
82
*/
75
83
public static boolean isValidJRE (Platform platform , File jrePath ) throws IOException {
76
- try {
77
- return checkPlatform (platform , jrePath );
78
- } catch (FileNotFoundException e ) {
79
- return new File (jrePath , "bin/java" ).exists () || new File (jrePath , "bin/java.exe" ).exists ();
80
- }
84
+ return (
85
+ jrePath != null &&
86
+ jrePath .isDirectory () &&
87
+ (checkPlatform (platform , jrePath ) ||
88
+ new File (jrePath , "bin/java" ).exists () ||
89
+ new File (jrePath , "bin/java.exe" ).exists ())
90
+ );
81
91
}
82
92
83
93
}
0 commit comments