Java file access

1

1-crm

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
 
J

Jean-Francois Briere

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
 
A

Andrew Thompson

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
....
...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

1-crm

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
 
A

Andrew Thompson

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

1-crm

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.
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.
 
C

Chris Uppal

Andrew said:
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
 
C

Chris Uppal

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
 
1

1-crm

@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!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,058
Latest member
QQXCharlot

Latest Threads

Top