Verify java version compatibility

后端 未结 4 1368
一整个雨季
一整个雨季 2021-01-05 19:44

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

相关标签:
4条回答
  • 2021-01-05 20:03

    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.

    0 讨论(0)
  • 2021-01-05 20:05

    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.

    0 讨论(0)
  • 2021-01-05 20:11

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

    0 讨论(0)
  • 2021-01-05 20:22

    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

    0 讨论(0)
提交回复
热议问题