带有 getPath 的 java.nio.file.InvalidPathException
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38887853/
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
java.nio.file.InvalidPathException with getPath
提问by Snake
I am using this code
我正在使用此代码
String path = getClass().getResource("Template.xls").getPath();
When I run it on my machine (windows), everything is good. I even did system.out.println on the get resource part and on the get path part and the results were:
当我在我的机器(windows)上运行它时,一切都很好。我什至在获取资源部分和获取路径部分上做了 system.out.println ,结果是:
file:/C:/Eclipse/Netbeans/SoftwareCom/build/classes/assets/Template.xls
文件:/C:/Eclipse/Netbeans/SoftwareCom/build/classes/assets/Template.xls
/C:/Eclipse/Netbeans/SoftwareCom/build/classes/assets/Template.xls
/C:/Eclipse/Netbeans/SoftwareCom/build/classes/assets/Template.xls
However I am getting the following error reports from some users
但是我从一些用户那里得到以下错误报告
java.nio.file.InvalidPathException: Illegal char <:> at index 4:
file:\C:\Software%20Com\SoftwareCom.exe!\assets\Template.xls
Iam not sure whats happening or why would it work for some and not others
我不确定发生了什么,或者为什么它对某些人而不是其他人有效
Any pointers?
任何指针?
回答by Guenther
To answer this question properly, it would be helpful to know what you want to do with the path information. To read the file, you don't need the path. You could just call
要正确回答这个问题,了解您想对路径信息做什么会很有帮助。要读取文件,您不需要路径。你可以打电话
getClass().getResourceAsStream("Template.xls")
If you really want to know the path, you should call
如果你真的想知道路径,你应该打电话
URL url = getClass().getResource("Template.xls");
Path dest = Paths.get(url.toURI());
This might cause problems as you seem to pack your java files in a windows executable. See Error in URL.getFile()
这可能会导致问题,因为您似乎将 java 文件打包在 Windows 可执行文件中。请参阅URL.getFile() 中的错误
Edit for your comment:
编辑您的评论:
As I wrote above, you don't need the path of the source to copy. You can use
正如我上面写的,您不需要复制源的路径。您可以使用
getClass().getResourceAsStream("Template.xls")
to get the content of the file and write the content to whereever you want to write it. The reason for failing is that the file in your second example is contained within an executable file:
获取文件的内容并将内容写入您想要写入的任何位置。失败的原因是第二个示例中的文件包含在一个可执行文件中:
file:\C:\Software%20Com\SoftwareCom.exe
as can be seen from the path:
从路径可以看出:
file:\C:\Software%20Com\SoftwareCom.exe!\assets\Template.xls
The exclamation mark indicates that the resource is within that file. It works within Netbeans because there the resource is not packed in a jar, but rather is a separate file on the filesystem. You should try to run the exe-version on your machine. It will most likely fail as well. If you want more information or help, please provide the complete code.
感叹号表示资源在该文件中。它在 Netbeans 中工作,因为资源没有打包在 jar 中,而是文件系统上的一个单独文件。您应该尝试在您的机器上运行 exe 版本。它也很可能会失败。如果您需要更多信息或帮助,请提供完整代码。
回答by E.S.
I faced this same issue and go around it by using the good ol' File API
我遇到了同样的问题,并通过使用好的 ol' File API 解决了这个问题
URL url = MyClass.class.getClassLoader().getResource("myScript.sh");
Path scriptPath = new File(url.getPath()).toPath();
And it worked!
它奏效了!