• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Help - getResource returns null

 
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is embarassing to say, but I have been stuck with this for some time.

I have the following code which in any combination of the file returns null and I am clueless about it.


I have tried the following possible combinations for THE_CLIENT_IMAGE and all of them return null
THE_CLIENT_IMAGE = "/about.gif"
THE_CLIENT_IMAGE = "/images/about.gif"
THE_CLIENT_IMAGE = "D:/test/dist/images/about.gif"

Could anyone help here?
 
Ranch Hand
Posts: 284
Netbeans IDE Firefox Browser Debian
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,
try something like
THE_CLIENT_IMAGE = "./images/about.gif"
Regards
 
Muthaiah Ramanathan
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am sorry it didn't work either. I forgot to say earlier, I had the d:\test\dist\ in the classpath.

Is it okay to instruct the evaluator to set the classpath?
Or is it okay to instruct the evaluator to cd to the proper directory?
 
Oricio Ocle
Ranch Hand
Posts: 284
Netbeans IDE Firefox Browser Debian
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello again,
what is the relative path of the images to the caller class file?
 
Muthaiah Ramanathan
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The caller class file is in runme.jar at d:\test\dist
So the caller class is d:\test\dist\runme.jar$suncertify\gui\Client
The image is at d:\test\dist\images

Initially I used the property "user.dir" and located the image file. It was not a problem. But that solutions works only if I run from the current directory (d:\test\dist). Therefore I thought I should use the getResource so that it can find images wherever it is, regardless the current working directory, i.e., it should be okay run from d:\ as 'java -jar test\dist\runme.jar Alone'
 
Muthaiah Ramanathan
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I just found a thread web page where Andrew was saying that he instructed the evaluator to change directory (in README.TXT) and so probably that is accepted.

With that you can ask the instructor to cd to the project where the SCJD_XXXXXX.jar is unjarred. You can execute by saying "java -jar <filename> [<mode>].

But it contradicts below the statement in the instructions.html.

When you submit your assignment, each part (client and server) must be executable using a command of this exact form:

java -jar <path_and_filename> [<mode>]



Guys, any thoughts on this?
[ October 03, 2005: Message edited by: Muthaiah Ram ]
 
Ranch Hand
Posts: 961
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I do not know what you guys are doing, but I jarred my application images and I get them with:



It works fine for me, because it searches the image in the whole class path, including jars.

Now, the assignment says the evaluator must run the application with:



So, I guess I can set up other jars in the Class-Path attribute of the SCJD.jar Manifest, so that it searches for the dependant jars. That way the evalutator will not have to specify them in the command line classpath.

I guess it will work fine that way.

Regarding Muthaiah Ram problem I just suggest this:

The problem is that you are running your application from the dist directory and it is trying to access the files departing from that dir. Locate you SCJD.jar in the base directory and you will see it works fine, or simply set your image path to the parent directory, i.e.: "../images/myImage.png".

I hope this helps!
[ October 03, 2005: Message edited by: Edwin Dalorzo ]
 
Muthaiah Ramanathan
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Edwin and Oricio,

Now I have the images directory inside the runme.jar and when I do



It is able to find the image in the runme.jar!images/, wherever I run.

I tried to have the images.jar inside the runme.jar and specify the classpath to add images.jar in the manifest file. BUT IT STILL DOEN'T WORK.

Can you explain why?

Meanwhile, is it okay to have to the 'images' directory inside the runme.jar?
[ October 03, 2005: Message edited by: Muthaiah Ram ]
 
Oricio Ocle
Ranch Hand
Posts: 284
Netbeans IDE Firefox Browser Debian
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello again guys,
I see the problem has been solved.
But, why you want to stuf your images into another jar?
My design:
into the package images:
Singleton class: ImageManager with static method: getImage(String fileName)
Basically this method uses getResource if there isn't any previous instance created.
Regards
 
Muthaiah Ramanathan
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oricio,

If you don't put your image files in the jar file, you will have to use the relative path to locate the image file. This will work only if you are running from the current directory.

Say, the evaluator decides to run it from (say d:\) some other directory by executing

The image files will not be found.

But if you put it in a jar, (either in a sub jar or in a sub directory), you can use getResource to access it. This way you don't have to worry from where you are running the program.

Anyway, to tell you guys, I finally settled myself to have the images in a directory inside the runme.jar. Didn't want to waste any more time in researching how to access a jar inside a jar -(Looks like you'll have to override the ClassLoader!)

Thanks for your time!
 
Oricio Ocle
Ranch Hand
Posts: 284
Netbeans IDE Firefox Browser Debian
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Muthaiah,
I know that the resources, this case the images, need to be in the classpath since you have to relatively locate them. That's why me and everybody packs them within the jar.

I tried to have the images.jar inside the runme.jar


that's what i meant: why do put your images into another jar?

Regards
 
Muthaiah Ramanathan
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry I'm unclear. Then how did you have the images in your runme.jar and how did you set the class-path.
 
Oricio Ocle
Ranch Hand
Posts: 284
Netbeans IDE Firefox Browser Debian
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Muthaiah, It isn't clear to me what's your question

Treat your images folder just like another package of your classpath.
If your unjarred application is working ok, when you pack it into the jar add also the images package.

If the problem is how to make the jar, i recommend you using an ide tool as first contact (all of them let you to export to a jar), and then going deeper into the jar format (
JAR File Specification)

Regards
 
reply
    Bookmark Topic Watch Topic
  • New Topic