• Post Reply Bookmark Topic Watch Topic
  • New Topic

Problem when executing runnable jar file.  RSS feed

 
Prabhat Gupta
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ranchers !!

My Project has following structure
Test-->com.company.Example.Test1
-->Rules/File1.txt

In class Test1 i am reading File1.txt by following code



When i run this class (Using eclipse)it works fine and show the content of File1.txt.

But the problem is coming when i create a jar file(which include class with main() method also).When i try to run this by java -jar Test.jar it is showing file not found exception for Rules/File1.txt file.
I guess i need to set a class-path somewhere so that when i execute jar file it can locate File1.txt.

I have noticed that when i unzip the jar file its content structure is not same as actual project Test. Actual project contains the File1.txt under Rules folder which is directly under project Test .But the unzipped jar contains File1.txt directly under Test and also Rules folder is not created.

Please suggest possible solution for above problem.

Thanks in advance.

-Prabhat
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's because the file doesn't exist--it's embedded in the jar. If you're bundling the text file with the jar then you need to read it as a resource off of the classpath.

That said, doing so may not be an idea solution if you want those rules to be changeable by the end user.
 
Prabhat Gupta
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have checked the text file exists in jar by unzipping it.
if you want those rules to be changeable by the end user.

No the text file and its content won't be changeable by the end user.

If you're bundling the text file with the jar then you need to read it as a resource off of the classpath.

How do we do it? can you please elaborate it.
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check out Class.getResourceAsStream and its Javadoc information.
 
Shashank Ag
Ranch Hand
Posts: 88
Java Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear prabhat,
You need to put your files as:
let mytestfolder be base folder in which following files should exsist:
/mytestfolder/Test.jar
/mytestfolder/Rules/File1.txt
Then your jar will be able to access File1.txt
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sounds like something too difficult for "beginning". Moving.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!