• Post Reply Bookmark Topic Watch Topic
  • New Topic

Current directory  RSS feed

 
colton peterson
Ranch Hand
Posts: 97
Linux Python VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there a variable or method that stores\returns the current directory of the JAR file or class
 
Paul Clapham
Sheriff
Posts: 22819
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 97
Linux Python VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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().
 
colton peterson
Ranch Hand
Posts: 97
Linux Python VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 21133
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
colton peterson
Ranch Hand
Posts: 97
Linux Python VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks it think I get it now
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!