Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typo3/2.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.net.URL不一致性/错误的建议_Java - Fatal编程技术网

正在寻找解决java.net.URL不一致性/错误的建议

正在寻找解决java.net.URL不一致性/错误的建议,java,Java,下面的代码暴露了这个bug,我知道我可以跟踪getFile()和getPath()的结果,但是我正在寻找一个更优雅、更健壮的东西。我希望能够在默认情况下遍历包含jar的文件夹,并使用用户选择的/配置路径查找和加载资源 import java.net.MalformedURLException; import java.net.URISyntaxException; import java.net.URL; public class UrlBug { UrlBug() { try {

下面的代码暴露了这个bug,我知道我可以跟踪getFile()和getPath()的结果,但是我正在寻找一个更优雅、更健壮的东西。我希望能够在默认情况下遍历包含jar的文件夹,并使用用户选择的/配置路径查找和加载资源

import java.net.MalformedURLException;
import java.net.URISyntaxException;
import java.net.URL;

public class UrlBug {

UrlBug() {
    try {
        final URL resourceRoot = this.getClass().getClassLoader().getResource(".");
        System.out.println(resourceRoot.getFile());
        System.out.println(resourceRoot.getPath());
        System.out.println(resourceRoot.toURI());
        System.out.println(resourceRoot.toExternalForm());
        System.out.println(resourceRoot.toString());
        new URL(resourceRoot.getFile());
    } catch (final URISyntaxException e) {
        e.printStackTrace();
    } catch (final MalformedURLException e) {
        e.printStackTrace();
    }
}
public static void main(final String[] args) {
    new UrlBug();
}
}
下面生成的带有“bug”的输出

/C:/Users/Me/workspaces/.../target/classes/            <-- Malformed filename
/C:/Users/Me/workspaces/.../target/classes/            <-- Malformed path
file:/C:/Users/Me/workspaces/.../target/classes/
file:/C:/Users/Me/workspaces/.../target/classes/
file:/C:/Users/Me/workspaces/.../target/classes/

java.net.MalformedURLException: no protocol:
/C:/Users/Me/workspaces/scratch/target/classes/     at
java.net.URL.<init>(URL.java:585)   at
java.net.URL.<init>(URL.java:482)   at
java.net.URL.<init>(URL.java:431)   at
scratch.UrlBug.<init>(UrlBug.java:20)   at
scratch.UrlBug.main(UrlBug.java:39)

/C:/Users/Me/workspaces/../target/classes/我不知道您为什么要自己遍历jar目录结构。Java现有的类就是为了处理这种情况而设计的。要修复代码,只需使用
toString()
方法:

new URL(resourceRoot.toString());
“file:/”前缀是“protocol”。使用
toString()
方法可以确保字符串前面有“file:/”协议。根据文件:

Protocol handlers for the following protocols are guaranteed to exist on the search path:

     http, https, ftp, file, and jar

到底是什么让你认为这是一个bug?你到底指的bug/不一致性是什么?@PaulTomblin如果你运行代码,你会得到一个异常:java.net.MalformedURLException:no protocol:/C:/src/other/stackoverflow/at java.net.URL.(URL.java:585)at java.net.URL.(URL.java:482)at java.net.URL.(URL.java:431)在UrlBug.(UrlBug.java:15)在UrlBug.main(UrlBug.java:23)是的,因为您正在传递一个文件名,就像它是一个URL一样。它是畸形的。错误在哪里?URL可以是文件名,请参阅RFC1630和RFC1738。错误在于getFile()和getPath()返回的文件名和路径格式无效。