我的 Java 类是从文件系统的哪个位置加载的?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/779650/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 13:45:03  来源:igfitidea点击:

Where on the file system was my Java class loaded from?

javadebuggingclassloader

提问by shsteimer

I think this is a situation every Java programmer runs into if they do it long enough. You're doing some debugging and make a change to class. When you go to re-run the program, these changes don't seem to be picked up but rather the old class still seems to be running. You clean and rebuild everything, same issue. Sometimes, this can come down to a classpath issue, of the same class being on the classpath more than once, but there doesn't seem to be an easy way to figure out where the class being loaded is coming from...

我认为这是每个 Java 程序员如果做足够长的时间都会遇到的情况。您正在进行一些调试并对类进行更改。当您重新运行程序时,这些更改似乎没有被拾取,而是旧类似乎仍在运行。你清理并重建一切,同样的问题。有时,这可能归结为类路径问题,同一类在类路径上不止一次,但似乎没有一种简单的方法来确定加载的类来自哪里......

Is there any way to find the file path for the class that was loaded? Preferable something that would work either if the class was loaded from a .classfile or a .jarfile. Any Ideas?

有没有办法找到加载的类的文件路径?如果类是从.class文件或.jar文件加载的,那么最好是一些可以工作的东西。有任何想法吗?

回答by Joachim Sauer

Simply run javausing the standard command line switch"-verbose:class" (see the javadocumentation). This will print out each time a class is loaded and tell you where it's loaded from.

简单地运行java使用标准命令行开关” -verbose:class“(见java文档)。这将在每次加载类时打印出来并告诉您它是从哪里加载的。

回答by Chris Thornhill

If you wanted to do it programmatically from inside the application, try:

如果您想从应用程序内部以编程方式执行此操作,请尝试:

URL loc = MyClass.class.getProtectionDomain().getCodeSource().getLocation();

(Note, getCodeSource() may return null, so don't actually do this all in one line :) )

(注意,getCodeSource() 可能会返回 null,所以实际上不要在一行中完成所有操作:))

回答by avalys

public static URL getClassURL(Class klass) {
    String name = klass.getName();
    name = "/" + convertClassToPath(name);
    URL url = klass.getResource(name);
    return url;
}

public static String convertClassToPath(String className) {
    String path = className.replaceAll("\.", "/") + ".class";
    return path;
}

Just stick this somewhere, and pass it the Class object for the class you want to find the definition of. It should work regardless of where it called from, since it calls getResource() on the class being searched for.

只需将其粘贴在某处,然后将要查找其定义的类的 Class 对象传递给它。无论从何处调用它都应该可以工作,因为它会在要搜索的类上调用 getResource()。

public static void main(String[] args) {
    System.out.println(getClassURL(String.class));       
}

Sample output: jar:file:/System/Library/Frameworks/JavaVM.framework/Versions/1.6.0/Classes/classes.jar!/java/lang/String.class

示例输出:jar:file:/System/Library/Frameworks/JavaVM.framework/Versions/1.6.0/Classes/classes.jar!/java/lang/String.class

回答by lothar

As the class needs to come from somewhere in the class path, I would recommend to simply print the class pathand check if there's an older version of you class somewhere.

由于班级需要来自班级路径中的某个位置,我建议您只需打印班级路径并检查某处是否有您班级的旧版本。