[英]Add docx file in resources and create executable jar
I want to add 我要添加
I read its not easy to make such jar :( and there is no fool prroof way... 我读到制作这样的罐子并不容易:(并且没有愚蠢的profof方式...
I have tried searching for it on net and found I will have to create URL and then file and then use it... however, when I use below code, I am not able to get URL itself... 我尝试过在网上搜索它,发现我必须先创建URL,然后归档然后使用它...但是,当我使用以下代码时,我无法获取URL本身...
URL urlOfDraftInSamePackage = CreateDraft.class.getResource("Draft_in_same_package.docx");
System.out.println("urlOfDraftInSamePackage is "+urlOfDraftInSamePackage.toString());
//This prints : urlOfDraftInSamePackage is file:/D:/aditya_workspace/SampleDraftMaker/bin/draftProcessing/Draft_in_same_package.docx //打印:urlOfDraftInSamePackage是文件:/ D:/aditya_workspace/SampleDraftMaker/bin/draftProcessing/Draft_in_same_package.docx
URL urlOfDraftInResourceFolder = CreateDraft.class.getResource("resouces/Draft_Apartment.docx");
System.out.println("urlOfDraftInResourceFolder is "+urlOfDraftInResourceFolder.toString());
//this gives null pointer exception
URI uri = null;
try {
uri = urlOfDraftInSamePackage.toURI();
File file = new File(uri);
System.out.println("file made");
} catch (URISyntaxException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
below is my folder structure: 下面是我的文件夹结构:
can anyone pls help me in creating such executable jar using eclipse? 谁能帮助我使用eclipse创建此类可执行jar?
Thanks In Advance!!! 提前致谢!!!
Following code works for me: 以下代码对我有用:
public static void testResource() throws IOException {
InputStream stream = Deserializace.class.getResourceAsStream("resources/ser.log");
BufferedReader reader = new BufferedReader(new InputStreamReader(stream));
String s;
while ( (s = reader.readLine()) != null) {
System.out.println(s);
}
}
Build directory structure: 建立目录结构:
Test.class
resources/ser.log
You must ensure that your resource directory is copied to correct place. 您必须确保将资源目录复制到正确的位置。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.