Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / September 2006

Tip: Looking for answers? Try searching our database.

Java file access

Thread view: 
1-crm@freenet.de - 04 Sep 2006 18:58 GMT
Hey,

I have following problem with my Java program:
My project folder looks like:

my_folder
+ binary
   + Class1.class
+ source
   + Class1.java
+ files
   + File1.txt
   + File2.txt
   ....

I'd like to get access from Class1 to the files File1.txt and File2.txt
I need access to the files from a JAR package of this project as well
as from a 'normal' classpath program.

File f = new File ("files/File1.txt"); doesn't work within a jar
package.
and the call ClassLoader.getResource("files/File1.txt") always returns
null.

Has anbody an idea how to get access to the files? Why does the
getResource() call return null? Because of my file structure?

Thanks in advance,
1-crm
Jean-Francois Briere - 04 Sep 2006 20:15 GMT
Try:
ClassLoader.getResource("/files/File1.txt")

This is assuming that your folder is in the class path or that your jar
is in the class path
and that both (folder and jar) have this (sub-)directory structure (/
means root):
/
/files
/files/File1.txt
...

Regards
Andrew Thompson - 05 Sep 2006 06:25 GMT
...
> I have following problem with my Java program:
> My project folder looks like:
[quoted text clipped - 8 lines]
>     + File2.txt
>     ....

> ...the call ClassLoader.getResource("files/File1.txt") always returns
> null.

Instead of
   .getResource("files/File1.txt")
..try..
   .getResource("/files/File1.txt")

HTH

Andrew T.
1-crm@freenet.de - 05 Sep 2006 08:53 GMT
Hey,

thank you for your answers.

I tried:
.getResource("/files/File1.txt")
but this also returns null.

I treid a few other parameters:
getResource("files/File1.txt")   -> null
getResource("/files/File1.txt")  -> null
getResource("/") -> myfolder/binary

if / gets binary then its impossible to find the folder filds in there
because
my file structure is:

 myfolder/binary/Class1.class
 myfolder/source/Class1.java
 myfolder/files/File1.txt
 myfolder/files/File2.txt

If I bind a JAR package .getResource("/files/File1.txt") returns the
expected result ../myfolder/files/File1.txt but only within the jar
package. What am i doing wrong?
How do i have to set my class path variable? Btw: i am using Eclipse
and i'm generateing the jar package with the ecplise export funcion

Thanks and best regards,
1-crm
Andrew Thompson - 05 Sep 2006 09:14 GMT
...
> I tried:
> .getResource("/files/File1.txt")
> but this also returns null.
...
> If I bind a JAR package .getResource("/files/File1.txt") returns the
> expected result ../myfolder/files/File1.txt but only within the jar
> package. What am i doing wrong?

You need to look further into what the '/', '.' and '..'
parts of the string mean.

For example, putting '/' at the very beginning of the string
will indicate that the resource path starts from the 'root'.

When run on loose classes in the file-system, the '/'
indicates the *drive* on which they reside, whereas when
running from a Jar file, it indicates the root of the Jar file.

Including the leading '/' works for Jar files, but not (usually)
loose class files.

Or, to put that another way - you need to use slightly different
methods to access loose resources, than you need when
accessing the same resources in a far file.

But then - why is it so important to access resources
*both* ways from within the one project?

Either this project will be run from loose class files
(extremely not recommended) or Jar files.  It is not
difficult to generate a Jar file for use during testing.

>...Btw: i am using Eclipse  ...

That is not relevant to the problem, if I thought it was,
I'd recommend you post to the Eclipse forums.

Andrew T.
1-crm@freenet.de - 05 Sep 2006 09:30 GMT
> For example, putting '/' at the very beginning of the string
> will indicate that the resource path starts from the 'root'.
> When run on loose classes in the file-system, the '/'
> indicates the *drive* on which they reside, whereas when
> running from a Jar file, it indicates the root of the Jar file.

If i run my program from loose classes, the / don't give me the root of
the drive but the folder: myfolder/binary. thats what I don't
understand.

> Including the leading '/' works for Jar files, but not (usually)
> loose class files.
>
> Or, to put that another way - you need to use slightly different
> methods to access loose resources, than you need when
> accessing the same resources in a far file.
Yes this could be the work around for my problem, but this
differentiating the access method isn't a good solution,  I think

> But then - why is it so important to access resources
> *both* ways from within the one project?
> Either this project will be run from loose class files
> (extremely not recommended) or Jar files.  It is not
> difficult to generate a Jar file for use during testing.
Sorry but that not question. I was asking for a solution for both.

> >...Btw: i am using Eclipse  ...
> That is not relevant to the problem, if I thought it was,
> I'd recommend you post to the Eclipse forums.
?? ?? ??
I mention that, because if it is a class path problems you may be give
me tips for setting the path correctly in eclipse.
Chris Uppal - 05 Sep 2006 10:15 GMT
> > But then - why is it so important to access resources
> > *both* ways from within the one project?
> > Either this project will be run from loose class files
> > (extremely not recommended) or Jar files.  It is not
> > difficult to generate a Jar file for use during testing.
> Sorry but that not question. I was asking for a solution for both.

It's not difficult to make a solution which works for both without code changes
(see below).  I presume that your difficulties are purely to do with Eclipse
and whatever magic it is performing.  I can't help with that except to suggest
that you (temporarily) ditch Eclipse and work with the command line tools until
you are sure that your code is doing what you think it should do, and that it
works correctly.  /Then/ you can look for the problem in Eclipse.

Anyway, this works for me (on Windows)

Code:
===================
package aaa.bbb;

public class Test
{
public static void
main(String[] args)
{
 ClassLoader loader = Test.class.getClassLoader();
 System.out.println(loader.getResource("resources/test.txt"));
}
}
===================

Note that I'm looking for the resource file using a path relative to the root
of the classpath segment (jar or directory).

Now.

File structure:
   C:\Home\tmp\find-resource\aaa\bbb\Test.class
   C:\Home\tmp\find-resource\resources\test.txt

Running:
   java -cp C:\Home\tmp\find-resource aaa.bbb.Test

Produces:
   file:/C:/Home/tmp/find-resource/resources/test.txt

Then jar-ing up to produce test.jar with contents including:
   aaa/bbb/Test.class
   resources/test.txt

Running:
   java -cp test.jar aaa.bbb.Test

Produces:
   jar:file:/C:/Home/tmp/find-resource/test.jar!/resources/test.txt

So it all works.  I suggest you start with some such simple program as the
above, get it working using the command line tools, and then work out how to
make it work under Eclipse.  Then apply your new-found mastery of Eclipse to
solving your original problem.

   -- chris
Chris Uppal - 05 Sep 2006 09:57 GMT
> Instead of
>     .getResource("files/File1.txt")
> ..try..
>     .getResource("/files/File1.txt")

Probably not a good idea.  As you note yourself (in a later post) the meaning
of absolute pathnames is dependent on the semantics of the "file system" (in a
wide sense) and also on the classloader itself.  Better to use a path relative
to the notional root (which the OP was trying to do).

   -- chris
1-crm@freenet.de - 05 Sep 2006 13:12 GMT
@Chris
thank you for your qualified answer! I modified my code acoording your
example and tried it without using eclipse... and it works! Seems like
Ecplise is realy doing some 'magic' things. Maybe my project options
are not correct. I'll check them.

Thank you very much!


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.