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

Checking Java version on startup #3418

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
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
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
45 changes: 45 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,52 @@ 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.
*/
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();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we are trying to move to JavaFX, would it be possible to display the error message with a FX window instead of a JFrame?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have a look at the FXDialogService.class. there are already some predefined dialogs for fx

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Siedlerchr @lenhard That was on purpose. I was using JFrame because what good will it be if I check the Java version, try to display a message and crash with an exception because FX is not available? 😄

Can we safely assume the FXDialog will always work?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@lenhard I made JabRef quit only when Java 9 is used. Otherwise it displays the message but continues

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@halirutan if no openjfx is avaiable it will crash nonetheless because the JabRef main inherits from Application (javafx.application.Application). That is the thing which you can't check for directly.
The only thing would be we use a "Pre-Main-Class" that does nit depend on javafx. But I am unsure if this would work

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Siedlerchr Ha.. overlooked this. My genius idea was to put the test at the very beginning before any of the fx stuff is instantiated. OK, I'm looking into fx dialog tonight.

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