Java Jar文件:使用资源错误:URI不是分层的 [英] Java Jar file: use resource errors: URI is not hierarchical

查看:113
本文介绍了Java Jar文件:使用资源错误:URI不是分层的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已将我的应用程序部署到jar文件。当我需要将数据从一个资源文件复制到jar文件外部时,我做这个代码:

  URL resourceUrl = getClass ().getResource( /资源/ data.sav); 
文件src = new File(resourceUrl.toURI()); // ERROR HERE
文件dst = new File(CurrentPath()+data.sav); // CurrentPath:jar文件的路径不包括jar文件名
FileInputStream in = new FileInputStream(src);
FileOutputStream out = new FileOutputStream(dst);
//这里的一些excute代码

我遇到的错误是:$ code> URI不是层次结构的。这个错误在IDE中运行时不符合要求。



如果我在StackOverFlow上更改了上述代码作为其他帖子的一些帮助:

  InputStream in = Model.class.getClassLoader()。getResourceAsStream(/ resource / data.sav); 
文件dst = new File(CurrentPath()+data.sav);
FileOutputStream out = new FileOutputStream(dst);
// ....
byte [] buf = new byte [1024];
int len;
while((len = in.read(buf))> 0){// NULL POINTER EXCEPTION
// ....
}


解决方案

你不能这样做

 文件src = new File(resourceUrl.toURI()); // ERROR HERE 

它不是一个文件!
当您从ide运行时,您没有任何错误,因为您不运行jar文件。在IDE中,文件系统中提取了类和资源。



但是您可以以这种方式打开一个 InputStream

  InputStream in = Model.class.getClassLoader()。getResourceAsStream(/ data.sav); 

删除/ resource。通常,IDE在文件系统类和资源上分离。但是当创建该jar时,它们将被放在一起。因此,文件夹级别/ resource仅用于类和资源分离。



获取资源时从classloader,您必须指定资源在jar内的路径,即真正的包层次结构。


I have deployed my app to jar file. When I need to copy data from one file of resource to outside of jar file, I do this code:

URL resourceUrl = getClass().getResource("/resource/data.sav");
File src = new File(resourceUrl.toURI()); //ERROR HERE
File dst = new File(CurrentPath()+"data.sav");  //CurrentPath: path of jar file don't include jar file name
FileInputStream in = new FileInputStream(src);
FileOutputStream out = new FileOutputStream(dst);
 // some excute code here

The error I have met is: URI is not hierarchical. this error I don't meet when run in IDE.

If I change above code as some help on other post on StackOverFlow:

InputStream in = Model.class.getClassLoader().getResourceAsStream("/resource/data.sav");
File dst = new File(CurrentPath() + "data.sav");
FileOutputStream out = new FileOutputStream(dst);
//....
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0) { //NULL POINTER EXCEPTION
  //....
}

解决方案

You cannot do this

File src = new File(resourceUrl.toURI()); //ERROR HERE

it is not a file! When you run from the ide you don't have any error, because you don't run a jar file. In the IDE classes and resources are extracted on the file system.

But you can open an InputStream in this way:

    InputStream in = Model.class.getClassLoader().getResourceAsStream("/data.sav");

Remove "/resource". Generally the IDEs separates on file system classes and resources. But when the jar is created they are put all together. So the folder level "/resource" is used only for classes and resources separation.

When you get a resource from classloader you have to specify the path that the resource has inside the jar, that is the real package hierarchy.

这篇关于Java Jar文件:使用资源错误:URI不是分层的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆