How to access and read a .txt file from a runnable jar

后端 未结 2 1489
无人共我
无人共我 2021-01-27 09:09

How can i load a text file with a runnable .jar file, It works fine when it\'s not jarred but after i jar the application it can\'t locate the file. Here\'s what i\'m using to l

2条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-01-27 09:40

    if this is your package structure:

    Correct way of retrieving resources inside runnable or.jar file is by using getResourceAsStream.

    InputStream resourceStream =  TestResource.class.getResourceAsStream("/resources/PUT_Request_ER.xml");
    

    If you do getResource("/resources/PUT_Request_ER.xml"), you get FileNotFoundException as this resource is inside compressed file and absolute file path doesn't help here.

提交回复
热议问题