• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

Running jar file resources can't be found

 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've a simple maven project and which I have successfully packaged on the terminal (mvn package)
When executing the jar file it gives an error that a text file can't be found which contains the data to be read in.
I'm using OS X.

My project structure is:
NameOfProject
src
   main
       java

    resources
        data.txt
        data2.txt
    META-INF
        MANIFEST.MF

The MANIFEST.MF contains:
Manifest-Version: 1.0

After running the jar file it gives the following error:
File could not be found file:/Users/Mike/Projects/NameOfProject/target/application-app-1.0-SNAPSHOT.jar!/data.txt (No such file or directory)Exception in thread "main" java.lang.NullPointerException

When I run the application from my ide it works fine.
When I look inside the jar file I can see both data.txt files.

I'm not sure but I see a ! at the end of  the name of the jar file.
What causes this error and how can I solve this?
 
Ranch Foreman
Posts: 323
4
Android Tomcat Server Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would suggest you to put .txt file inside src folder.
 
Ranch Foreman
Posts: 22
1
Netbeans IDE Oracle Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maybe because the  txt file is deleted you need to create a new .txt file with the same file name in your Code!
I've experience it in C language but just my opinion!😊
Have A Nice Day!
 
Mike Dalton
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Randy
That wouldn't solve it. The file should go in the resources folder. When running the application in my IDE it works perfectly. No errors.

@Jasper
The txt file is inside the jar file. I can see it.
 
Randy Tong
Ranch Foreman
Posts: 323
4
Android Tomcat Server Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does this helped?
http://www.avajava.com/tutorials/lessons/where-do-i-put-resources-in-my-maven-project.html
 
Mike Dalton
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I checked and I got the same structure.
 
Saloon Keeper
Posts: 2619
329
Android Eclipse IDE Angular Framework MySQL Database TypeScript Redhat Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How are you loading the resource contents?  When the resource is in a jar, it probably easiest to use getResourceAsStream

For example:
 
Mike Dalton
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That could be it!
Could someone help me to convert it?

 
Ron McLeod
Saloon Keeper
Posts: 2619
329
Android Eclipse IDE Angular Framework MySQL Database TypeScript Redhat Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Mike Dalton wrote:Could someone help me to convert it?


If you look at my example, you can see that a BufferedReader is created using a reference to an InputStream:
    new BufferedReader(new InputStreamReader(in))

Do that instead of creating a BufferedReader based on a File.
 
Ron McLeod
Saloon Keeper
Posts: 2619
329
Android Eclipse IDE Angular Framework MySQL Database TypeScript Redhat Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, you should prefix your resource name with a  /  otherwise your code will be looking for the resource inside your application package (and will not find it).
 
Mike Dalton
Greenhorn
Posts: 26
  • Likes 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I got it.



Now changing the last part to get the file read in successfully.

EDIT: Also changed it and it is working now.
Thanks all for the support!
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!