I wrote the Swing program in Java 8. It, of course, does not run on machines which have Java 7 on them. Is there a way to prompt the user, when he or she tries to run the .jar file, that Java 8 is required to run the program, preferably, by showing the dialog window with this prompt.
-
You can check the version on startup by reading a system property `System.getProperty("java.version")` – toniedzwiedz May 08 '14 at 15:42
-
4@Tom But the application won't start.. – Duncan Jones May 08 '14 at 15:43
-
If the program was compiled with Java 8 as target then it wont even run. – Leonard Brünings May 08 '14 at 15:44
-
2@Duncan you can use this anyway by building a launcher program of some sort that is compatible with lower versions and then use it to run the actual app. Sorry if that wasn't obvious from the context. – toniedzwiedz May 08 '14 at 15:46
-
possible duplicate of [How to check JRE version prior to launch?](http://stackoverflow.com/questions/222187/how-to-check-jre-version-prior-to-launch) – toniedzwiedz May 08 '14 at 15:46
-
You could also just post Java 8 as a system requirement :) – David says Reinstate Monica May 08 '14 at 15:48
3 Answers
Create a new java 7 file that checks the users java version using System.getProperty("java.version") and then if it comes back as 8 then the java 7 file can start the java 8 file, if not it can send out a dialog box telling the user to upgrade.

- 377
- 6
- 26
-
5I'd actually recommend to lower the requirement to Java 5 for that simple program, to enhance compatability. – skiwi May 08 '14 at 16:04
-
-
@user3507225 `When did java start supporting Swing? ` The Comment You have Written above is Kind of Strange. You probably need to google what SWING is. – Stanley Mungai May 08 '14 at 16:13
-
Not a fan of this option. How old a version must you go back? 7? 6? 5? 4? Those versions are likely to be EOL, and who wants to support 2 versions of java for each app they write? When the decision has been made to upgrade your version of Java, tear off the rear-view mirror. – splungebob May 08 '14 at 16:31
-
@user3507225: Java includes Swing since version `1.2`. But of course, you would have to check whether all API classes and methods you are using for you message dialog are included in that version to lower the minimum requirement to that version. So you would have to find an API reference for that version… – Holger May 08 '14 at 16:31
-
@splungebob: even jdk8 `javac` still supports `-source 1.2 -target 1.2` though it prints a warning that it won’t do in a future release. You only have to care not to use classes/members that didn’t exist in the old version when using a newer version to compile the fall-back. – Holger May 08 '14 at 16:37
-
@Holger That's seems undesirable to me. So I have to write a launcher jar using only Java 1.2 classes? I wouldn't even know what they are since I don't have that JDK. Same for Java 3, 4, 5, and 6. And now I can't use any classes in my framework for fear that they may use or have a class that uses Generics, for example. So I have to re-write any library calls just so they conform to an arbitrary old version of Java? No thank you. – splungebob May 08 '14 at 16:46
-
Any decent IDE will automatically warn you (with errors before compiling) if something is not supported in the IDE you are targetting. It might require you to install the appropiate JDK for full support though. In the end it's just a tradeoff, do you rather want people to be pissed about the application not working (and losing potential clients), or do you write an application for java 5 once and then never return to it again. – skiwi May 08 '14 at 16:53
-
@skiwi I agree with you there, if you're going to use this method you might as well use the oldest version of java possible – man-qa May 09 '14 at 07:45
-
1@splungebob: the important question is what the front-end class ought to support. For a desktop application to show an error message telling that the JRE is too old, you only need `System.getProperty` and Swing’s `JOptionPane`; both exist in `1.2` and did not undergo substantial changes since then. More sophistication apps might need `ResourceBundle`s and even they work the same from `1.2` to `5`. It could be a single class written once and used forever. Compare to how every Windows program today still contains small code at the front showing an error message when started on MS-DOS. – Holger May 09 '14 at 09:03
If I remeber correctly, Launch4J could check for the correct JRE and would probably show some error message if the JRE could not be found. However, I do not know whether it works with Java 8.
This would also only work for Windows though.

- 1,362
- 1
- 8
- 15
I don't like the idea of a 2nd "launcher" jar/class that's written/compatible with a lower version of Java. That means that I have to keep this old version around indefinitely, which may be problematic since:
- I need to know how old a version the launcher jar must be to cover all potential users.
- I now need to support 2 versions of Java for my builds.
- The old version may be insecure, and possibly EOL, with no hope of patches/fixes.
- Java started embedding expiration dates in the version.
Consider launching your app via Java Web Start. This works on all platforms (I believe), and will handle the version checking for you.
Another alternative is to write a wrapper script (not written in Java) that launches your app. It could check for the presence and value of an environment variable (perhaps JAVA_HOME).

- 5,357
- 2
- 22
- 45
-
You don’t need two jars. A single start class within the same jar would be enough. – Holger May 08 '14 at 16:40