简体   繁体   English

在资源中添加docx文件并创建可执行jar

[英]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. 资源文件夹中的docx文件,请使用位于同一应用程序另一个软件包中的类编写的代码中的那些文件。
  • And then I want to make an executable jar out of it which will be working on windows. 然后,我想用它制作一个可执行的jar,它将在Windows上运行。

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.

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