Win a copy of Node.js Design Patterns: Design and implement production-grade Node.js applications using proven patterns and techniques this week in the Server-Side JavaScript and NodeJS forum!
  • 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
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Runnable JAR won't recognize .txt file

 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I apologize if this is a duplicate question, but I can't seem to find any solution that work for my issue. I have a program that requires the use of a .txt file. I have successfully used the file if I run from Eclipse using:



But when I export it to a runnable JAR it can't find it. You all have solved this issue for me before for a font file, but I can't seem to figure it out for a normal .txt file. In researching online I have come across this:



But that leaves it as type InputStream, and I need it to be a file. Can anybody help me out with including it in a JAR, or maybe in how to use InputStream to create the File? Thanks in advance.
 
Rancher
Posts: 978
23
Netbeans IDE Oracle MySQL Database Tomcat Server C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You have one of two problems--either you made a typo like you did in your example or your file was not included in your JAR.
 
Marshal
Posts: 3657
516
Android Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You could use FileOutputStream and read from the InputStream and write to the OutputStream.
 
Joel Blake
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yeah disregard the typo in the post, my code doesn't reflect that. I agree it's not included in the JAR, that's the issue I'm having. It needs to be added as a resource I think.. I know this is basic stuff, sorry, I'm trying to get a grip on it here...
 
Ron McLeod
Marshal
Posts: 3657
516
Android Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Joel Blake wrote:I agree it's not included in the JAR, that's the issue I'm having


Are you sure it's not there? Use an un-zipper application (something like 7zip) and check. I did what I think you did (placed the file at the root of the source tree), and found it in the jar.

 
Joel Blake
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's there. But it's not reading it. I think the path is changing when it gets archived....
 
Ron McLeod
Marshal
Posts: 3657
516
Android Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You posted this code (I corrected the typo on the file name):

Are you able to read from the InputStream?
 
Sheriff
Posts: 26796
82
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Remember that using a relative URL (as both you and Ron did) means that the resource should be in the same folder with the class in the JAR. And when I say "the class" I mean the class where that code is located. If you want to put the resource in the JAR's root folder then use an absolute URL:

 
Ron McLeod
Marshal
Posts: 3657
516
Android Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Paul - what you said sounds logical, but I built a test app to verify and it didn't prove-out. Am I doing something wrong?

D:\TEMP>java -jar ResourceFileTest.jar
Absolute: null
Relative: ok

My file structure:
D:.
|
+---bin
|   |   config.txt
|   |
|   \---com
|       \---mcleod
|               ResourceFileTest.class
|
\---src
    |   config.txt
    |
    \---com
        \---mcleod
                ResourceFileTest.java
 
Paul Clapham
Sheriff
Posts: 26796
82
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I always use the getResource() method of Class, rather than ClassLoader:



At least the API documentation for Class.getResource() has a halfway comprehensible explanation of how the method works, whereas the docs for ClassLoader.getResource() are totally vague. Apparently they work differently. Why that is, I have no idea but it don't impress me much.

So what I said refers to Class.getResource(). Sorry for the confusion.
 
Ron McLeod
Marshal
Posts: 3657
516
Android Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your right about the ClassLoader doc being vague -- it only says "The name of a resource is a '/'-separated path name that identifies the resource." - no mention of how to actually form the name.

D:\TEMP>java -jar ResourceFileTest.jar
Absolute (ClassLoader): null
Relative (ClassLoader): ok
Absolute (Class): ok
Relative (Class): null
 
He's giving us the slip! Quick! Grab this tiny ad!
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic