Skip to content

Commit 4d7fef5

Browse files
committed
A tests if specified JDK is for the same platform than target platform
1 parent 99e1658 commit 4d7fef5

File tree

2 files changed

+70
-1
lines changed

2 files changed

+70
-1
lines changed

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import io.github.fvarrui.javapackager.utils.CommandUtils;
2828
import io.github.fvarrui.javapackager.utils.FileUtils;
2929
import io.github.fvarrui.javapackager.utils.IconUtils;
30+
import io.github.fvarrui.javapackager.utils.JDKUtils;
3031
import io.github.fvarrui.javapackager.utils.JavaUtils;
3132
import io.github.fvarrui.javapackager.utils.Logger;
3233
import io.github.fvarrui.javapackager.utils.VelocityUtils;
@@ -292,8 +293,14 @@ protected void bundleJre(File destinationFolder, File jarFile, File libsFolder,
292293
Logger.warn("Cannot create a customized JRE ... target platform (" + platform + ") is different than execution platform (" + Platform.getCurrentPlatform() + "). Use jdkPath property.");
293294

294295
bundleJre = false;
295-
296+
296297
} else {
298+
299+
// tests if specified JDK is for the same platform than target platform
300+
if (!JDKUtils.isValidJdk(platform, jdkPath)) {
301+
throw new Exception("Invalid JDK: '" + jdkPath + "' for platform: " + platform);
302+
}
303+
297304

298305
String modules = getRequiredModules(libsFolder, customizedJre, jarFile, defaultModules, additionalModules);
299306

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package io.github.fvarrui.javapackager.utils;
2+
3+
import java.io.File;
4+
import java.io.FileInputStream;
5+
import java.io.FileNotFoundException;
6+
import java.io.IOException;
7+
import java.util.HashMap;
8+
import java.util.Map;
9+
import java.util.Properties;
10+
11+
import io.github.fvarrui.javapackager.model.Platform;
12+
13+
public class JDKUtils {
14+
15+
/**
16+
* Converts "release" file from specified JDK to map
17+
* @param jdkPath JDK directory path
18+
* @return Map with all properties
19+
* @throws FileNotFoundException release file not found
20+
* @throws IOException release file could not be read
21+
*/
22+
public static Map<String, String> getRelease(File jdkPath) throws FileNotFoundException, IOException {
23+
Map<String, String> propertiesMap = new HashMap<>();
24+
25+
File releaseFile = new File(jdkPath, "release");
26+
27+
if (!releaseFile.exists()) {
28+
releaseFile = new File(jdkPath, "Contents/Home/release");
29+
}
30+
31+
if (!releaseFile.exists()) {
32+
throw new FileNotFoundException("release file not found");
33+
}
34+
35+
Properties properties = new Properties();
36+
properties.load(new FileInputStream(releaseFile));
37+
properties.forEach((key, value) -> propertiesMap.put(key.toString(), value.toString().replaceAll("^\"|\"$", "")));
38+
39+
return propertiesMap;
40+
}
41+
42+
public static boolean isValidJdk(Platform platform, File jdkPath) throws Exception {
43+
44+
try {
45+
Map<String, String> releaseMap = getRelease(jdkPath);
46+
String osName = releaseMap.get("OS_NAME");
47+
switch (platform) {
48+
case linux: return "Linux".equalsIgnoreCase(osName);
49+
case mac: return "Darwin".equalsIgnoreCase(osName);
50+
case windows: return "Windows".equalsIgnoreCase(osName);
51+
default:
52+
}
53+
} catch (FileNotFoundException e) {
54+
throw new Exception(e.getMessage(), e);
55+
} catch (IOException e) {
56+
throw new Exception(e.getMessage(), e);
57+
}
58+
59+
return false;
60+
}
61+
62+
}

0 commit comments

Comments
 (0)