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

前端 未结 3 497
一向
一向 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:43

    To the point, the FileReader can only read disk file system resources. But a JAR contains classpath resources only. You need to read it as a classpath resource. You need the ClassLoader for this.

    Assuming that Foo is your class in the JAR which needs to read the resource and items.txt is put in the classpath root of the JAR, then you should read it as follows (note: leading slash needed!):

    InputStream input = Foo.class.getResourceAsStream("/items.txt");
    reader = new BufferedReader(new InputStreamReader(input, "UTF-8"));
    // ...
    

    Or if you want to be independent from the class or runtime context, then use the context class loader which operates relative to the classpath root (note: no leading slash needed!):

    ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
    InputStream input = classLoader.getResourceAsStream("items.txt");
    reader = new BufferedReader(new InputStreamReader(input, "UTF-8"));
    // ...
    

    (UTF-8 is of course the charset the file is encoded with, else you may see Mojibake)

    0 讨论(0)
  • 2021-01-20 23:50

    Get the location of your jar file

    Firstly create a folder(say myfolder) and put your files inside it

    Consider the following function

    public String path(String filename)
    {
    URL url1 = getClass().getResource("");
    String ur=url1.toString();
    ur=ur.substring(9);
    String truepath[]=ur.split("myjar.jar!");
    truepath[0]=truepath[0]+"myfolder/";
    truepath[0]=truepath[0].replaceAll("%20"," ");
    return truepath[0]+filename;
    }//This method will work on Windows and Linux as well.
    //You can alternatively use the following line to get the path of your jar file
    //classname.class.getProtectionDomain().getCodeSource().getLocation().getPath();
    

    Suppose your jar file is in D:\Test\dist

    Then path() will return /D:/Test/dist/myfolder/filename

    Now you can place 'myfolder' inside the folder where your jar file is residing

    OR

    If you want to access some read-only file inside your jar you should copy it to one

    of your packages and can access it as

    yourClassname.getResource("/packagename/filename.txt");

    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题