• Post Reply Bookmark Topic Watch Topic
  • New Topic

Runnable JAR won't recognize .txt file  RSS feed

 
Joel Blake
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • 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.
 
Les Morgan
Rancher
Posts: 779
19
C++ Java MySQL Database Netbeans IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • 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.
 
Ron McLeod
Bartender
Posts: 1602
232
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could use FileOutputStream and read from the InputStream and write to the OutputStream.
 
Joel Blake
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • 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
Bartender
Posts: 1602
232
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • Mark post as helpful
  • send pies
  • 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: 20
  • Mark post as helpful
  • send pies
  • 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
Bartender
Posts: 1602
232
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • Mark post as helpful
  • send pies
  • 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?
 
Paul Clapham
Sheriff
Posts: 22828
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • 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
Bartender
Posts: 1602
232
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • Mark post as helpful
  • send pies
  • 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: 22828
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • 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
Bartender
Posts: 1602
232
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • Mark post as helpful
  • send pies
  • 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
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!