简体   繁体   English

带有getPath的java.nio.file.InvalidPathException

[英]java.nio.file.InvalidPathException with getPath

I am using this code 我正在使用此代码

 String path =  getClass().getResource("Template.xls").getPath();

When I run it on my machine (windows), everything is good. 当我在我的机器(Windows)上运行它时,一切都很好。 I even did system.out.println on the get resource part and on the get path part and the results were: 我甚至在get资源部分和get路径部分上执行了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? 有什么指针吗?

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. 这可能会导致问题,因为您似乎将java文件打包在Windows可执行文件中。 See Error in URL.getFile() 请参阅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. 它在Netbeans中工作,因为资源没有打包在jar中,而是文件系统上的单独文件。 You should try to run the exe-version on your machine. 您应该尝试在您的计算机上运行exe-version。 It will most likely fail as well. 它很可能也会失败。 If you want more information or help, please provide the complete code. 如果您需要更多信息或帮助,请提供完整的代码。

I faced this same issue and go around it by using the good ol' File API 我遇到了同样的问题,并通过使用优秀的'File API来解决它

URL url = MyClass.class.getClassLoader().getResource("myScript.sh");
Path scriptPath = new File(url.getPath()).toPath();

And it worked! 它奏效了!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 java.nio.file.InvalidPathException - java.nio.file.InvalidPathException 系统错误:java.nio.file.InvalidPathException: - SystemError: java.nio.file.InvalidPathException: 为什么这会抛出 java.nio.file.InvalidPathException? - Why does this throw java.nio.file.InvalidPathException? 遇到java.nio.file.InvalidPathException:非法char &lt;:&gt; - Encountering java.nio.file.InvalidPathException: Illegal char <:> java.nio.file.InvalidPathException 当路径包含冒号 (:) - java.nio.file.InvalidPathException when the path contains colon (:) Magnolia 5.4在发布/取消发布时升级java.nio.file.InvalidPathException - Magnolia 5.4 upgrade java.nio.file.InvalidPathException on publishing/unpublishing 使用闭包编译器格式化javascript文件时,在索引7处获取到java.nio.file.InvalidPathException:非法char &lt;:&gt; - Got java.nio.file.InvalidPathException:Illegal char <:> at index 7 when formatting javascript file using closure compiler WFLYCTL0080 Spring MVC java.lang.RuntimeException:java.nio.file.InvalidPathException - WFLYCTL0080 Spring MVC java.lang.RuntimeException: java.nio.file.InvalidPathException 在尝试编译 java 文件时获取 java.nio.file.InvalidPathException: Illegal char &lt;*&gt; - Getting a java.nio.file.InvalidPathException: Illegal char <*> while trying to compile java files java.nio.file.InvalidPathException:使用国家字符时格式错误的输入或输入包含不可映射的字符 - java.nio.file.InvalidPathException: Malformed input or input contains unmappable characters when using national characters
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM