wood burning stoves 2.0*
The moose likes Java in General and the fly likes getSystemResourceAsStream - Reg Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "getSystemResourceAsStream - Reg" Watch "getSystemResourceAsStream - Reg" New topic
Author

getSystemResourceAsStream - Reg

Sundi Mahadevan
Greenhorn

Joined: Jan 24, 2002
Posts: 4
Summary:
I am receiving null when I run my java program from the command promt. However, when I run in WASD 4.0.3, it works.
problem:
Assume a file "a.template" is available in c:\test\template.jar. The file "a.template" is in a package com.sundi.util.
I am writing a java program that uses "a.templete"; When I use
InputStream is = ClassLoader.getSystemResourceAsStream("com\\sundi\\util\\template.jar"),
I understand that it appends the working directory in front of it. This works fine, when I am running the program inside WASD 4.0.3.
I am always receiving null, when I am running from the command line argument (c:\test), after promoting all the classes to a jar file.
The above statement returns null.
When I change the argument to the one listed below, I am still getting null;
InputStream is = ClassLoader.getSystemResourceAsStream("/com\\sundi\\util\\template.jar").
The absolute path looks like c:\com\sundi\util\template.jar.
How can I make the getResourceAsStream to get the file from the jar file in c:\test\template.jar.
Please direct me to a specific link or briefly explain the mistake I am commiting.
Regards,
Sundi Mahadevan
Sundi Mahadevan
Greenhorn

Joined: Jan 24, 2002
Posts: 4
After surfing the internet, I found that the String was formatted incorrectly and changed them to the one listed below:
import java.io.*;
public class Test {
public static void main(String[] args) throws Exception {
String name = "/com.sundi.util.template.a.template";
Thread t = Thread.currentThread();
ClassLoader cl = t.getContextClassLoader();
InputStream is = cl.getResourceAsStream(name);
System.out.println("is = " + is );
}
I continue to get null. I also tried
name = "a.template" and
name = "/com/sundi/util/template/a.template"
with no improvements.
The file "a.template" is present in template.jar and the file is present within the dir structure
com/sundi/util/template.
I am running the code as
c:\test> java -cp c:\test\template.jar;%classpath%;. Test
Please help.
Regards,
Sundi
Sundi Mahadevan
Greenhorn

Joined: Jan 24, 2002
Posts: 4
I was instructed by one of my friend to make the following code change and it worked.
import java.io.*;
public class Test {
public static void main(String[] args) throws Exception {
new Test();
}
Test()
{
String name = "/com/sundi/util/template/a/template";
//Thread t = Thread.currentThread();
//ClassLoader cl = t.getContextClassLoader();
InputStream is = getClass().getResourceAsStream(name);
System.out.println("is = " + is );
}
}
I am still not sure why the previous code did not work. Any help is appreciated. We will also explore further.
Regards,
Sundi Mahadevan
Dave Landers
Ranch Hand

Joined: Jul 24, 2002
Posts: 401
From the javadoc for ClassLoader.getResource(String)
The name of a resource is a "/"-separated path name that identifies the resource.
 
jQuery in Action, 2nd edition
 
subject: getSystemResourceAsStream - Reg