This week's book giveaway is in the Cloud/Virtualizaton forum.
We're giving away four copies of Mesos in Action and have Roger Ignazio on-line!
See this thread for details.
Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

getSystemResourceAsStream - Reg

 
Sundi Mahadevan
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 401
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From the javadoc for ClassLoader.getResource(String)
The name of a resource is a "/"-separated path name that identifies the resource.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic