| Author |
Current directory
|
colton peterson
Ranch Hand
Joined: Nov 18, 2007
Posts: 97
|
|
|
Is there a variable or method that stores\returns the current directory of the JAR file or class
|
www.mormon.org
|
 |
Paul Clapham
Bartender
Joined: Oct 14, 2005
Posts: 16483
|
|
Jar files and classes don't have a "current directory". The process in which your classes are running has a current working directory, and there's a system property which tells you what that is. But I'm guessing you are trying to ask about something else. Can we go back one step from your question and find out what your earlier question was? The one to which the answer was "Use the current directory of a class".
|
 |
colton peterson
Ranch Hand
Joined: Nov 18, 2007
Posts: 97
|
|
The intent of my question was that I could put all of the external files such as pictures into my JAR file and be able to use them in my program. So I could go File example = new File(currentdirectory+"pics\mypic.GIF"); or something like that. If there is any other way to do that I would like to know
|
 |
Ernest Friedman-Hill
author and iconoclast
Marshal
Joined: Jul 08, 2003
Posts: 24054
|
|
|
You can actually put the images inside the jar file, and use the Class.getResource() method to load them from there, instead of using FileInputStreams(). Check out the Javadoc for java.lang.Class.getResource().
|
[Jess in Action][AskingGoodQuestions]
|
 |
colton peterson
Ranch Hand
Joined: Nov 18, 2007
Posts: 97
|
|
So I would go Class<myclassname>.getResource(mypic.GIF); and then it says It returns a URL, what do I do with that. Also what is a class Loader and is that important for doing this perhaps an example would make more sense to me [ March 16, 2008: Message edited by: colton peterson ] [ March 16, 2008: Message edited by: colton peterson ]
|
 |
Rob Spoor
Sheriff
Joined: Oct 27, 2005
Posts: 19216
|
|
For now, don't fuss about class loaders. Using classes to get the resources will do just fine. You need to learn something about the locations used in getResource. Use / to separate paths (I don't know if this is strictly necessary, but just use it. It makes the code portable). Use a leading / to have the resource be located relative to the start of your package, or omit it to have it be located relative to the class file itself. For example: Now the following are valid: Test.class.getResource("pic1.gif") Test.class.getResource("images/pic2.gif") Test.class.getResource("/images/pic3.gif") Now your question what to do with the URL. If you want to load images, javax.swing.ImageIcon can take that URL and load the image from it. You can use the ImageIcon in any JLabel or JButton without a problem. Now if you need the actual contents of the resource, use getResourceAsStream instead of getResource. This will return a java.io.InputStream from which you can read until there is no more data. Please be aware that both getResource and getResourceAsStream return null if the resource cannot be found.
|
SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
|
 |
colton peterson
Ranch Hand
Joined: Nov 18, 2007
Posts: 97
|
|
|
Thanks it think I get it now
|
 |
 |
|
|
subject: Current directory
|
|
|