1111import io .github .fvarrui .javapackager .model .Platform ;
1212
1313/**
14- * JDK utils
14+ * JDK utils
1515 */
1616public class JDKUtils {
17-
17+
1818 /**
1919 * Converts "release" file from specified JDK or JRE to map
20+ *
2021 * @param jdkPath JDK directory path
2122 * @return Map with all properties
22- * @throws FileNotFoundException release file not found
2323 * @throws IOException release file could not be read
2424 */
25- private static Map <String , String > getRelease (File jdkPath ) throws FileNotFoundException , IOException {
25+ private static Map <String , String > getRelease (File jdkPath ) throws IOException {
2626 Map <String , String > propertiesMap = new HashMap <>();
2727 File releaseFile = new File (jdkPath , "release" );
2828 if (!releaseFile .exists ()) {
29- throw new FileNotFoundException ( "release file not found: " + releaseFile );
29+ return propertiesMap ;
3030 }
3131 Properties properties = new Properties ();
3232 properties .load (new FileInputStream (releaseFile ));
3333 properties .forEach ((key , value ) -> propertiesMap .put (key .toString (), value .toString ().replaceAll ("^\" |\" $" , "" )));
3434 return propertiesMap ;
3535 }
36-
36+
3737 /**
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+ *
3941 * @param platform
4042 * @param jdkPath
4143 * @return true if JDK is for platform
@@ -46,38 +48,46 @@ private static boolean checkPlatform(Platform platform, File jdkPath) throws Fil
4648 Map <String , String > releaseMap = getRelease (jdkPath );
4749 String osName = releaseMap .get ("OS_NAME" );
4850 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 ;
5359 }
5460 }
55-
61+
5662 /**
5763 * Checks if a JDK is for platform
64+ *
5865 * @param platform Specific platform
5966 * @param jdkPath Path to the JDK folder
6067 * @return true if is valid, otherwise false
61- * @throws FileNotFoundException Path to JDK not found
68+ * @throws FileNotFoundException Path to JDK not found
6269 * @throws IOException Error reading JDK "release" file
6370 */
6471 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 );
6673 }
67-
74+
6875 /**
6976 * Checks if a JRE is for platform
77+ *
7078 * @param platform Specific platform
71- * @param jrePath Path to the JRE folder
79+ * @param jrePath Path to the JRE folder
7280 * @return true if is valid, otherwise false
7381 * @throws IOException Error reading JDK "release" file
7482 */
7583 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+ );
8191 }
8292
8393}
0 commit comments