Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Java version parsing for pre- and post-Java 9 versions #2143

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 26 additions & 1 deletion utils/src/main/java/org/web3j/commons/JavaVersion.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,38 @@
*/
package org.web3j.commons;

/**
* A utility class to retrieve the current Java version.
*/
public class JavaVersion {

/**
* Returns the Java specification version as a string.
* For example, "1.8" for Java 8 or "9" for Java 9 and above.
*
* @return the Java version as a string.
*/
public static String getJavaVersion() {
return System.getProperty("java.specification.version");
}

/**
* Returns the Java specification version as a double.
* For Java versions before 9 (e.g., Java 8), it returns the version number after "1."
* (e.g., 8.0 for Java 1.8).
* For Java versions 9 and above, it returns the version number directly
* (e.g., 9.0 for Java 9).
*
* @return the Java version as a double.
*/
public static double getJavaVersionAsDouble() {
return Double.parseDouble(System.getProperty("java.specification.version"));
String version = System.getProperty("java.specification.version");
if (version.startsWith("1.")) {
// For versions before Java 9 (e.g., "1.8"), extract the part after "1."
return Double.parseDouble(version.substring(2));
} else {
// For Java 9 and above (e.g., "9", "10"), parse the version directly
return Double.parseDouble(version);
}
}
}