Skip to content

Commit

Permalink
Feature java version check again (#3428)
Browse files Browse the repository at this point in the history
* Checking Java version on startup

* Update Localization messages

* Refactor code, make it more stable and add test

* Add comment regarding Java 9 check
  • Loading branch information
Patrick Scheibe authored and tobiasdiez committed Nov 13, 2017
1 parent dba8db5 commit c1d425a
Show file tree
Hide file tree
Showing 25 changed files with 283 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ We refer to [GitHub issues](https://github.com/JabRef/jabref/issues) by using `#
- Integrity check "Abbreviation Detection" detects abbreviated names for journals and booktitles based on the internal list instead of only looking for "." signs. Fixes [#3144](https://github.com/JabRef/jabref/issues/3144).
- We added a dialog to show that JabRef is working on checking integrity. [#3358](https://github.com/JabRef/jabref/issues/3358)
- We added an option to mass append to fields via the Quality -> set/clear/append/rename fields dialog. [#2721](https://github.com/JabRef/jabref/issues/2721)
- We added a check on startup to ensure JabRef is run with an adequate Java version. [3310](https://github.com/JabRef/jabref/issues/3310)

### Fixed
- We fixed the translation of \textendash in the entry preview [#3307](https://github.com/JabRef/jabref/issues/3307)
Expand Down
10 changes: 9 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ sourceCompatibility = 1.8
targetCompatibility = 1.8
mainClassName = "org.jabref.JabRefMain"

// These are the Java version requirements we will check on each start of JabRef
ext.minRequiredJavaVersion = "1.8.0_144"
ext.allowJava9 = false

install4j {
installDir = file(project.ext.install4jDir)
}
Expand Down Expand Up @@ -160,7 +164,11 @@ processResources {
"year": String.valueOf(Calendar.getInstance().get(Calendar.YEAR)),
"authors": new File('AUTHORS').readLines().findAll { !it.startsWith("#") }.join(", "),
"developers": new File('DEVELOPERS').readLines().findAll { !it.startsWith("#") }.join(", "),
"azureInstrumentationKey": System.getenv('AzureInstrumentationKey'))
"azureInstrumentationKey": System.getenv('AzureInstrumentationKey'),
"minRequiredJavaVersion": minRequiredJavaVersion,
"allowJava9": allowJava9

)
filteringCharset = 'UTF-8'
}

Expand Down
48 changes: 48 additions & 0 deletions src/main/java/org/jabref/JabRefMain.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import java.net.Authenticator;
import java.util.Map;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;

import javafx.application.Application;
Expand All @@ -24,6 +26,8 @@
import org.jabref.logic.protectedterms.ProtectedTermsLoader;
import org.jabref.logic.remote.RemotePreferences;
import org.jabref.logic.remote.client.RemoteListenerClient;
import org.jabref.logic.util.BuildInfo;
import org.jabref.logic.util.JavaVersion;
import org.jabref.logic.util.OS;
import org.jabref.migrations.PreferencesMigrations;
import org.jabref.model.EntryTypes;
Expand Down Expand Up @@ -53,11 +57,55 @@ public void start(Stage mainStage) throws Exception {
SwingUtilities.invokeLater(() -> start(arguments));
}

/**
* Tests if we are running an acceptable Java and terminates JabRef when we are sure the version is not supported.
* This test uses the requirements for the Java version as specified in <code>gradle.build</code>. It is possible to
* define a minimum version including the built number and to indicate whether Java 9 can be use (which it currently
* can't). It tries to compare this version number to the version of the currently running JVM. The check is
* optimistic and will rather return true even if we could not exactly determine the version.
* <p>
* Note: Users with an very old version like 1.6 will not profit from this since class versions are incompatible and
* JabRef won't even start. Currently, JabRef won't start with Java 9 either, but the warning that it cannot be used
* with this version is helpful anyway to prevent users to update from an old 1.8 directly to version 9. Additionally,
* we soon might have a JabRef that does start with Java 9 but is not perfectly compatible. Therefore, we should leave
* the Java 9 check alive.
*/
private static void ensureCorrectJavaVersion() {
// Check if we are running an acceptable version of Java
final BuildInfo buildInfo = Globals.BUILD_INFO;
JavaVersion checker = new JavaVersion();
final boolean java9Fail = !buildInfo.isAllowJava9() && checker.isJava9();
final boolean versionFail = !checker.isAtLeast(buildInfo.getMinRequiredJavaVersion());

if (java9Fail || versionFail) {
StringBuilder versionError = new StringBuilder(
Localization.lang("Your current Java version (%0) is not supported. Please install version %1 or higher.",
checker.getJavaVersion(),
buildInfo.getMinRequiredJavaVersion()
)
);
if (!buildInfo.isAllowJava9()) {
versionError.append("\n");
versionError.append(Localization.lang("Note that currently, JabRef does not run with Java 9."));
}
final JFrame frame = new JFrame();
JOptionPane.showMessageDialog(frame, versionError, Localization.lang("Error"), JOptionPane.ERROR_MESSAGE);
frame.dispose();

// We exit on Java 9 error since this will definitely not work
if (java9Fail) {
System.exit(0);
}
}
}

private static void start(String[] args) {
FallbackExceptionHandler.installExceptionHandler();

JabRefPreferences preferences = JabRefPreferences.getInstance();

ensureCorrectJavaVersion();

ProxyPreferences proxyPreferences = preferences.getProxyPreferences();
ProxyRegisterer.register(proxyPreferences);
if (proxyPreferences.isUseProxy() && proxyPreferences.isUseAuthentication()) {
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/org/jabref/logic/util/BuildInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ public class BuildInfo {
private final String developers;
private final String year;
private final String azureInstrumentationKey;
private final String minRequiredJavaVersion;
private final boolean allowJava9;


public BuildInfo() {
Expand All @@ -45,6 +47,8 @@ public BuildInfo(String path) {
year = properties.getProperty("year", "");
developers = properties.getProperty("developers", "");
azureInstrumentationKey = properties.getProperty("azureInstrumentationKey", "");
minRequiredJavaVersion = properties.getProperty("minRequiredJavaVersion", "1.8");
allowJava9 = "true".equals(properties.getProperty("allowJava9", ""));
}

public Version getVersion() {
Expand All @@ -66,4 +70,12 @@ public String getYear() {
public String getAzureInstrumentationKey() {
return azureInstrumentationKey;
}

public String getMinRequiredJavaVersion() {
return minRequiredJavaVersion;
}

public boolean isAllowJava9() {
return allowJava9;
}
}
76 changes: 76 additions & 0 deletions src/main/java/org/jabref/logic/util/JavaVersion.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package org.jabref.logic.util;

import java.util.Scanner;
import java.util.regex.Pattern;

/**
* Provides simple checks to ensure the correct version for JabRef is available. Currently, we need to make sure that we
* have Java 1.8 but not Java 9. The functions here are not intended for direct use. Instead, they are called inside
* {@link BuildInfo}, which has the required java version string (e.g. 1.8.0_144) available through the build system.
* The version check should always happen through the <code>Globals#BUILD_INFO</code> instance which is available at a
* very early stage in the JabRef startup.
*/
public class JavaVersion {

// See http://openjdk.java.net/jeps/223
private static final Pattern DELIMITER = Pattern.compile("[._\\-+]");
private final String JAVA_VERSION;

public JavaVersion() {
// Be adventurous and assume that we can always access this property!
JAVA_VERSION = System.getProperty("java.version");
}

public JavaVersion(final String version) {
JAVA_VERSION = version;
}

/**
* Tries to determine if we are running on Java 9. This test should return false, when we cannot extract the correct
* Java version. Note that Java 9 has a different version scheme like "9-internal".
*
* @return true if Java 9 is used
*/
public boolean isJava9() {
if (JAVA_VERSION != null) {
// Since isAtLeast is very optimistic, we first need to check if we have a "number" in the version string
// at all. Otherwise we would get false-positives.
final Scanner scanner = new Scanner(JAVA_VERSION);
scanner.useDelimiter(DELIMITER);
if (scanner.hasNextInt()) {
return isAtLeast("1.9");
}
}
return false;
}

/**
* A very optimistic test for ensuring we at least have a minimal required Java version. It will not fail when we
* cannot determine the result. In essence, this method splits a version string using {@link
* JavaVersion#DELIMITER} and compares two version number by number.
*
* @param version Should be in the form X.X.X_XXX where X are integers.
* @return true if the numbers in version available for comparison are all greater-equals the currently running Java
* version.
*/
public boolean isAtLeast(final String version) {
if (JAVA_VERSION == null || version == null) {
return true;
}
final Scanner scannerRunningVersion = new Scanner(JAVA_VERSION);
final Scanner scannerRequiredVersion = new Scanner(version);
scannerRunningVersion.useDelimiter(DELIMITER);
scannerRequiredVersion.useDelimiter(DELIMITER);
while (scannerRunningVersion.hasNextInt() && scannerRequiredVersion.hasNextInt()) {
final int running = scannerRunningVersion.nextInt();
final int required = scannerRequiredVersion.nextInt();
if (running == required) continue;
return running >= required;
}
return true;
}

public String getJavaVersion() {
return JAVA_VERSION;
}
}
2 changes: 2 additions & 0 deletions src/main/resources/build.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@ year=${year}
authors=${authors}
developers=${developers}
azureInstrumentationKey=${azureInstrumentationKey}
minRequiredJavaVersion = ${minRequiredJavaVersion}
allowJava9 = ${allowJava9}
3 changes: 3 additions & 0 deletions src/main/resources/l10n/JabRef_da.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2350,3 +2350,6 @@ Show_console_output_(only_necessary_when_the_launcher_is_used)=
Remove_line_breaks=
Removes_all_line_breaks_in_the_field_content.=
Checking_integrity...=

Note_that_currently,_JabRef_does_not_run_with_Java_9.=
Your_current_Java_version_(%0)_is_not_supported._Please_install_version_%1_or_higher.=
3 changes: 3 additions & 0 deletions src/main/resources/l10n/JabRef_de.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2350,3 +2350,6 @@ Show_console_output_(only_necessary_when_the_launcher_is_used)=Anzeigen_der_Kons
Remove_line_breaks=Entfernen_der_Zeilenumbrüche
Removes_all_line_breaks_in_the_field_content.=Entfernen_aller_Zeilenumbrüche_im_Inhalt_des_Feldes.
Checking_integrity...=

Note_that_currently,_JabRef_does_not_run_with_Java_9.=JabRef_kann_nicht_mit_Java_9_verwendet_werden.
Your_current_Java_version_(%0)_is_not_supported._Please_install_version_%1_or_higher.=Die_verwendete_Java_Installation_(%0)_wird_nicht_unterstützt._Bitte_installieren_Sie_Version_%1_oder_neuer.
3 changes: 3 additions & 0 deletions src/main/resources/l10n/JabRef_el.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2350,3 +2350,6 @@ Show_console_output_(only_necessary_when_the_launcher_is_used)=
Remove_line_breaks=
Removes_all_line_breaks_in_the_field_content.=
Checking_integrity...=

Note_that_currently,_JabRef_does_not_run_with_Java_9.=
Your_current_Java_version_(%0)_is_not_supported._Please_install_version_%1_or_higher.=
3 changes: 3 additions & 0 deletions src/main/resources/l10n/JabRef_en.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2350,3 +2350,6 @@ Show_console_output_(only_necessary_when_the_launcher_is_used)=Show_console_outp
Remove_line_breaks=Remove_line_breaks
Removes_all_line_breaks_in_the_field_content.=Removes_all_line_breaks_in_the_field_content.
Checking_integrity...=Checking_integrity...

Note_that_currently,_JabRef_does_not_run_with_Java_9.=Note_that_currently,_JabRef_does_not_run_with_Java_9.
Your_current_Java_version_(%0)_is_not_supported._Please_install_version_%1_or_higher.=Your_current_Java_version_(%0)_is_not_supported._Please_install_version_%1_or_higher.
3 changes: 3 additions & 0 deletions src/main/resources/l10n/JabRef_es.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2350,3 +2350,6 @@ Show_console_output_(only_necessary_when_the_launcher_is_used)=
Remove_line_breaks=
Removes_all_line_breaks_in_the_field_content.=
Checking_integrity...=

Note_that_currently,_JabRef_does_not_run_with_Java_9.=
Your_current_Java_version_(%0)_is_not_supported._Please_install_version_%1_or_higher.=
3 changes: 3 additions & 0 deletions src/main/resources/l10n/JabRef_fa.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2350,3 +2350,6 @@ Show_console_output_(only_necessary_when_the_launcher_is_used)=
Remove_line_breaks=
Removes_all_line_breaks_in_the_field_content.=
Checking_integrity...=

Note_that_currently,_JabRef_does_not_run_with_Java_9.=
Your_current_Java_version_(%0)_is_not_supported._Please_install_version_%1_or_higher.=
3 changes: 3 additions & 0 deletions src/main/resources/l10n/JabRef_fr.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2350,3 +2350,6 @@ Show_console_output_(only_necessary_when_the_launcher_is_used)=Afficher_la_sorti
Remove_line_breaks=Supprimer_les_sauts_de_ligne
Removes_all_line_breaks_in_the_field_content.=Supprime_tous_les_sauts_de_ligne_du_contenu_d'un_champ
Checking_integrity...=

Note_that_currently,_JabRef_does_not_run_with_Java_9.=
Your_current_Java_version_(%0)_is_not_supported._Please_install_version_%1_or_higher.=
3 changes: 3 additions & 0 deletions src/main/resources/l10n/JabRef_in.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2350,3 +2350,6 @@ Show_console_output_(only_necessary_when_the_launcher_is_used)=
Remove_line_breaks=
Removes_all_line_breaks_in_the_field_content.=
Checking_integrity...=

Note_that_currently,_JabRef_does_not_run_with_Java_9.=
Your_current_Java_version_(%0)_is_not_supported._Please_install_version_%1_or_higher.=
3 changes: 3 additions & 0 deletions src/main/resources/l10n/JabRef_it.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2350,3 +2350,6 @@ Show_console_output_(only_necessary_when_the_launcher_is_used)=
Remove_line_breaks=
Removes_all_line_breaks_in_the_field_content.=
Checking_integrity...=

Note_that_currently,_JabRef_does_not_run_with_Java_9.=
Your_current_Java_version_(%0)_is_not_supported._Please_install_version_%1_or_higher.=
3 changes: 3 additions & 0 deletions src/main/resources/l10n/JabRef_ja.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2350,3 +2350,6 @@ Show_console_output_(only_necessary_when_the_launcher_is_used)=
Remove_line_breaks=
Removes_all_line_breaks_in_the_field_content.=
Checking_integrity...=

Note_that_currently,_JabRef_does_not_run_with_Java_9.=
Your_current_Java_version_(%0)_is_not_supported._Please_install_version_%1_or_higher.=
3 changes: 3 additions & 0 deletions src/main/resources/l10n/JabRef_nl.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2350,3 +2350,6 @@ Show_console_output_(only_necessary_when_the_launcher_is_used)=
Remove_line_breaks=
Removes_all_line_breaks_in_the_field_content.=
Checking_integrity...=

Note_that_currently,_JabRef_does_not_run_with_Java_9.=
Your_current_Java_version_(%0)_is_not_supported._Please_install_version_%1_or_higher.=
3 changes: 3 additions & 0 deletions src/main/resources/l10n/JabRef_no.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2350,3 +2350,6 @@ Show_console_output_(only_necessary_when_the_launcher_is_used)=
Remove_line_breaks=
Removes_all_line_breaks_in_the_field_content.=
Checking_integrity...=

Note_that_currently,_JabRef_does_not_run_with_Java_9.=
Your_current_Java_version_(%0)_is_not_supported._Please_install_version_%1_or_higher.=
3 changes: 3 additions & 0 deletions src/main/resources/l10n/JabRef_pt_BR.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2350,3 +2350,6 @@ Show_console_output_(only_necessary_when_the_launcher_is_used)=
Remove_line_breaks=
Removes_all_line_breaks_in_the_field_content.=
Checking_integrity...=

Note_that_currently,_JabRef_does_not_run_with_Java_9.=
Your_current_Java_version_(%0)_is_not_supported._Please_install_version_%1_or_higher.=
3 changes: 3 additions & 0 deletions src/main/resources/l10n/JabRef_ru.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2350,3 +2350,6 @@ Show_console_output_(only_necessary_when_the_launcher_is_used)=
Remove_line_breaks=
Removes_all_line_breaks_in_the_field_content.=
Checking_integrity...=

Note_that_currently,_JabRef_does_not_run_with_Java_9.=
Your_current_Java_version_(%0)_is_not_supported._Please_install_version_%1_or_higher.=
3 changes: 3 additions & 0 deletions src/main/resources/l10n/JabRef_sv.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2350,3 +2350,6 @@ Show_console_output_(only_necessary_when_the_launcher_is_used)=
Remove_line_breaks=
Removes_all_line_breaks_in_the_field_content.=
Checking_integrity...=

Note_that_currently,_JabRef_does_not_run_with_Java_9.=
Your_current_Java_version_(%0)_is_not_supported._Please_install_version_%1_or_higher.=
3 changes: 3 additions & 0 deletions src/main/resources/l10n/JabRef_tr.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2350,3 +2350,6 @@ Show_console_output_(only_necessary_when_the_launcher_is_used)=Consol_çıktıs
Remove_line_breaks=
Removes_all_line_breaks_in_the_field_content.=
Checking_integrity...=

Note_that_currently,_JabRef_does_not_run_with_Java_9.=
Your_current_Java_version_(%0)_is_not_supported._Please_install_version_%1_or_higher.=
3 changes: 3 additions & 0 deletions src/main/resources/l10n/JabRef_vi.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2350,3 +2350,6 @@ Show_console_output_(only_necessary_when_the_launcher_is_used)=
Remove_line_breaks=
Removes_all_line_breaks_in_the_field_content.=
Checking_integrity...=

Note_that_currently,_JabRef_does_not_run_with_Java_9.=
Your_current_Java_version_(%0)_is_not_supported._Please_install_version_%1_or_higher.=
3 changes: 3 additions & 0 deletions src/main/resources/l10n/JabRef_zh.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2350,3 +2350,6 @@ Show_console_output_(only_necessary_when_the_launcher_is_used)=
Remove_line_breaks=
Removes_all_line_breaks_in_the_field_content.=
Checking_integrity...=

Note_that_currently,_JabRef_does_not_run_with_Java_9.=
Your_current_Java_version_(%0)_is_not_supported._Please_install_version_%1_or_higher.=
Loading

0 comments on commit c1d425a

Please sign in to comment.