• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

FileNotFoundException in wired situation

 
Ranch Hand
Posts: 692
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Untitled.png
[Thumbnail for Untitled.png]
 
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please copy paste the exception stack trace (inside code tags) instead of posting an image of it. Many people, including me, have images disabled.
 
naved momin
Ranch Hand
Posts: 692
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

The error message is self explanatory.
Do you have the file at this location?
 
naved momin
Ranch Hand
Posts: 692
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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..?
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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)
 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The file: protocol doesn't use a backslash.

edit Also, a jar entry isn't a file.
 
naved momin
Ranch Hand
Posts: 692
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 692
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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?
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Rancher
Posts: 2759
32
Eclipse IDE Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 692
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Rancher
Posts: 2759
32
Eclipse IDE Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 692
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

thanks Jayesh, great explanation bro, thanks for your time and support, appreciate
 
reply
    Bookmark Topic Watch Topic
  • New Topic