验证java版本兼容性

验证java版本兼容性,java,Java,我有一个使用JDK1.7编译的jar文件,我想在加载期间检查jar运行的java运行时环境是否为1.7或更高版本。有办法吗 我试着使用System.getProperty(“java.version”),但没有用,因为我的类版本是51,jre 1.6拒绝加载它。遗憾的是,如果它是在1.7上编译的,它将不会在任何旧版本上运行。。。因此,您添加的代码实际上不会运行(悲伤的脸)。您可以使用-target 1.6标志根据旧版本编译它。但从你的帖子看来,你需要1.7的功能。因此,也许您可以编写一个“包装器

我有一个使用JDK1.7编译的jar文件,我想在加载期间检查jar运行的java运行时环境是否为1.7或更高版本。有办法吗


我试着使用System.getProperty(“java.version”),但没有用,因为我的类版本是51,jre 1.6拒绝加载它。

遗憾的是,如果它是在1.7上编译的,它将不会在任何旧版本上运行。。。因此,您添加的代码实际上不会运行(悲伤的脸)。您可以使用
-target 1.6
标志根据旧版本编译它。但从你的帖子看来,你需要1.7的功能。因此,也许您可以编写一个“包装器”应用程序,该应用程序根据旧版本的Java进行编译,进行检查,然后加载并运行jar——或者您可以使用某种脚本实现同样的功能


另请参见。

您可以编译指定旧版本的jar

我有同样的要求,我用以下方法解决了它:

拥有一个“startup”类,它对任何新的JDK类或任何应用程序类都没有依赖关系(=导入)。你不是按名字导入主类的人

比如:

在starter类中,检查所需的Java版本

如果检查成功,那么使用反射加载主类并启动它的main方法(或使用的任何方法)。大概是这样的:

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 });
   }
}
<-- 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> 再次:确保MyStarter不包含任何导入到您的应用程序或类的导入,这些导入在Java 1.2(或任何您将针对的Java版本)中都不可用

然后使用
-source 1.2-target 1.2
编译
MyStarter
(并且仅编译该类) 使用常规编译器编译其余的类(例如创建Java7.class文件)

如果生成一个可执行jar,那么添加
com.foo.MyStarter
作为
Main类:
属性

我的Ant
build.xml中的“compile”目标如下所示:

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 });
   }
}
<-- 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> ....
然后将所有内容放入一个jar文件中

考虑使用Java Web Start技术部署您的软件。然后,您可以在jnlp文件中指定应用程序支持的确切java版本

具体来说,最低java版本在j2se元素中声明

例如:

<?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>

动态树演示
动态团队

正如您所观察到的,您只能使用1.7或更高版本运行此功能。您可以用1.7运行针对1.6编译的jar,但不能反过来运行。因此,您的检查是多余的。有关1.7和兼容性的更多信息,请查看Nice-这就是我所说的包装应用程序:)