• Post Reply Bookmark Topic Watch Topic
  • New Topic

File not found inside .ear

 
Joao Longo
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello people!

Inside my .ear

-META-INF
-lib
-ejb-shared.jar
-ejb.jar
-com/ejb/... (classes)
-fileXml.xml (file I'm trying to access)
-web.war


Some description:

* "ejbshared" contains some ejbs and JPA entities
* "ejb" contains some ejbs and JPA entities and uses "ejb-shared" project


The problem is that I can't access fileXml.xml. Inside an EJB bean (of ejb.jar) I've done:

File f = new File("fileXml.xml");
System.out.println(f.exists()); // returns false!

I don't know why, but it seems that fileXml.xml is not in the classpath, althougth it's present in the .ear, or maybe I'm doing things in the wrong way!

 
Paul Clapham
Sheriff
Posts: 21889
36
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just because something is in the classpath, that doesn't mean that it's a file, and it especially doesn't mean that it's in your current working directory. Which are the two assumptions you have made.

The way to get at that resource, assuming that it's in the classpath in that location in the EAR, is like this:
 
Joao Longo
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot!

But the fact is that I'm using a lib that needs the file path of this file. Is it possible to get?

 
Paul Clapham
Sheriff
Posts: 21889
36
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's a pity, because the resource might not even be in a file. Are you sure your library doesn't have overloaded methods which accept InputStream instead of File? (Well-designed libraries would have them, and all of the XML libraries I am familiar with do have them.)

You could try the getResource() method, instead of getResourceAsStream(). It returns a URL, from which (if you are lucky) you can extract the file name. Otherwise you're going to have to copy from the InputStream into a temporary file, so you have a File which your library can use.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!