|
Re: Using getResourceAsStream in a plugin project [message #192050 is a reply to message #192005] |
Mon, 05 February 2007 20:19 |
Eclipse User |
|
|
|
Originally posted by: merks.ca.ibm.com
Carl,
I wonder if the path is relative or absolute? Maybe you want to call
this method directly on the java.lang.Class rather than getting the
class loader first; I think that will make a difference for relative
paths. And I assume you've verified that the file is actually present
in the jar for the plugin? I.e., that it's not a build issue...
Carl Manaster wrote:
> Hi,
>
> This function has worked perfectly for me in non-plugin projects; when
> I try to use it in a plugin project test I get a null pointer
> exception on stream. How is the Eclipse framework interfering with
> this, and, more importantly, how can I get it to work? Thank you.
>
> private BufferedReader getResourceReader(String path) {
> InputStream stream = getClass().
> getClassLoader().getResourceAsStream(path);
> InputStreamReader inputStreamReader =
> new InputStreamReader(stream);
> return new BufferedReader(inputStreamReader);
> }
>
> I have tried a number of variations, thinking that the framework
> probably messes with the class loader (getSystemResourceAsStream(),
> getParent(), etc.) without success.
>
>
> Peace,
> --Carl
>
|
|
|
|
Re: Using getResourceAsStream in a plugin project [message #192071 is a reply to message #192057] |
Mon, 05 February 2007 21:25 |
Eclipse User |
|
|
|
Originally posted by: merks.ca.ibm.com
Carl,
I know we use this same type of approach in EMF to load .ecore files and
it should work nicely. But one thing you should ensure is that the
file is being copied to the bin folder with the expected path and hence
is actually present on the classpath at runtime.
Carl Manaster wrote:
> Hi, Ed,
>
> Thank you for the reply.
>
>> I wonder if the path is relative or absolute? Maybe you want to call
>> this method directly on the java.lang.Class rather than getting the
>> class loader first; I think that will make a difference for relative
>> paths. And I assume you've verified that the file is actually
>> present in the jar for the plugin? I.e., that it's not a build issue...
>
> The path is absolute, although I tried copying the file into the same
> folder as the class's source and using a relative path, to no avail.
> I don't know about jar files - I'm running this as a JUnit test (I've
> tried both as JUnit test and as JUnit plugin test; neither works) from
> within Eclipse; I don't understand what the jar situation is at that
> point. I'm using this approach to load test data from a text file;
> this has worked very well for me in the past, in exactly the same
> context (that is to say, running as a JUnit test from the IDE). I
> haven't tried java.lang.Class; I'll try it next chance I get. Thanks.
>
> Peace,
> --Carl
>
|
|
|
|
Re: Using getResourceAsStream in a plugin project [message #192228 is a reply to message #192220] |
Tue, 06 February 2007 16:13 |
Eclipse User |
|
|
|
Originally posted by: merks.ca.ibm.com
This is a multi-part message in MIME format.
--------------050001040304050604030503
Content-Type: text/plain; charset=ISO-8859-15; format=flowed
Content-Transfer-Encoding: 7bit
Carl,
Sorry providing an answer that's so easy to misinterpret. Here's an
example from generated EMF code where we load an "Xyz.ecore" file
relative to the class that is in the same package as the Xyz.ecore itself:
public void loadPackage() {
if (isLoaded)
return;
isLoaded = true;
URL url = *getClass().getResource(packageFilename)*;
if (url == null) {
throw new RuntimeException(
"Missing serialized package: " + packageFilename);
//$NON-NLS-1$
}
This is some similar code (in EMFPlugin) that looks for the
plugin.properties in various places:
public URL getBaseURL()
{
if (baseURL == null)
{
if (getPluginResourceLocator() == null)
{
try
{
// Determine the base URL by looking for the
plugin.properties file in the standard way.
//
Class<? extends EMFPlugin> theClass = getClass();
URL pluginPropertiesURL =
theClass.getResource("plugin.properties");
if (pluginPropertiesURL == null)
{
// If that fails, determine the URL for the class itself.
// The URL will be of one of the following forms,
// so there are a few good places to consider looking
for the plugin.properties.
//
// For a plugin.xml with runtime="common.jar":
//
jar:file:/D:/sandbox/unpackage1-3.1M7/eclipse/plugins/org.ec lipse.emf.common/common.jar!/org/eclipse/common/CommonPlugin .class
//
// For a plugin.xml with runtime="runtime/common.jar":
//
jar:file:/D:/sandbox/unpackage1-3.1M7/eclipse/plugins/org.ec lipse.emf.common/runtime/common.jar!/org/eclipse/common/Comm onPlugin.class
//
// For a plugin.xml with runtime="." where the plugin is
jarred:
//
jar:file:/D:/sandbox/unpackage1-3.1M7/eclipse/plugins/org.ec lipse.emf.common.jar!/org/eclipse/common/CommonPlugin.class
//
// For a plugin.xml with runtime="." where the plugin is
not jarred.
//
file:/D:/sandbox/unpackage1-3.1M7/eclipse/plugins/org.eclips e.emf.common/org/eclipse/emf/common/CommonPlugin.class
//
// Running in PDE with bin on classpath:
//
file:/D:/sandbox/unpackage1-3.1M7/eclipse/plugins/org.eclips e.emf.common/bin/org/eclipse/emf/common/CommonPlugin.class
//
String className = theClass.getName();
int index = className.lastIndexOf(".");
URL classURL = theClass.getResource((index == -1 ?
className : className.substring(index + 1)) + ".class");
URI uri = URI.createURI(classURL.toString())
Carl Manaster wrote:
> Hi, Ed,
>
>> Maybe you want to call this method directly on the java.lang.Class
>> rather than getting the class loader first
>
> When I try
>
> InputStream stream =
> java.lang.Class.getClassLoader().getResourceAsStream(path);
>
> I get the error:
>
> Cannot make a static reference to the non-static method
> getClassLoader() from the type Class.
>
> Peace,
> --Carl
>
--------------050001040304050604030503
Content-Type: text/html; charset=ISO-8859-15
Content-Transfer-Encoding: 8bit
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta content="text/html;charset=ISO-8859-15"
http-equiv="Content-Type">
</head>
<body bgcolor="#ffffff" text="#000000">
Carl,<br>
<br>
Sorry providing an answer that's so easy to misinterpret.
|
|
|
|
Powered by
FUDForum. Page generated in 0.08225 seconds