Skip to content

Commit 2ae109d

Browse files
committed
Prevent NPE for orphan JRE installations
This closes #1421
1 parent 93f3099 commit 2ae109d

File tree

1 file changed

+13
-6
lines changed

1 file changed

+13
-6
lines changed

org.eclipse.m2e.launching/src/org/eclipse/m2e/internal/launch/MavenLaunchDelegate.java

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,7 @@ public static IVMInstall getBestMatchingVM(String requiredVersion) {
321321
}
322322
if(mainVersion != null) {
323323
return matchingJREs.stream()
324-
.filter(jre -> getArtifactVersion(jre).getMajorVersion() == mainVersion.getMajorVersion()).findFirst()
324+
.filter(jre -> getJREVersion(jre).getMajorVersion() == mainVersion.getMajorVersion()).findFirst()
325325
.orElse(null);
326326
}
327327
return !matchingJREs.isEmpty() ? matchingJREs.get(0) : null;
@@ -338,8 +338,9 @@ private static List<IVMInstall> getAllMatchingJREs(VersionRange versionRange) {
338338
for(IVMInstallType vmType : JavaRuntime.getVMInstallTypes()) {
339339
for(IVMInstall vm : vmType.getVMInstalls()) {
340340
if(satisfiesVersionRange(vm, versionRange)) {
341-
if(vm instanceof IVMInstall2 vm2) {
342-
installedJREsByVersion.put(new DefaultArtifactVersion(vm2.getJavaVersion()), vm);
341+
ArtifactVersion jreVersion = getJREVersion(vm);
342+
if(jreVersion != DEFAULT_JAVA_VERSION) {
343+
installedJREsByVersion.put(jreVersion, vm);
343344
} else {
344345
log.debug("Skipping IVMInstall '{}' from type {} as not implementing IVMInstall2", vm.getName(),
345346
vmType.getName());
@@ -351,7 +352,7 @@ private static List<IVMInstall> getAllMatchingJREs(VersionRange versionRange) {
351352
}
352353

353354
private static boolean satisfiesVersionRange(IVMInstall jre, VersionRange versionRange) {
354-
ArtifactVersion jreVersion = getArtifactVersion(jre);
355+
ArtifactVersion jreVersion = getJREVersion(jre);
355356
if(versionRange.getRecommendedVersion() != null) {
356357
return jreVersion.compareTo(versionRange.getRecommendedVersion()) >= 0;
357358
}
@@ -360,8 +361,14 @@ private static boolean satisfiesVersionRange(IVMInstall jre, VersionRange versio
360361

361362
private static final ArtifactVersion DEFAULT_JAVA_VERSION = new DefaultArtifactVersion("0.0.0");
362363

363-
private static ArtifactVersion getArtifactVersion(IVMInstall jre) {
364-
return jre instanceof IVMInstall2 jre2 ? new DefaultArtifactVersion(jre2.getJavaVersion()) : DEFAULT_JAVA_VERSION;
364+
private static ArtifactVersion getJREVersion(IVMInstall jre) {
365+
if(jre instanceof IVMInstall2 jre2) {
366+
String version = jre2.getJavaVersion();
367+
if(version != null) {
368+
return new DefaultArtifactVersion(version);
369+
}
370+
}
371+
return DEFAULT_JAVA_VERSION;
365372
}
366373

367374
@Override

0 commit comments

Comments
 (0)