read a file from inside the plugin jar. [message #660506] |
Fri, 18 March 2011 17:11 |
|
Hi All,
I have some files in my plugin that contain template data for my wizards, I have the strange situation that I can run the plugin successfully in the test environment, (pressing on "launch Eclipse application"), but when I try to export the plugin in a jar and deploy it in a different eclipse installation, I get the following exception:
Quote: | Caused by: java.lang.RuntimeException: uri was opaque. filename was: /resources/erroneousSectionsFile.ini uri was: jar:file:/D:/Users/Me/My%20Documents/eclipse/plugins/com.myC ompany.plugin_1.0.0.alpha.jar!/resources/erroneousSectionsFi le.ini
at org.myCompany.qat.plugin.Activator.getInternalFile(Activator .java:202)
|
I raise the exception when the URI is Opaque (it would have been thrown anyways from the constructor of java.lang.File).
Any Idea about what's the problem here?
Below the code of the getInternalFile method as I wrote it (obviously the line 202 is the one with the throw new RuntimeException):
Quote: | public static File getInternalFile(String filename) {
Bundle bundle = Platform.getBundle(PLUGIN_ID);
URL fileURL = bundle.getEntry(filename);
if (fileURL == null) {
throw new RuntimeException(
"fileUrl was null when searching for file: " + filename);
}
File file = null;
try {
URL resolved = FileLocator.resolve(fileURL);
try {
String clean = resolved.toString().replace(" ", "%20");
resolved = new URL(clean);
} catch (MalformedURLException e1) {
throw new RuntimeException("Problems cleaning the URL: "
+ fileURL, e1);
}
URI uri = resolved.toURI();
uri = uri.normalize();
if (uri.isOpaque()) {
throw new RuntimeException("uri was opaque. filename was: "
+ filename + " uri was: " + uri.toString());
}
file = new File(uri);
} catch (URISyntaxException e) {
throw new RuntimeException(
"internal pluging error: Filename is incorrect could not be resolved: "
+ fileURL, e);
} catch (IOException e) {
throw new RuntimeException(
"internal pluging error: could not find file" + fileURL, e);
}
return file;
}
|
How would you obtain a File from inside the bundle jar?
Thanks a lot in advance,
Giulio
[Updated on: Fri, 18 March 2011 17:13] Report message to a moderator
|
|
|
Re: read a file from inside the plugin jar. [message #661201 is a reply to message #660506] |
Wed, 23 March 2011 15:05 |
|
In the end I managed to solve it with the following method.
using it I'm able to obtain the input stream code from the jar package:
Quote: | private static InputStream getInternalFile(String fileName) {
Bundle bundle = Platform.getBundle(Activator.PLUGIN_ID);
Path originPath = new Path(fileName);
URL bundledFileURL = FileLocator.find(bundle, originPath, null);
InputStream inputStream;
try {
bundledFileURL = FileLocator.resolve(bundledFileURL);
inputStream = (InputStream) bundledFileURL.getContent();
} catch (IOException e) {
throw new RuntimeException("Plugin Error: can't read file: "
+ fileName, e);
}
return inputStream;
}
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.03856 seconds