简体   繁体   中英

Add docx file in resources and create executable jar

I want to add

  • docx files in resources folder, use those files in code written in class located at another package of same application.
  • And then I want to make an executable jar out of it which will be working on windows.

I read its not easy to make such jar :( and there is no fool prroof way...

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 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

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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