Java problems with accessing resource files when application is used as library -


i have 2 java applications. 1 application contain resource files , used library other java application.

first app com.test.resourceusing.mainclass.java contains res/base.xml resource file.

package com.test.resourceusing;  import java.io.bufferedinputstream; import java.io.file;  import java.net.malformedurlexception; import java.net.url;  import java.util.scanner;  public class mainclass { public mainclass() {     super(); }  public static void main(string[] args) {     mainclass main = new mainclass();     try {         main.start();     } catch (malformedurlexception e) {     } }  public void start() throws malformedurlexception {     url url = getclass().getresource("res/base.xml");     system.out.println(url.getpath());     system.out.println(url.getfile());     file f = new file(url.getfile());     if (f.exists()) {         system.out.println("file exist!");          bufferedinputstream result = (bufferedinputstream)                getclass().getresourceasstream("res/base.xml");         scanner scn = new scanner(result);         while(scn.hasnext()){             system.out.println(scn.next());         }     } else {         system.out.println("not working! :(");         } } 

}

result is:

/c:/work/projects/resourceusing/classes/com/test/resourceusing/res/base.xml /c:/work/projects/resourceusing/classes/com/test/resourceusing/res/base.xml file exist! <?xml version='1.0' encoding='utf-8'?> <schema> </schema> 

then create .jar file contains resource files , try use library in other application.

second app: resourcetest.mainclasstest.java

package resourcetest;  import com.test.resourceusing.mainclass;  import java.net.malformedurlexception;  public class mainclasstest {   public mainclasstest() {     super();   }    public static void main(string[] args) {     mainclass main = new mainclass();     try {         main.start();     } catch (malformedurlexception e) {     }   } } 

result is:

file:/c:/work/projects/resourceusing/deploy/archive1.jar!/com/test/resourceusing/res/base.xml file:/c:/work/projects/resourceusing/deploy/archive1.jar!/com/test/resourceusing/res/base.xml not working! :( 

i don't understand why it's not working, there problems in code? or solution not possible in java?

do see difference in location of files?

/c:/work/projects/resourceusing/classes/com/test/resourceusing/res/base.xml file:/c:/work/projects/resourceusing/deploy/archive1.jar!/com/test/resourceusing/res/base.xml 

you cannot access resource located in jar file file api.

your code on way. simple edit should work:

public void start() throws ioexception {     url url = getclass().getresource("res/base.xml");     if (url != null) {         system.out.println(url.getpath());         system.out.println(url.getfile());         system.out.println("file exist!");          try(inputstream result = url.openstream()) {             try(scanner scn = new scanner(result)) {                 while(scn.hasnext()) {                     system.out.println(scn.next());                 }             }         }     } else {         system.out.println("not working! :(");         } } 

Comments