Where does Java put resource files when I JAR my program?

前端 未结 3 499
一向
一向 2021-01-20 23:26

Been looking for this for the past 2 hours and can\'t find anything (I\'ve found solutions to the same problem but with images, not text files).

Pretty much, I made a pr

3条回答
  •  心在旅途
    2021-01-20 23:55

    A more robust way to get a file whether you are running from Eclipse or a JAR is to do

    MyClass.getResource("items.txt")
    

    where MyClass is a class in the same package (folder) as the resource you need.

    If Eclipse is not putting the file in your JAR you can go to

    Run -> Run Configurations -> -> Classpath tab -> Advanced -> Add Folders

    Then add the folder containing your file to the classpath. Alternatively, export the Ant script and create a custom build script.

提交回复
热议问题