Reputation: 727
I have created a runnable jar with eclipse. Inside my project I have a folder called questionnaire which contains some text files I use. When I run my runnable jar it doesn't work except if I have in the same folder as the jar the folder questionnaire. I tried some solutions that I read in stackoverflow like add folder questionnaire as a source folder and also from properties -> java build path -> libraries ->Add class folder and add questionnaire folder but still doesn't work.
This is the code:
File srcFolder = new File(".\\questionnaire");
if(!srcFolder.exists()){
System.out.println("Directory does not exist.");
//just exit
System.exit(0);
}
And every time I get message "Directory does not exist". Also when I unzip the jar there is no folder questionnaire
Upvotes: 0
Views: 1256
Reputation: 8946
It is not a good idea to export a folder within a jar. Moreover you cannot acces the files of the folder if by somehow you export them You again need to use JarFile class to read the contents of the folder.
One thing you can do is create the jar without the folder inside it. Use this line
File srcFolder = new File("questionnaire");
and when you run the jar make sure the jar and the questionnaire folder are in the same directory. that will work efficiently.
Read this thread
Upvotes: 1
Reputation: 6650
To add the folder "questionnaire" into your jar, you need to set the parent directory of "questionnaire" as a source folder.
Then, you'll have a questionnaire "folder" into your jar. However, you won't be able to access it via the File API, since it won't be a regular folder but an entry into your jar. If you simply need to read files from that folder, have a look at the Class.getRessourceAsStream(String) method.
Upvotes: 2