Win a copy of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 this week in the Java in General forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Current directory

 
Ranch Hand
Posts: 97
Python VI Editor Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is there a variable or method that stores\returns the current directory of the JAR file or class
 
Sheriff
Posts: 26973
85
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Python VI Editor Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author and iconoclast
Posts: 24203
44
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Python VI Editor Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Sheriff
Posts: 22586
122
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Python VI Editor Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks it think I get it now
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic