Serialized files don't work when project is converted to executable jar?

前端 未结 4 1158
温柔的废话
温柔的废话 2021-01-06 17:34

I made my java project into an executable jar using the export to jar option in eclipse. The jar runs as expected, except that it does not use any of the serialized files. I

4条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-06 17:43

    You can use

    AClass.class.getResource(String str);
    //or
    AClass.class.getResourceAsStream(String str);
    

    AClass: one of your classes.

    str: file location which you want to read.

    For example;

    if your class hierarchy seem like this:

    +src
        +-com
             +-test
                 |-AClass.java
                 +-util
                     +-PrintUtil.java
                 +-resources
                     |-Bouble.png
                     |-Mouse.png
                     +-Ocean.png
    

    and for reading "Mouse.png" image, you can this with a lots of ways:

    AClass.class.getResource("/resources/Mouse.png");
    //or
    PrintUtil.class.getResource("../resources/Mouse.png");
    ...
    

提交回复
热议问题