socialnero.blogg.se

Java get file path of resource
Java get file path of resource











java get file path of resource
  1. #Java get file path of resource full#
  2. #Java get file path of resource code#

This seems non-dynamic to me (meaning that the user must know where these directories are and must type/modify these paths themselves.) I do not expect the user, when wanting to run the program, to type in "java -cp.

#Java get file path of resource code#

Doing a getResourceAsStream("images/someUserPicture.jpg") method or even a new ImageIcon("images/someUserPicture.jpg") method call in my code doesn't always work for me.Ģ) Honestly, I don't like the idea of hardcoding the directory paths to the jar file and the images directory as a classpath option. The issue to me isn't if I should use or not use the getResourceAsStream() method but what should I put as the input parameter for the getResourceAsStream() method? And how do I get the URL or the filename string of the images to load? I've been having a problem with, when running the program, the current working directory (CWD) isn't always the same as the directory where the program is located (execution directory). But my issues are thatġ) I didn't know what to put as the input string for the getResourceAsStream() method. I did try to use getResourceAsStream() and adding the directories to the classpath as navision said. I did and still am taking it into consideration. It is considered to be the dominant paradigm for Java access to resources, at least among Sun and other programming examples I've seen from people I respect very highly. Treadwell, while vafarmboy's solution is correct and probably resilient to OS changes and other such things, you really should do what navision said. Blah.īut my original question still stands: Is there a way to programmatically get the executing jar file's absolute path? The only thing that sucks is that, in Eclipse, this relative path doesn't work and I have to append the jar file folder to the images folder (ie String imageDir="lib/images" ) for it to work properly (but now doesn't work properly when running standalone outside of Eclipse).

java get file path of resource

I'm guessing that, when launching the program through the batch file, the current working directory is now the jar file directory (which coincidently is what I want), so using a relative path to the images directory works. And, when launching the program through the batch file, I can properly get what I need in the images directory. And I've noticed that I can code in a relative path to my images directory (ie String imageDir="images" ) without having to append the jar file directory path. I do have batch file now to launch the program.

#Java get file path of resource full#

My issue is trying not to hardcode the full path to the images directory but getting it during runtime if possible.īut I think I have a fix for my situation. I've got that portion of the code to work. I don't have any problems finding the image resources (since I've been hardcoding the full path to the images directory) and loading them in my program. I think there might be a bit of confusion. Ok, uhmmm, I'm going to plea ignorance and ask how do I "add your images directory to your classpath at launch time"? Being a newbie Java person, the concept of classpath is still, well, new to me.Īlso, uhmmm, playing with the getResourceAsStream() method (and subsequently the getResource() method), I don't see how this will get the jar file directory path.

java get file path of resource

Where "foo.gif" is the path relative to the directory you added to the classpath. InputStream stream = Thread.currentThread().getContextClassLoader().getResourceAsStream("foo.gif")

java get file path of resource

Add your images directory to your classpath at launch time and do something like this:













Java get file path of resource