• Post Reply Bookmark Topic Watch Topic
  • New Topic

Accessing parent directory  RSS feed

 
Kejal Shah
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm facing a problem
All my .class files are in folder say classes
Some of my classes are in a package in subclasses => classes/subclasses
All my images are in folder images in classes => classes/images
How does a .class in my subclasses package (dir) access my images in classes/images?
Hope i've made my problem clear :-)
Thanks in advance.
 
Stefan Wagner
Ranch Hand
Posts: 1923
Linux Postgres Database Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Kejal Shah:

All my .class files are in folder say classes
Some of my classes are in a package in subclasses => classes/subclasses
All my images are in folder images in classes => classes/images
How does a .class in my subclasses package (dir) access my images in classes/images?
Hope i've made my problem clear :-)


Did you try
 
Maulin Vasavada
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Kejal
either u'd have to use absolute path for the images folder OR use getResourceAsStream() method of java.lang.Class to make this work always.
the reason i'm saying this is, currently u might have code as a stand alone class but if u move this code someday to servlet then it will not work as the files are tried to be read from the web-servers directory from where it was actaully started. ie. the web-server will find classes as we would have configured web-server classpath but if we do File f = new File("test.txt"); then it will try to look for that file in directory where the web-server actually starts. e.g. on Tomcat it will be /bin directory of the Tomcat and on iPlanet it would be config directory...
hope this helps...
u can look for how to use getResourceAsStream() via googling...
getResourceAsStream() looksup for the resource from the CLASSPATH directories and jar files as its delegated to the ClassLoader...
Thanks!
Maulin
 
Elinor Chang
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To access a non-java file for your JAVA file, you could do the following:
1. Add the file to a directory and add that directory to your CLASSPATH or
include the file in your .jar file and add the JAR to your CLASSPATH
2. To access the file you need to use call to getResourceAsStream() as the following;
ClassLoader loader = YourClass.class.getClassLoader();
InputStream inputStream = loader.getResourceAsStream(res_file);
if (inputStream == null) {
throw XcodeError.FILE_IO(": '" + res_file + "'");
}
where the res_file is the relative path (to your CLASSPATH) of your non-java file. For example, if the res_file inlucded as "image/my.gif" in your JAR file, the value of the res_file is "image/my.gif".
 
Kejal Shah
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks all
 
Kejal Shah
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Stefan. Thansks fro ur reply, but that doesn't work
package pkg;
class Trial{
ImageIcon icn = new ImageIcon(getClass().getResource("../images/beach.jpg");
}
This doesn't work.
Can u help me out.
Thanks again
 
Maulin Vasavada
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Kejal
it wont work. u combined two approaches suggested here (one from stephan and one from me and Elinor). remember, getResource() loads resource from the CLASSPATH hence it will convert all "." with "/" and so we can't write relative paths easily u know...
try the way Elinor suggested.
regards
maulin
 
Kejal Shah
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've done this:
Package pkg;
class Trial{
InputStream is = getClass().getClassLoader().getResourceAsStream("/images/beach.jpg");
ImageIcon icn;
...
}
My dir structure is as follows
classes
\images
\pkg
Will the code above be able to access the .jpg from images folder?
How do i use the is to create an ImageIcon object?
Tx in advance.
 
Kejal Shah
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I did this:

InputStream is = getClass().getClassLoader().getResourceAsStream("images/beach.jpg");
byte[] b = new byte[is.available()];
is.read(b);
ImageIcon icn = new ImageIcon(b);

Jus tell me if this is the right way of doing it.
Thanks all
 
Stefan Wagner
Ranch Hand
Posts: 1923
Linux Postgres Database Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Try this - it will work (I assume you hava beach.jpg ? )

<hr></blockquote>
[ August 01, 2003: Message edited by: Stefan Wagner ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!