aspose file tools*
The moose likes Java in General and the fly likes FileNotFoundException in wired situation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "FileNotFoundException in wired situation" Watch "FileNotFoundException in wired situation" New topic
Author

FileNotFoundException in wired situation

naved momin
Ranch Hand

Joined: Jul 03, 2011
Posts: 692

in my application i have put the file in my source file folder and taken the file as a resource like

now some where in my application i am writing a word "On" in that file, and to do that i m doing something like this

its running fine from netbeans but when i build the project and run that .jar file
it throws an exception saying fileNotFound for more info on exception please have a look at below image


[Thumbnail for Untitled.png]



The Only way to learn is ...........do!
Visit my blog http://inaved-momin.blogspot.com/
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10451
    
    8

Please copy paste the exception stack trace (inside code tags) instead of posting an image of it. Many people, including me, have images disabled.


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
naved momin
Ranch Hand

Joined: Jul 03, 2011
Posts: 692

Maneesh Godbole wrote:Please copy paste the exception stack trace (inside code tags) instead of posting an image of it. Many people, including me, have images disabled.

Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10451
    
    8


The error message is self explanatory.
Do you have the file at this location?
naved momin
Ranch Hand

Joined: Jul 03, 2011
Posts: 692

Maneesh Godbole wrote:
The error message is self explanatory.
Do you have the file at this location?

tell the stuff which i don't know, i m not new to Java i know that this is not a location of a file.
i have even tried to remove the exclamation and then build the absolute path but that also, is not working
can any one tell me the main reason of this exception. i know that the JVM is not able to find the file but my question is why he is not able to find the file ?
because i have tried specifying the absolute path
i have tried specifying the relative path
but still result is failure
does any one know any thing else to get ahead..?
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

naved momin wrote:tell the stuff which i don't know,


No need to be rude.

i m not new to Java


Yes, you are. Not that there's anything wrong with that.

i have tried specifying the absolute path
i have tried specifying the relative path


Have you tried reading the docs for that getResource() method that you're using? Have you tried looking at the URL that you're getting back? Have you tried looking at its getPath() method? Have you tried understanding that you can't represent a file inside a jar just using java.io.File?

Also, please UseRealWords.(⇐click)
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4642
    
    5

The file: protocol doesn't use a backslash.

edit Also, a jar entry isn't a file.


luck, db
There are no new questions, but there may be new answers.
naved momin
Ranch Hand

Joined: Jul 03, 2011
Posts: 692

Jeff Verdegan wrote:
naved momin wrote:tell the stuff which i don't know,


No need to be rude.

i m not new to Java


Yes, you are. Not that there's anything wrong with that.

i have tried specifying the absolute path
i have tried specifying the relative path


Have you tried reading the docs for that getResource() method that you're using? Have you tried looking at the URL that you're getting back? Have you tried looking at its getPath() method? Have you tried understanding that you can't represent a file inside a jar just using java.io.File?

Also, please UseRealWords.(⇐click)

Soory jeff and anyone to whom have feel bad with my words, yes, i accept it i m pretty much new to Java (I know few things but still need to learn more)
naved momin
Ranch Hand

Joined: Jul 03, 2011
Posts: 692

Jeff Verdegan wrote:
naved momin wrote:tell the stuff which i don't know,


No need to be rude.

i m not new to Java


Yes, you are. Not that there's anything wrong with that.

i have tried specifying the absolute path
i have tried specifying the relative path


Have you tried reading the docs for that getResource() method that you're using? Have you tried looking at the URL that you're getting back? Have you tried looking at its getPath() method? Have you tried understanding that you can't represent a file inside a jar just using java.io.File?

Also, please UseRealWords.(⇐click)

ok so what should i do so that i can read and write a file by taking it as a resource?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18675
    
    8

naved momin wrote:ok so what should i do so that i can read and write a file by taking it as a resource?


First let me remind you, a jar entry isn't a file. The way to read it is to use the getResourceAsStream() method (instead of getResource). This gives you an InputStream which you can read from. The way to write it is, there isn't a way to write it. You can't write to a jar entry.
Jayesh A Lalwani
Bartender

Joined: Jan 17, 2008
Posts: 2409
    
  28

Like Paul said, you cannot write to a file that is inside a jar file. The reason it works from netbeans is because doesn't jar your classes and resources, and the call to getResource method returns the path to the file from your classpath. You shouldn't write to a file that is in your classpath.

I think at this point the question that need to be asked is why are you writing to a file in your classpath
naved momin
Ranch Hand

Joined: Jul 03, 2011
Posts: 692

You can't write to a jar entry.

beautiful, answer i like that , i have just figured out the same, thanks jayesh and paul..for the answer
and jayesh, many times i have read the blog which says if your application use a temp area say file to store 1 or 2 words of information. put it as a resource so that will be portable and you do not have to create it again and again while deploying your application .. so i was trying that but it was not working
also i need to ask a question know if I cannot write to a file inside jar entry than what are the steps I need to follow to reduce my deployment efforts?
Jayesh A Lalwani
Bartender

Joined: Jan 17, 2008
Posts: 2409
    
  28

You always put read only files inside your Jar. This is usually things like property files, but can include a lot of other things like XML configuration files, velocity templates etc. These only contain things that your application needs to read

If your application wants to create a temporary file, you can create a file using File.createTempFile

If your application needs to store data in files, then you will have to provide some way the user can tell you where to store the file. You don't want to store the file just about anywhere because you are running into a risk that you will make the user's disk full. You should provide the user some way of specifying a path. You cna do this in many ways.

Many applications do this using environment variables. Usually, they ask the user to set a "home" environment variable, and the files are stored in a folder relative to the home folder.

Or if you want zero configuration, you can get the system property user.home. THis will always point to the current user's home folder which is generally assumed to be read-write. Please keep in mind that you shouldn't store large files in the home folder, and if you do, you should provide a way for the user to override and specify a differrent folder.
naved momin
Ranch Hand

Joined: Jul 03, 2011
Posts: 692


thanks Jayesh, great explanation bro, thanks for your time and support, appreciate
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: FileNotFoundException in wired situation