Java 为什么我的 URI 不是分层的?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18055189/
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
Why is my URI not hierarchical?
提问by grep
I have files in resource folder. For example if I need to get file from resource folder, I do like that:
我在资源文件夹中有文件。例如,如果我需要从资源文件夹中获取文件,我喜欢这样:
File myFile= new File(MyClass.class.getResource(/myFile.jpg).toURI());
System.out.println(MyClass.class.getResource(/myFile.jpg).getPath());
I've testedand everything works!
我已经测试过,一切正常!
The path is
路径是
/D:/java/projects/.../classes/X/Y/Z/myFile.jpg
/D:/java/projects/.../classes/X/Y/Z/myFile.jpg
But, If I create jar file, using , Maven:
但是,如果我创建 jar 文件,使用 , Maven:
mvn package
...and then start my app:
...然后启动我的应用程序:
java -jar MyJar.jar
I have that following error:
我有以下错误:
Exception in thread "Thread-4" java.lang.RuntimeException: ?a??a??aü?a¢?a¥?a??a??aá?a??aí ?a??aé?a£?a??a¥?aá?a??a??a??aü?a??a??aé ?a??a??aú?a??aü?a??a??a??aü?a??aé
Caused by: java.lang.IllegalArgumentException: URI is not hierarchical
at java.io.File.<init>(File.java:363)
...and path of file is:
...和文件的路径是:
file:/D:/java/projects/.../target/MyJar.jar!/X/Y/Z/myFile.jpg
This exception happens when I try to get file from resource folder. At this line. Why? Why have that problem in JAR file? What do you think?
当我尝试从资源文件夹中获取文件时会发生此异常。在这一行。为什么?为什么在 JAR 文件中有这个问题?你怎么认为?
Is there another way, to get the resource folder path?
还有另一种方法来获取资源文件夹路径吗?
采纳答案by rocketboy
You should be using
你应该使用
getResourceAsStream(...);
when the resource is bundled as a jar/war or any other single file package for that matter.
当资源被捆绑为 jar/war 或任何其他与此相关的单个文件包时。
See the thing is, a jar is a single file (kind of like a zip file) holding lots of files together. From Os's pov, its a single file and if you want to access a part of the file
(your image file) you must use it as a stream.
看到的是,jar 是一个单独的文件(有点像 zip 文件),其中包含许多文件。从 Os 的 pov 来看,它是一个文件,如果您想访问part of the file
(您的图像文件),则必须将其用作流。
回答by Ilya Buziuk
Here is a solution for Eclipse RCP / Plugin developers:
这是 Eclipse RCP/插件开发人员的解决方案:
Bundle bundle = Platform.getBundle("resource_from_some_plugin");
URL fileURL = bundle.getEntry("files/test.txt");
File file = null;
try {
URL resolvedFileURL = FileLocator.toFileURL(fileURL);
// We need to use the 3-arg constructor of URI in order to properly escape file system chars
URI resolvedURI = new URI(resolvedFileURL.getProtocol(), resolvedFileURL.getPath(), null);
File file = new File(resolvedURI);
} catch (URISyntaxException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
It's very important to use FileLocator.toFileURL(fileURL)
rather than resolve(fileURL)
, cause when the plugin is packed into a jar this will cause Eclipse to create an unpacked version in a temporary location so that the object can be accessed using File. For instance, I guess Lars Vogel has an error in his article - http://blog.vogella.com/2010/07/06/reading-resources-from-plugin/
使用FileLocator.toFileURL(fileURL)
而不是非常重要resolve(fileURL)
,因为当插件打包到 jar 中时,这将导致 Eclipse 在临时位置创建一个解压版本,以便可以使用 File 访问该对象。例如,我猜 Lars Vogel 在他的文章中有一个错误 - http://blog.vogella.com/2010/07/06/reading-resources-from-plugin/
回答by Prateek Mishra
I face same issue when I was working on a project in my company. First Of All, The URI is not hierarichal Issue is because probably you are using "/" as file separator.
当我在我的公司从事一个项目时,我遇到了同样的问题。首先,URI 不是分层的问题是因为您可能使用“ /”作为文件分隔符。
You must remember that "/" is for Windows and from OS to OS it changes, It may be different in Linux. Hence Use File.seperator.
你必须记住,“/”是针对 Windows 的,它会随着操作系统的不同而变化,在 Linux 中可能会有所不同。因此使用File.separator。
So using
所以使用
this.getClass().getClassLoader().getResource("res"+File.separator+"secondFolder")
may remove the URI not hierarichal. But Now you may face a Null Pointer Exception. I tried many different ways and then used JarEntries Class to solve it.
可能会删除非层次结构的 URI。但是现在您可能会遇到空指针异常。我尝试了很多不同的方法,然后使用 JarEntries 类来解决它。
File jarFile = new File(this.getClass().getProtectionDomain().getCodeSource().getLocation().toURI().getPath());
String actualFile = jarFile.getParentFile().getAbsolutePath()+File.separator+"Name_Of_Jar_File.jar";
System.out.println("jarFile is : "+jarFile.getAbsolutePath());
System.out.println("actulaFilePath is : "+actualFile);
final JarFile jar = new JarFile(actualFile);
final Enumeration<JarEntry> entries = jar.entries(); //gives ALL entries in jar
System.out.println("Reading entries in jar file ");
while(entries.hasMoreElements()) {
JarEntry jarEntry = entries.nextElement();
final String name = jarEntry.getName();
if (name.startsWith("Might Specify a folder name you are searching for")) { //filter according to the path
System.out.println("file name is "+name);
System.out.println("is directory : "+jarEntry.isDirectory());
File scriptsFile = new File(name);
System.out.println("file names are : "+scriptsFile.getAbsolutePath());
}
}
jar.close();
You have to specify the jar name here explicitly. So Use this code, this will give you directory and sub directory inside the folder in jar.
您必须在此处明确指定 jar 名称。所以使用此代码,这将为您提供jar中文件夹内的目录和子目录。