Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Plugin Development Environment (PDE) » read a file from inside the plugin jar.
read a file from inside the plugin jar. [message #660506] Fri, 18 March 2011 17:11 Go to next message
Giulio is currently offline GiulioFriend
Messages: 11
Registered: February 2011
Junior Member

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 Go to previous messageGo to next message
Giulio is currently offline GiulioFriend
Messages: 11
Registered: February 2011
Junior Member

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;

}
Re: read a file from inside the plugin jar. [message #662346 is a reply to message #661201] Wed, 30 March 2011 10:05 Go to previous message
Lorenzo Bettini is currently offline Lorenzo BettiniFriend
Messages: 1417
Registered: July 2009
Senior Member
have you tried using a platform uri with 'plugin'?

"platform:/plugin/<plugin name>/<path to file>"

cheers
Lorenzo

On 03/23/2011 04:06 PM, Giulio wrote:
> 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;
>>
>> }
>


--
Lorenzo Bettini, PhD in Computer Science, DI, Univ. Torino
ICQ# lbetto, 16080134 (GNU/Linux User # 158233)
HOME: http://www.lorenzobettini.it MUSIC: http://www.purplesucker.com
http://www.myspace.com/supertrouperabba
BLOGS: http://tronprog.blogspot.com http://longlivemusic.blogspot.com
http://www.gnu.org/software/src-highlite
http://www.gnu.org/software/gengetopt
http://www.gnu.org/software/gengen http://doublecpp.sourceforge.net
Previous Topic:Problems with a TableView - it's not showing...
Next Topic:Wizard Page depending on Wizard Page
Goto Forum:
  


Current Time: Fri Dec 19 10:19:36 GMT 2014

Powered by FUDForum. Page generated in 0.04128 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software