getResourceAsStream returns null

前端 未结 16 2332
臣服心动
臣服心动 2020-11-22 04:00

I\'m loading a text file from within a package in a compiled JAR of my Java project. The relevant directory structure is as follows:

/src/initialization/Life         


        
16条回答
  •  -上瘾入骨i
    2020-11-22 04:40

    What you really need is a full absolute classPath for the file. So instead of guessing it, try to find out the ROOT and then move the file to a better location base one <.war> file structures...

    URL test1 = getClass().getResource("/");
    URL test2 = getClass().getClassLoader().getResource("/");            
    URL test3 = getClass().getClassLoader().getResource("../");
    
    logger.info(test1.getPath()); 
    logger.info(test2.getPath());
    logger.info(test3.getPath());
    

提交回复
热议问题