Skip to content

Commit 285364a

Browse files
committed
U fixed issue when checking if an existing jre is valid before embedding it
1 parent 53c737d commit 285364a

File tree

2 files changed

+52
-31
lines changed

2 files changed

+52
-31
lines changed

src/main/java/io/github/fvarrui/javapackager/packagers/BundleJre.java

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -53,28 +53,39 @@ protected File doApply(Packager packager) throws Exception {
5353

5454
Logger.info("Embedding JRE from " + specificJreFolder);
5555

56-
// fixes the path to the JRE on MacOS if "release" file not found
57-
if (platform.equals(Platform.mac) && !FileUtils.folderContainsFile(specificJreFolder, "release")) {
58-
specificJreFolder = new File(specificJreFolder, "Contents/Home");
59-
Logger.warn("Specified jrePath fixed: " + specificJreFolder);
56+
if (!specificJreFolder.isDirectory()) {
57+
throw new Exception("'" + specificJreFolder + "' is not a directory!");
6058
}
61-
62-
// checks if valid jre specified
59+
60+
// checks if the specified jre is valid (it looks for 'release' file into it, and if so, checks if it matches the right platform
6361
if (!JDKUtils.isValidJRE(platform, specificJreFolder)) {
64-
throw new Exception("Invalid JRE specified for '" + platform + "' platform: " + specificJreFolder);
62+
63+
Logger.warn("An invalid JRE may have been specified for '" + platform + "' platform: " + specificJreFolder + " ('release' file not found)");
64+
65+
// try to fix the path to the JRE on MacOS adding Contents/Home to JRE path
66+
if (platform.equals(Platform.mac)) {
67+
68+
File fixedJreFolder = new File(specificJreFolder, "Contents/Home");
69+
if (JDKUtils.isValidJRE(platform, fixedJreFolder)) {
70+
specificJreFolder = fixedJreFolder;
71+
Logger.warn("Specified 'jrePath' fixed: " + specificJreFolder);
72+
}
73+
74+
}
75+
6576
}
66-
77+
6778
// removes old jre folder from bundle
6879
if (destinationFolder.exists()) FileUtils.removeFolder(destinationFolder);
6980

7081
// copies JRE folder to bundle
7182
FileUtils.copyFolderContentToFolder(specificJreFolder, destinationFolder);
7283

73-
// sets execution permissions on executables in jre
84+
// sets execute permissions on executables in jre
7485
File binFolder = new File(destinationFolder, "bin");
7586
Arrays.asList(binFolder.listFiles()).forEach(f -> f.setExecutable(true, false));
7687

77-
// sets execution permissions on jspawnhelper in jre
88+
// sets execute permissions on jspawnhelper in jre
7889
File libFolder = new File(destinationFolder, "lib");
7990
File jshFile = new File(libFolder, "jspawnhelper");
8091
if (jshFile.exists()) {

src/main/java/io/github/fvarrui/javapackager/utils/JDKUtils.java

Lines changed: 31 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -11,31 +11,33 @@
1111
import io.github.fvarrui.javapackager.model.Platform;
1212

1313
/**
14-
* JDK utils
14+
* JDK utils
1515
*/
1616
public 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

Comments
 (0)