how do you make getResourceAsStream work while debugging Java in Eclipse?

后端 未结 6 1185
清酒与你
清酒与你 2020-12-03 06:05

This is mind-boggling... I can make getResource() and getResourceAsStream() work properly when I run Java on my packaged JAR file that includes a text file. (for reference s

6条回答
  •  情话喂你
    2020-12-03 06:52

    I have a hunch it has to do with CLASSPATH and/or where Eclipse puts the .class files it autocompiles.

    In the Run Configuration for however you are launching your application, go to the Classpath tab and add the root folder of the path that you are using to access the file.

    So for example if you are trying to load "blah/file.txt" and the file resides in "folder/blah/file.txt", add "folder" to the classpath. If you are trying to load the resource with just "file.txt", then add "folder/blah".

提交回复
热议问题