• 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

Running jar file resources can't be found

 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Rancher
Posts: 662
10
Android Tomcat Server Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would suggest you to put .txt file inside src folder.
 
Greenhorn
Posts: 22
1
Netbeans IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Rancher
Posts: 662
10
Android Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I checked and I got the same structure.
 
Marshal
Posts: 4491
572
VSCode Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That could be it!
Could someone help me to convert it?

 
Ron McLeod
Marshal
Posts: 4491
572
VSCode Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 4491
572
VSCode Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
reply
    Bookmark Topic Watch Topic
  • New Topic