6

I have a jar file compiled using jdk 1.7, I'd like to check during load time that the java runtime environment my jar is running under is 1.7 or newer. Is there a way to do that?

I tried using System.getProperty("java.version") but that didn't help since my class version was 51 and jre 1.6 refuse to load it.

user2512495
  • 91
  • 1
  • 5
  • 6
    As you already observed you can only run this with 1.7 or higher. You can run a jar compiled for 1.6 with 1.7, but not the other way around. Hence, your check is superfluous. – Ingo Jun 27 '13 at 15:49
  • 2
    For more info regarding 1.7 and compatibility check out http://www.oracle.com/technetwork/java/javase/compatibility-417013.html – Rafael Oltra Jun 27 '13 at 15:52

4 Answers4

7

I have the same requirement and I solved it the following way:

Have a "startup" class that has absolutely no dependencies (=imports) to any new JDK class or to any of your application's classes. You man not import your main class by name!

Something like:

Inside that starter class, check the Java version that you need.

If the check is successful then load your main class using reflection and start it's main method (or whichever method you use). Something like this:

public class MyStarter
{
   public static void main(String[] args)
   {
      String version = System.getProperty("java.version", null);
      boolean isVersionOK = ... ; 

      if (!isVersionOK)
      {
         System.err.println("Wrong Java version detected");
         // or display some messagebox using JOptionPane
         System.exit(1);
      }

      Class mainClass = Class.forName("com.foo.MyMain");
      Method main = mgr.getDeclaredMethod("main", new Class[] { String[].class });
      main.invoke(null, new Object[] { args });
   }
}

Again: make sure MyStarter does not contain any imports to your application or classes that are not available in Java 1.2 (or whatever Java version you will target).

Then compile MyStarter (and only that class) with -source 1.2 -target 1.2 Compile the rest of your classes with the regular compiler (e.g. creating Java7 .class files).

If you generate an executable jar, then add com.foo.MyStarter as the Main-Class: attribute.

My the "compile" target in my Ant build.xml looks something like this:

<-- compile the starter class for Java 1.2 -->
<javac destdir="${build}"
       srcdir="${src}"
       target="1.2"
       source="1.2"
       encoding="ISO-8859-1"
       includeantruntime="false"
       includes="com/foo/MyStarter.java"/>  

<-- compile the rest with for Java 1.6 -->
<javac destdir="${build}"
       srcdir="${src}"
       target="1.6"
       source="1.6"
       includeantruntime="false"
       encoding="ISO-8859-1">
  <exclude name="com/foo/MyStarter.java"/>
  <classpath>
      ....
  </classpath>
</javac>

Then put everything into one jar file.

Esko Luontola
  • 73,184
  • 17
  • 117
  • 128
6

Sadly if it's compiled on 1.7 it won't run on anything older... so no code that you add will actually run (sad face). You could compile it against an older version using the -target 1.6 flag. But it seems from your post that you need the functionality of 1.7. So perhaps you could write a "wrapper" application that was compiled against an old version of Java, did your check and then loaded and ran your jar - or perhaps you could achieve the same thing using some kind of script.

See this question also.

Community
  • 1
  • 1
selig
  • 4,834
  • 1
  • 20
  • 37
0

You can compile the jar specifying the older version. Check this link for further detail.

Manisha Mahawar
  • 627
  • 6
  • 9
0

Consider deploying your software using Java Web Start technology. You can then specify in the jnlp file the exact java version supported by your application.

Specifically the minimum java version is declared in the j2se element.

For example:

<?xml version="1.0" encoding="UTF-8"?>
<jnlp spec="1.0+" codebase="" href="">
    <information>
        <title>Dynamic Tree Demo</title>
        <vendor>Dynamic Team</vendor>
        <icon href="sometree-icon.jpg"/>
        <offline-allowed/>
    </information>
    <resources>
        <!-- Application Resources -->
        <j2se version="1.6+" href=
           "http://java.sun.com/products/autodl/j2se"/>
        <jar href="DynamicTreeDemo.jar"
            main="true" />

    </resources>
    <application-desc
         name="Dynamic Tree Demo Application"
         main-class="webstartComponentArch.DynamicTreeApplication"
         width="300"
         height="300">
     </application-desc>
     <update check="background"/>
</jnlp>

See this link for more information