How to list the files inside a JAR file?

前端 未结 16 2584
温柔的废话
温柔的废话 2020-11-22 00:40

I have this code which reads all the files from a directory.

    File textFolder = new File(\"text_directory\");

    File [] texFiles = textFolder.listFiles         


        
16条回答
  •  夕颜
    夕颜 (楼主)
    2020-11-22 01:16

    So I guess my main problem would be, how to know the name of the jar where my main class lives.

    Assuming that your project is packed in a Jar (not necessarily true!), you can use ClassLoader.getResource() or findResource() with the class name (followed by .class) to get the jar that contains a given class. You'll have to parse the jar name from the URL that gets returned (not that tough), which I will leave as an exercise for the reader :-)

    Be sure to test for the case where the class is not part of a jar.

提交回复
热议问题