0

I have created a Java project application in eclipse and i am using the JRE System Library[JavaSE-1.7].

when i make an executable jar file of the application then test it on different computers which have different Java versions. The problem is that the jar file only works on some of the computers because of the java version.

my question is: is there away to make the application to check the computer see which java version it has and just use that version instead of the java version that was used to implement the project? in other words can you make an application which is not java version dependent?

any suggestion is of great help.

thanks


Thanks for all the suggestions and comments. But i think i'll just stick with the java 7 version and let the user know that they need java 7 to run it.

  • 2
    You should have no problem making it run with any Java 7 compliant JVM. What do you mean by "doesn't work" anyway? Any messages? – fge Mar 01 '14 at 11:36
  • 3
    If you would want to make it in any slightly reasonable Java version, you would need to fully write and compile it in Java 5... Therefore I suggest that you find others way to ensure that your clients run Java 7, because that really is the most reasonable version at the moment. – skiwi Mar 01 '14 at 11:37
  • @skiwi Better yet, Java 8 is about to be unleashed. :-) – Hungry Blue Dev Mar 01 '14 at 11:41
  • @fge the application works fine on any computer with Java 7. but some older version doesnt let the jar to work...by this i mean when you click the jar file it displays a Java VM Launcher dialog message saying.."Could not find the main class: Main.main.Program will exit." - on this computer i checked the version and it had version 51.0 – user3232384 Mar 01 '14 at 11:42
  • @skiwi the application is for any one so if they run it on a java version that is not compatible then the jar wont work. – user3232384 Mar 01 '14 at 11:45
  • Well, "downgrade" the language level to, say, 6 and compile for Java 6. You may have to modify your code, though. – fge Mar 01 '14 at 11:53
  • You have to compile your Java application using the lowest Java version you want it to run on. If some of the computers are running the JRE 5, then you have to compile at Java version 5. – Gilbert Le Blanc Mar 01 '14 at 11:59

2 Answers2

1

No, there's no way to check that, not in the way I think you mean.
In fact, on the one hand you have the Java version in which you compile the class (which you can choose), and on the other hand the Java version (of the JVM) in which your application runs (which you cannot choose).

Well, I do believe there's a way to check the Java version through some method call or similar, but I don't think it would help you here, I mean that I guess you can't do something like:

if(java-7) {
    //do try-with-resources
} else {
    // do usual try-statement
}

As @skiwi suggested, make sure your application compiles against an older Java version, and you should be fine with JVMs of that version and beyond. I'm not sure what's causing your ClassNotFound error, but it could be that the JVM checks the required Java version needed and stops if it is above the available.

If you compile from the command line, there are switches to tell javac the source Java version and the target Java versions. I never had used them, so you better search for questions about them (i.e. javac source and target options). With those tools it will be the compiler that warns you against problems, without you having to explicitly check the version in your code.

Community
  • 1
  • 1
watery
  • 5,026
  • 9
  • 52
  • 92
0

There is a way to check the Java version, but you would need multiple executables to do it.

You would need a root executable, that would have a low-bar minimum Java version, simply so it can check the version of the systems, then it would launch an executable from there based on the system's Java version.

How you would come about this:

System.getProperty() allows you to, well, get system properties, along with a few other things, like versions of installed applications like Java.

You should create a separate JAR with one class:

public class Launcher {

 public static void main(String[] args) {

      String javaVersion = System.getProperty("java.version");

      // Conditional to launch Jar file

      if(correctVersion) { // if version is confirmed

           Runtime.getRuntime().exec("java -jar whatever.jar");

      }

 }

}

The reason we need a root, lower bar executable, is because if the original executable can't run in the first place, how is it going to check the Java version?

Hope this works out for you. Sorry it took so long to get an answer.

Galen Nare
  • 376
  • 2
  • 4
  • 18