How to read a file from jar in Java?

后端 未结 5 1347
我寻月下人不归
我寻月下人不归 2020-11-22 05:45

I want to read an XML file that is located inside one of the jars included in my class path. How can I read any file which is included in the jar?<

5条回答
  •  一个人的身影
    2020-11-22 06:22

    Check first your class loader.

    ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
    
    if (classLoader == null) {
        classLoader = Class.class.getClassLoader();
    }
    
    classLoader.getResourceAsStream("xmlFileNameInJarFile.xml");
    
    // xml file location at xxx.jar
    // + folder
    // + folder
    // xmlFileNameInJarFile.xml
    

提交回复
热议问题