我已将我的应用程序部署到jar文件.当我需要将数据从一个资源文件复制到jar文件之外时,我执行以下代码:
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
Run Code Online (Sandbox Code Playgroud)
我遇到的错误是:URI is not hierarchical
.在IDE中运行时我不满足此错误.
如果我更改上面的代码作为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) { …
Run Code Online (Sandbox Code Playgroud) 我有2个plguins 阿和乙.在A的MANIFEST.MF中,我在Require-Bundle部分中有插件B. 但是,当我试图让B的从资源一个样
ClassFromA.class.getClassLoader().getResource('resource_from_B')
Run Code Online (Sandbox Code Playgroud)
我变得空了.如果我将B的 resourсe(文件夹)放到A的根部,一切都像魅力一样.我错过了什么吗?
注意:我读过Lars Vogel的文章
Bundle bundle = Platform.getBundle("de.vogella.example.readfile");
URL fileURL = bundle.getEntry("files/test.txt");
File file = null;
try {
file = new File(FileLocator.resolve(fileURL).toURI());
} catch (URISyntaxException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud)
^当我从eclipse运行我的插件时这个解决方案有效,但当我将它打包到jar并尝试从本地更新站点安装时我得到了
java.lang.IllegalArgumentException: URI is not hierarchical
Run Code Online (Sandbox Code Playgroud)
PS我还在StackOverflow上阅读了几个相关问题,但未能找到答案:
解决方案:非常感谢@ greg-449.所以正确的代码是:
Bundle bundle = Platform.getBundle("resource_from_some_plugin");
URL fileURL = bundle.getEntry("files/test.txt");
File file = …
Run Code Online (Sandbox Code Playgroud)