如题。机器安装1.6的jdk。项目中用1.4的jdk。用System.getProperty("java.version")获取的是安装的jdk版本号(1.6)。现在要获取项目的jdk版本号(1.4),如何获取?

解决方案 »

  1.   

    look
      

  2.   

    一般做项目都知道要用什么版本的jdk(jre)吧?
    如果不知道那还咋开发?不能有人用1.5,有人用1.4吧?如果实在不知道那就只能看class文件的编译信息了,百度一下就能找到:引用
    http://www.rgagnon.com/javadetails/java-0544.html 
    The first 4 bytes are a magic number, 0xCAFEBABe, to identify a valid class file then the next 2 bytes identify the class format version (major and minor). Possible major/minor value : major minor Java platform version 
    45       3           1.0 
    45       3           1.1 
    46       0           1.2 
    47       0           1.3 
    48       0           1.4 
    49       0           1.5 
    50       0           1.6 import java.io.*; public class ClassVersionChecker { 
        public static void main(String[] args) throws IOException { 
            for (int i = 0; i < args.length; i++) 
                checkClassVersion(args[i]); 
        }     private static void checkClassVersion(String filename) 
            throws IOException 
        { 
            DataInputStream in = new DataInputStream 
             (new FileInputStream(filename));         int magic = in.readInt(); 
            if(magic != 0xcafebabe) { 
              System.out.println(filename + " is not a valid class!");; 
            } 
            int minor = in.readUnsignedShort(); 
            int major = in.readUnsignedShort(); 
            System.out.println(filename + ": " + major + " . " + minor); 
            in.close(); 
        } 
    } > java ClassVersionChecker ClassVersionChecker.class 
    ClassVersionChecker.class: 49 . 0 如果连class都没有,那啥也别说了。
      

  3.   

    System.getProperties().getProperty("java.vm.version"),就是当前使用的JDK的版本号吧。
      

  4.   

    这个
    你可以在打包jar的时候将编译项目的jdk版本写到META-INF中。在build文件中写,应该可以实现的,I believe.
      

  5.   

    你点击myeclipse菜单中的window→preferences→java→Installed JREs→然后选择你要用的jre版本
      

  6.   

    直接 在jre/bin下执行 java -version 即可