• 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

Problem when executing runnable jar file.

 
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Check out Class.getResourceAsStream and its Javadoc information.
 
Ranch Hand
Posts: 88
Spring Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sounds like something too difficult for "beginning". Moving.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic