Pregunta

Im trying to read a file from a path. This is my sample code;

String path = "repository"+ File.separator +"resources"+ File.separator +"api_templates";

        String fileName = path + TEMPLATE_FILE_PREFIX + type + ".xml";
        InputStream in = null;
        try {
           log.info("##############File path#############"+fileName);
            in = Thread.currentThread().getContextClassLoader().getResourceAsStream(fileName);

Here i get inputstream as null. I suspect the system could not load the file. But when i print my filepath, it correctly prints my file path.

This problem occurs only when i try to run my server as windows service, using "yajsw".

What might be the issue?

Edit:

My Sample wrapper-conf file;

#********************************************************************
 # working directory
 #******************************************************************** 

wrapper.working.dir=${my_home}

 ............

 wrapper.java.additional.2 = -Xms256m 
 wrapper.java.additional.3 = -Xmx1024m 
 wrapper.java.additional.4 = -XX:MaxPermSize=256m 
 wrapper.java.additional.5 = -XX:+HeapDumpOnOutOfMemoryError 
 wrapper.java.additional.6 = -XX:HeapDumpPath=${my_home}\\repository\\logs\\heap-dump.hprof  
 wrapper.java.additional.7 = -Djava.endorsed.dirs=${my_home}\\lib\\endorsed;${java_home}\\jre\\lib\\endorsed
¿Fue útil?

Solución

This is because of a classpath issue between resources and files. We can not use classloaders to access files. For that we need to use File, filereader, file input stream. After changing like this everything works fine;

InputStream in = new FileInputStream(filePath);

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top