Skip to Main Content

Java Development Tools

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Interested in getting your voice heard by members of the Developer Marketing team at Oracle? Check out this post for AppDev or this post for AI focus group information.

unable to find flatfile path of project while running as jar

955430Aug 13 2012 — edited Aug 14 2012
Hi,

Iam developing one jar file.
In my project ther is one .txt file and one XYZ folder.
.txt file contains some file List,In my project Iam reading the .txt file and creating the files listed under the .txt file
for this i am just passing the file path as File f = new File(".
FileList.txt)

I just exported this project as an executable jar.
While running from the eclipse workspace, its working fine.But while iam running as a jar file it's unable to find the "FileList.txt" which was bundled in Jar.
As for my knowledge "." will fetch from the directory where we are running.Instead of passing as ".
FileList.txt", how i have to pass to make the jar file to find the .txt file

Could anyone help me in this

Thanks in advance
Vanisree

Comments

EJP
As for my knowledge "." will fetch from the directory where we are running.
Exactly. A directory. Not a JAR file. If you want to access it via File, you can't put it into the JAR. if you want to put it into the JAR, you need to use Class.getResource() and friends.
955430
Iam using the following Code.

String path = getClass().getClassLoader().getResource("fileList.txt").getPath();
This is printing the path, but while running in the eclipse its throwing "FileNotFound Exception", even the file exists
gimbal2
You're going about it the wrong way. The file is IN the jar right? Then its not on the actual filesystem, so you're not going to be using File/IO classes to access it.

In stead, use getClass().getClassLoader().getResourceAsStream() to directly get an inputstream to the file to be able to load it.
955430
Thanks boss..
That works fine.
Iam able to get the resource file path using getResourceAsStream.

How can i get the folder path in the jar???

Thanks in advance
Vanisree
EJP
I've answered that. getResource(). It gives you a URL, or null. But it isn't a path you can treat as though it could be fed to a File, as everybody has already told you.
1 - 5
Locked Post
New comments cannot be posted to this locked post.

Post Details

Locked on Sep 11 2012
Added on Aug 13 2012
5 comments
519 views