URL myurl = this.getClass().getResource("/SummaryReport.rptdesign");
			File file = new File(myurl.getFile());
			System.out.println("Name=" + file.getAbsolutePath());

The above code seems to get the resource. But I guess I am going about creating a file connected to that resource wrong? Because I cannot subsequently open a FileReader connected to that file. . I am assuming this is because the File is not set up correctly. I'm pretty tired right now, but I followed this pretty far including searches on daniweb etc, and am not getting anywhere so I'd appreciate any help.

Recommended Answers

All 9 Replies

>>But I guess I am going about creating a file connected to that resource wrong? Nope.
Instances of File class may or may not denote an actual file-system object such as a file or a directory.

The associated errors would help. Also, provide the full path to the "root" directory of the classpath item that the class making the call resides in, and the full path to the actual file.

Also, check the access rights of the user to the file, and the directory it's in. And I hope that this "file" is not in a jarfile, right?

No, it is not in a jar file. And yes, I have all the needed permissions, and no permissions are denied, I just checked under the file's Properties. I'm trying to make it so that when this application is distributed the file can be located.

File path: C:\Documents and Settings\Kyle\workspace\First Build\SummaryReport.rptdesign

Java file location: C:\Documents and Settings\Kyle\workspace\First Build\src\first_build\BIRT\BirtCard.java

.class file location: C:\Documents and Settings\Kyle\workspace\First Build\bin\first_build\BIRT

URL myurl = this.getClass().getResource("SummaryReport.rptdesign");
			System.out.println(myurl.getFile());

Causes:
java.lang.NullPointerException. The exception comes from the second line, myurl.getFile(), because myurl was previously set to null because getResource returned null.

And I'm looking into access rights.

getResource searches (unless you are doing clever stuff with class loaders) your classpath, so I'd try putting the data file alongside the class file (or specify its location as a path starting from the folder where the class file is) (modified as necessary if you have specified a package name).

BestJewSinceJC,
.class and a file (SummaryReport.rptdesign) must be in a same folder.

No, they don't necessarily need to be in the same place, but it does need to be on the class path.

Say you have /classes on your classpath. And your class is my.package.MyClass. Then your class is located at /classes/my/package/MyClass.class. Assuming there are no other packages, then your file (named myfile) could be located in the following locations

/classes/myfile                          getResource("/myfile")
/classes/my/myfile                       getResource("/my/myfile")
/classes/my/package/myfile               getResource("/my/package/myfile")
                                         getResource("myfile")
/classes/my/package/some/sub/dir/myfile  getResource("/classes/my/package/some/sub/dir/myfile"
                                         getResource("some/sub/dir/myfile")
/classes/some/other/dir/myfile           getResource("/some/other/dir/myfile")
commented: Thanks +7
/classes/my/package/some/sub/dir/myfile  getResource("/classes/my/package/some/sub/dir/myfile"
                                         getResource("some/sub/dir/myfile")

Oops! Obviously that one should be

/classes/my/package/some/sub/dir/myfile  getResource("/my/package/some/sub/dir/myfile"
                                         getResource("some/sub/dir/myfile")

> Because I cannot subsequently open a FileReader connected to
> that file.

You might consider using getResourceAsStream() if all you want is to create a FileReader instance.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.