0

I'm working on a maven modular project that has a structure that look like at the following:

|-- parent
    |-- model
    --pom.xml
    |-- services
    --pom.xml
    |-- web-app
    --pom.xml

No in my service module I have some resources in src/main/resources. But when i try to get them:

String fileName = getClass().getResource("/myFile.txt").getPath();
map.put("myReport",JasperCompileManager.compileReport(fileName));

the file name has the following value

file:/home/myUser/apache-tomcat-8.0.3/webapps/myApp/WEB-INF/lib/services-0.0.1-SNAPSHOT.jar!/myFile.txt

and then when i try to use it i get :

Caused by: java.io.FileNotFoundException: file:/home/myUser/apache-tomcat-8.0.3/webapps/myApp/WEB-INF/lib/services-0.0.1-SNAPSHOT.jar!/myFile.txt

Also i saw others post (not resolved) like Get file in the resources folder in Java where somebody said the is not possible to get resource from different modules.

Is this true? How can I do to fix it?

Community
  • 1
  • 1
Skizzo
  • 2,883
  • 8
  • 52
  • 99
  • Looks like it's working fine, but you cannot use a `File` to access opaque paths. Post what you are trying to do. In the first instance I would point you to [`getResourceAsStream`](http://docs.oracle.com/javase/7/docs/api/java/lang/Class.html#getResourceAsStream(java.lang.String)). – Boris the Spider Jun 08 '14 at 09:41
  • your path has "!" in jar file name. Is it by mistake? – Devesh Jun 08 '14 at 10:03
  • @BoristheSpider I Updated my question and there you can see that I'm trying to compile a report that i made with jasper report. I often used getResourceAsStream (and works fine) then I want to understand why getResorce("/myFile").getPath() try to acces at the .jar instead that at the classpath like getResourceAsStream does – Skizzo Jun 08 '14 at 10:16
  • 1
    Because, I'm assuming here, `JasperCompileManager.compileReport` takes a `File` path. What you have is not a file path, it's an _opaque_ path. The file is _inside_ an `.jar` and therefore cannot be opened like a normal file. The `ClassLoader` is specifically designed to load files from jars - i.e. classes. You can use the stream to create a copy _outside_ the jar and then this will work. – Boris the Spider Jun 08 '14 at 10:34

0 Answers0