Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/solr/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 从class.getResource(";/";)生成URI时如何指定架构?_Java_Path_Uri_Nio - Fatal编程技术网

Java 从class.getResource(";/";)生成URI时如何指定架构?

Java 从class.getResource(";/";)生成URI时如何指定架构?,java,path,uri,nio,Java,Path,Uri,Nio,我试图建立一个静态实用程序类来提供相对于我的类的路径 public class FolderUtil { private static URI baseUri; public FolderUtil() throws URISyntaxException { baseUri = FolderUtil.class.getResource("/").toURI(); }; public static Path getBasePath(){

我试图建立一个静态实用程序类来提供相对于我的类的路径

public class FolderUtil {

    private static URI baseUri;
    public FolderUtil() throws URISyntaxException {
         baseUri =  FolderUtil.class.getResource("/").toURI();
    };

    public static Path getBasePath(){
        return Paths.get(baseUri).toAbsolutePath();
    }
}
我认为这是正确的。但是,当我在
getBasePath()
函数上运行JUnit测试时,它会抛出一个NPE

检查Paths类我看到了这一点(代码段不完整)

而URI类则表示:

public final class URI
    implements Comparable<URI>, Serializable
{

    // Note: Comments containing the word "ASSERT" indicate places where a
    // throw of an InternalError should be replaced by an appropriate assertion
    // statement once asserts are enabled in the build.

    static final long serialVersionUID = -6052424284110960213L;


    // -- Properties and components of this instance --

    // Components of all URIs: [<scheme>:]<scheme-specific-part>[#<fragment>]
    private transient String scheme;            // null ==> relative URI

但是它仍然在模式部分抛出一个NPE。

您想得到类似
System.getProperty(“user.dir”)
?@RomanVottner这会给我一个有效的路径吗?当然,确切地说,它返回应用程序的基本目录(=调用应用程序的目录)。关于原点问题:路径是否包含空格?对于一个有效的URL,空白应该是用% 20 F.E.<代码> C:\用户第一个名字“文件名\文件/代码>应该是类似于“代码>文件://用户/第一个名字%20LASTNEST/文件 @ RunvoToTnER,似乎工作……如果你解决了你的问题,考虑把它作为一个答案并接受它,这样未来读者可能会受益。
public final class URI
    implements Comparable<URI>, Serializable
{

    // Note: Comments containing the word "ASSERT" indicate places where a
    // throw of an InternalError should be replaced by an appropriate assertion
    // statement once asserts are enabled in the build.

    static final long serialVersionUID = -6052424284110960213L;


    // -- Properties and components of this instance --

    // Components of all URIs: [<scheme>:]<scheme-specific-part>[#<fragment>]
    private transient String scheme;            // null ==> relative URI
 baseUri =  URI.create("file:").resolve(FolderUtil.class.getResource("/").toURI());