[英]Resolving classpath-relative paths for non-existing files
I have a project where I'm compiling files to locations relative to getClass().getResource("/")
. 我有一个项目,正在将文件编译到相对于getClass().getResource("/")
。 How can I get File
objects for these locations? 如何获得这些位置的File
对象?
When I try getClass().getResource("/nonExisting")
, they return null. 当我尝试getClass().getResource("/nonExisting")
,它们返回null。 How can I resolve the paths? 如何解决路径?
There's no reason to expect existent resources to be normal files; 没有理由期望现有资源是普通文件。 they could be buried inside a jar-file somewhere on your classpath. 它们可能被埋在类路径中某个位置的jar文件中。
With non-existent resources, the situation is even worse; 如果没有资源,情况就更糟了。 even if you could guarantee in the abstract that it is a non-existent normal file, it could be a non-existent normal file in any of the top-level directories in your classpath; 即使您可以抽象地保证它是不存在的普通文件,也可以是您的类路径中任何顶级目录中的不存在的普通文件; there's no sensible way to decide which directory it would have been in, had it existed. 有决定它会一直在目录中没有合理的方式, 如果它存在。 (For example, if your classpath contains both classes
and testClasses
, then /nonExisting
could be either classes/nonExisting
or testClasses/nonExisting
.) (例如,如果您的类路径同时包含classes
和testClasses
,则/nonExisting
可以是classes/nonExisting
或testClasses/nonExisting
。)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.