简体   繁体   English

Eclipse导出文件jar不包含文件夹

[英]Eclipse export file jar doesn't contain folder

I have created a runnable jar with eclipse. 我用Eclipse创建了一个可运行的jar。 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. 当我运行可运行的jar时,它将无法正常工作,除非与该jar位于同一文件夹中,文件夹调查表也是如此。 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. 我尝试了一些我在stackoverflow中阅读的解决方案,例如将文件夹调查表添加为源文件夹,也从属性-> Java构建路径->库->添加类文件夹并添加调查表文件夹,但仍然无法正常工作。

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 另外,当我解压缩罐子时,没有文件夹问卷

To add the folder "questionnaire" into your jar, you need to set the parent directory of "questionnaire" as a source folder. 要将“ questionnaire”文件夹添加到您的jar中,您需要将“ questionnaire” 的父目录设置为源文件夹。

Then, you'll have a questionnaire "folder" into your jar. 然后,将一个调查表“文件夹”放入您的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. 但是,您将无法通过File API访问它,因为它不是常规文件夹,而是进入jar的条目。 If you simply need to read files from that folder, have a look at the Class.getRessourceAsStream(String) method. 如果只需要从该文件夹中读取文件,请查看Class.getRessourceAsStream(String)方法。

It is not a good idea to export a folder within a jar. 在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. 此外,如果以某种方式导出文件,则无法访问该文件夹的文件。再次需要使用JarFile类来读取文件夹的内容。

One thing you can do is create the jar without the folder inside it. 您可以做的一件事就是创建一个不带文件夹的jar。 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. 运行jar时,请确保jar和问卷文件夹位于同一目录中。 that will work efficiently. 那将有效地工作。

Read this thread 阅读此主题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM