permaculture playing cards*
The moose likes EJB and other Java EE Technologies and the fly likes File load in bean Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "File load in bean" Watch "File load in bean" New topic
Author

File load in bean

Tmmet Johnson
Ranch Hand

Joined: Nov 03, 2004
Posts: 56
Hi,
I want to read a file in EJB in bean method.
I have the file (error.prop) in the ejbmodule/META-INF folder.

When I right click on the ejb and click "Run on Server" to test it, I get
an Exception.Basically,the call fails in File.exists() method.
Below is the code.Can anyone please tell me the problem in the below code.

I know I can use ResourceBundle to load the file.But,want to know the
problem in the below code.The code looks ok to me.I tried putting the file
directly in ejbModule folder.No luck. I also tried setting strMessageFile
in the below code to "error.properties", "//error.properties" etc., It did
not work.

public class MessageBean implements javax.ejb.SessionBean {

public String getMessage(){
try {
String strMessageFile = "\\error.properties";
java.io.File messageFile = new java.io.File(strMessageFile );
if (messageFile .exists()){
//Get the error message from file
System.out.println("File exists");
return "File Exists";
}
else{
System.err.println( "Message file does not exist" );
return "File Does not Exist";
}
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
Valentin Tanase
Ranch Hand

Joined: Feb 17, 2005
Posts: 704
Hi Tmmet,

First of all you have to understand that performing IO operation within your ejbs is not recommended and ejbs specs strictly forbid it. You might consider using the ResourceBoundle class or the less orthodox approach of calling the ClassLoader.getResourceAsStream() method. On the other hand although is been a while since I looked at the io api, it occurs to me that opening a file which is inside of a jar module (which in turn could be inside of a ear file) is not going to be easily done using io api. I believe you should open the jar file first, un-jar it and finally opening the properties file. Again io is not suitable for this task in my opinion you should better try using another approach.
Regards.


I think, therefore I exist -- Rene Descartes
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: File load in bean
 
Similar Threads
questions about several File methods.
Getting an error while compiling javac code
I need help regarding java.io.File vs sun.awt.shell. Win32ShellFolder2
Configurable Errors - Design Question
how to iterate over files in a directory