Skip to content

Commit

Permalink
improve java version detection
Browse files Browse the repository at this point in the history
  • Loading branch information
per committed Dec 27, 2021
1 parent 28df0f8 commit 1d3ceb0
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/main/java/se/alipsa/ride/code/jstab/JsTab.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import se.alipsa.ride.console.ConsoleTextArea;
import se.alipsa.ride.console.WarningAppenderWriter;
import se.alipsa.ride.utils.ExceptionAlert;
import se.alipsa.ride.utils.JavaVersion;

import javax.script.ScriptContext;
import javax.script.ScriptEngine;
Expand Down Expand Up @@ -59,7 +60,7 @@ public JsTab(String title, Ride gui) {

public void initSession() {
NashornScriptEngineFactory nashornScriptEngineFactory = new NashornScriptEngineFactory();
double jvmVersion = Double.parseDouble(System.getProperty("java.specification.version"));
int jvmVersion = JavaVersion.specVersion();
String[] options;
// Nashorn is part of the JDK until Java 14 but anything above 11 is not tested (it became deprecated in java 11)
if (jvmVersion >= 11) {
Expand Down
22 changes: 22 additions & 0 deletions src/main/java/se/alipsa/ride/utils/JavaVersion.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package se.alipsa.ride.utils;


public final class JavaVersion {

private JavaVersion() {
// Utility class
}

/**
*
* @return the java version currently running
* for java < 9 the right-hand side will be returned e.g 1.8 will become 8
*/
public static int specVersion() {
double jvmVersion = Double.parseDouble(System.getProperty("java.specification.version"));
if (jvmVersion < 2) {
jvmVersion = (jvmVersion - 1) * 10;
}
return (int)Math.round(jvmVersion);
}
}

0 comments on commit 1d3ceb0

Please sign in to comment.