Java 将包名转换为路径

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1921975/
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-08-13 01:27:58  来源:igfitidea点击:

convert package name to path

java

提问by rocksportrocker

I have a class de.xyz.MyClasswhich is stored in de/xyz/MyClass.java. How can I get this path from the class MyClassitself?

我有一个de.xyz.MyClass存储在de/xyz/MyClass.java. 我怎样才能从类MyClass本身获得这条路径?

回答by Peter Lang

Try

尝试

this.getClass().getPackage().getName();

回答by T.J. Crowder

Like this:

像这样:

String path = MyClass.class.getName().replace(".", "/") + ".java";

But note that sometimes class names are annotated (e.g., inner classes, etc.). More here.

但请注意,有时类名会被注释(例如,内部类等)。更多在这里

If you're starting with an instance of the class (perhaps thisinside one of the class's methods), you can get the class via instance.getClass(). For instance:

如果您从类的实例开始(可能this在类的方法之一中),则可以通过instance.getClass(). 例如:

private String getPathToThis() {
    return this.getClass().getName().replace(".", "/") + ".java";
}

Of course, these are going to be relative to some base (directory or jar) in the classpath...

当然,这些将与类路径中的某个基础(目录或 jar)相关...

回答by OscarRyz

I'm not sure if this answer you question so I'll do my best guess:

我不确定这是否能回答你的问题,所以我会尽力猜测:

package de.xyz;

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

Which when compiled and run gives:

编译和运行时给出:

C:\Users\Oscar\code>javac -d . PathForClass.java

C:\Users\Oscar\code>java -cp . de.xyz.PathForClass
file:/C:/Users/Oscar/code/de/xyz/PathForClass.class

That way you can modify a little the calls and have the path as you want:

这样你就可以稍微修改一下调用并根据需要设置路径:

package de.xyz;

class PathForClass {
    public static void main( String [] args ) {
        String currentDir  = new java.io.File(".").toURI().toString();
        String pathToClass = PathForClass.class.getResource( "PathForClass.class" ).toString();
        System.out.println( pathToClass.substring( currentDir.length() - 2  )); // -2 to get rid of the /./ at the end of the current dir
    }
}

C:\Users\Oscar\code>java -cp . de.xyz.PathForClass
de/xyz/PathForClass.class

Bear in mind that will only work where the classes are stored in directories, for jars you'll have to modify it a little bit ( if( pathToClass.startsWith("jar")){ //etc...)

请记住,这仅适用于类存储在目录中的情况,对于 jar,您必须稍微修改它 ( if( pathToClass.startsWith("jar")){ //etc...)

Also, class loaders don't always work with the filesystem, some of them load files from databases, ldaps and pretty much anywhere, so is not always true that for de.xyz.MyClassthere exists a de/xyz/MyClass.classfile ( as shown with the jar example)

此外,类加载器并不总是与文件系统一起工作,其中一些从数据库、ldaps 和几乎任何地方加载文件,因此并不总是de.xyz.MyClass存在de/xyz/MyClass.class文件(如 jar 示例所示)

I hope you find this useful.

希望这个对你有帮助。

回答by Dara

There is an util class ==> org.springframework.util.ClassUtils.

有一个 util 类 ==> org.springframework.util.ClassUtils。

ClassUtils.convertClassNameToResourcePath("de.xyz.MyClass");

Source : https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/util/ClassUtils.html

来源:https: //docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/util/ClassUtils.html