简体   繁体   English

如何可靠地找到Java的rt.jar或同等的?

[英]How to reliably locate Java's rt.jar or equivalent?

How can one reliably locate the jar containing Java's bootstrap classes (rt.jar or equivalent)? 如何可靠地找到包含Java的引导类(rt.jar或等效类)的jar? I was using the below code, but I've discovered that JAVA_HOME is used to refer to the JDK, not the JRE, and will fail entirely if there is no JDK installed. 我使用下面的代码,但我发现JAVA_HOME用于引用JDK而不是JRE,如果没有安装JDK,它将完全失败。

def findJRE():
    try:
        home = os.environ['JAVA_HOME']
        path = os.path.join(home, 'jre', 'lib', 'rt.jar')
        if os.path.isfile(path):
            return path

        #For macs
        path = os.path.join(home, 'bundle', 'Classes', 'classes.jar')
        if os.path.isfile(path):
            return path
    except Exception as e:
        pass

As the location and name of the file differs between platforms and it seems there is no environment variable pointing to it I suppose that your best bet is to look for the file in the filesystem: 由于文件的位置和名称在平台之间有所不同,似乎没有环境变量指向它,我想你最好的办法是在文件系统中查找文件:

#!/usr/bin/env python
import subprocess
import re

rtjarPaths = subprocess.check_output(["locate", "rt.jar"])
paths = re.findall('^.*/jre/.*$', rtjarPaths, re.M)
print paths

vicent@deckard:~$ python findrt.py 
['/usr/lib/jvm/java-6-openjdk/jre/lib/rt.jar']

The above code works fine for me on my Ubuntu box. 上面的代码在我的Ubuntu盒子上工作正常。 It can be easily extended for working on MacOS X too. 它也可以轻松扩展,以便在MacOS X上运行。 Other linux distros may require to adapt the code too. 其他Linux发行版也可能需要调整代码。

Update: After some googling I've found in the oracle documentation that the rt.jar file is located in the path stored in the sun.boot.class.path system property. 更新:经过一些谷歌搜索后,我在oracle文档中发现rt.jar文件位于存储在sun.boot.class.path系统属性中的路径中。 Unfortunately I don't know how to get this property directly from Python or from the command line so I can only provide the following dirty alternative to the previous code. 不幸的是我不知道如何直接从Python或命令行获取此属性,因此我只能提供以下代码的脏替代。

Create and compile PropertiesTest.java: 创建并编译PropertiesTest.java:

public class PropertiesTest {
    public static void main(String[] args)
        throws Exception {
        String value = System.getProperty("sun.boot.class.path");
        System.out.println(value);
    }
}

Then execute the following Python script: 然后执行以下Python脚本:

#!/usr/bin/env python
import subprocess
import re

jrePaths = subprocess.check_output(["java", "PropertiesTest"])
rt = re.findall('(?:.*:)?(.*/(?:rt|classes)\.jar):?', jrePaths)
print rt

which should work on both Linux and MacOS X platforms. 哪个应该适用于Linux和MacOS X平台。 On my Ubuntu system it gives the output 在我的Ubuntu系统上,它给出了输出

['/usr/lib/jvm/java-6-openjdk/jre/lib/rt.jar']

I like Vincent's more principled answer , but here is an even simpler hack: 我喜欢文森特更有原则性的答案 ,但这里有一个更简单的黑客:

java -verbose 2>/dev/null | sed -ne '1 s/\[Opened \(.*\)\]/\1/p'

prints the absolute path to rt.jar . 打印到rt.jar的绝对路径。

Tested with Sun Java 6 and 8 on Linux and Windows (Cygwin), and Sun Java 6 on OS X (where rt.jar is instead called classes.jar ). 在Linux和Windows(Cygwin)上测试Sun Java 6和8,在OS X上测试Sun Java 6(其中rt.jar称为classes.jar )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM