Listing the files in a directory of the current JAR file

后端 未结 5 1811
轮回少年
轮回少年 2020-12-16 15:15

I am making a game in JAVA where I want to come up with a list of files in a certain directory in my jar so I can make sure to have a list of those classes to be used in the

5条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-16 15:42

    Probably the best approach is to list the classes at compile time.

    There is a fragile runtime approach. Take you Class (MyClass.class of this.getClass()). Call getProtectionDomain. Call getCodeSource. Call getLocation. Call openConnection. (Alternatively open a resource.) Cast to JarURLConnection. Call getJarFile. Call entries. Iterate through checking getName. I really do not recommend this approach.

提交回复
热议问题