Problem getting properties in EAR file

loading a properties file...

Page 1 of 1

5 Replies - 12887 Views - Last Post: 03 October 2007 - 11:22 AM Rate Topic: -----

#1 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Problem getting properties in EAR file

Posted 01 October 2007 - 01:48 PM

I had a project the worked wonderfully in weblogic, but everything fell apart when I tried to deploy it on websphere.

One thing that seem to be happening is that it will not find any of the properties files.

the files are located in:

EARBASE\WEB-INF\classes

and the program is located in "EARBASE\EJB.jar"

here is a sample of the code:

	public void onMessage(Message msg) {
		Properties p = new Properties();
		InputStream  in = this.getClass().getClassLoader().getSystemResourceAsStream("log4j.properties");
		try {
			if (in != null) {
				p.load(in);
			} else {
				System.out.println("Error Loading Properties file!");
			}
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		Enumeration en = p.propertyNames();
		while (en.hasMoreElements())
		{
			System.out.println(p.getProperty((String)en.nextElement()));
		}
}



as you can guess, in==null. I have tried all sorts of locations for the properties file and none of them seem to work. This is just the tip of the iceburg of my problems... basically none of the jars used by the EJB seem to be loading.

Originally I was using code that looked like:
	public PropertyLoader()
	{
 		ResourceBundle resBundle = ResourceBundle.getBundle("log4j");
		props = new Properties();
		String key;
		String value;
		for (Enumeration keys = resBundle.getKeys(); keys.hasMoreElements();) {
			key = (String) keys.nextElement();
			value = resBundle.getString(key);
			props.put(key.toLowerCase(), value);
//			log2File.info("Propertie: " + key.toLowerCase() + " = " + value);
			//System.out.print("Propertie: " + key.toLowerCase() + " = " + value+ '\n');
		}
	}


This worked well in WebLogic. I could deploy the properties file inside the EAR, or in the class path for the app. Worked well under weblogic, but does not work under webshere. Throws an error about how it cannot find resource "log4j" (actually that is not the properties file that is really loaded, it just an example, please don't get caught up on the log4j thing).

Is This A Good Question/Topic? 0
  • +

Replies To: Problem getting properties in EAR file

#2 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: Problem getting properties in EAR file

Posted 01 October 2007 - 03:36 PM

To answer (partially) my own question:

There are two problems with the above:

#1 I should be using .getResouceAsStream() rather than the system resource which would be used statically just using the ClassLoader class.

#3 It seems that the properties file needs to be INSIDE the jar file (in this case the ejb.jar file) and its path must be expressed.

So:
InputStream  in = this.getClass().getClassLoader().getResourceAsStream("/the/package/name/log4j.properties");


So I can place the properties file in the base of the jar file simply by using the correct path:
InputStream  in = this.getClass().getClassLoader().getResourceAsStream("/log4j.properties");


This is NOT what I want. Ideally the properties file would not be deployed in the application itself (allowing for quick changes without having to edit and redeploy the project).

But I can't seem to get the damned thing any other way! This is not how it is supposed to work is it?
Was This Post Helpful? 0
  • +
  • -

#3 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: Problem getting properties in EAR file

Posted 01 October 2007 - 04:12 PM

What is really frustrating is that I don't seem to understand how things are supposed to laid out in the EAR... I *thought* I did, but everything is wrong when I go to use things.

For instance. I use log4j in my application. So I places the log4j.jar in

EAR\WEB-INF\lib

and that has always worked for me. But suddenly I am getting:

java.lang.NoClassDefFoundError: org/apache/log4j/Logger

Obviously either WebSphere app server is not truly J2EE complaint, or I have been relying upon a bug in WebLogic! Why does the jar file need to be in the base directory of the ear... and WHY do we have the lib directory if it isn't used to store libraries that are used in the application?

Is it because the libs are used in a ejb jar file, and so should actually be deployed with the ejb and not the ear... well that is irritating as I would have to deploy the same libraries 5-6 times for 1 application. I just don't get it. Things are not working the way I understand them to.

How could I place the libraries in the MANIFEST.MF so that they were found in the WEB-INF\lib directory of the EAR?
Was This Post Helpful? 0
  • +
  • -

#4 1lacca  Icon User is offline

  • code.rascal
  • member icon

Reputation: 44
  • View blog
  • Posts: 3,822
  • Joined: 11-August 05

Re: Problem getting properties in EAR file

Posted 02 October 2007 - 01:46 AM

Check the J2EE version of the application servers, the ear format went through some changes during the time (also, some appservers are more forgiving, some are not, and all of them have their bugs).
Libraries can be installed in the container, and in the application. Both of them have their respective advantages and disadvantages. Check the bootstrap order, it can shed some light on many things (especially with conflicting libraries).
I know this might not be of many use, but unfortunately I only dealt with Bea and Ora so far, and no Websphere.
Was This Post Helpful? 0
  • +
  • -

#5 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: Problem getting properties in EAR file

Posted 02 October 2007 - 10:14 AM

I did end up with the libraries in the ear/WEB-INF/lib directory, but I had to add a class-path entry for each in the ejb.jar's Manifest file.

I still have not found a way to properly load a properties file from outside of the ear file (or jar file for that matter).

I have been looking at examples and it seems as though I am doing it right, yet it still does not work. I really hate WebSphere app server. WebLogic was SO nice to me. Everything worked the way I thought it should. WebSphere is just NOT that way.
Was This Post Helpful? 0
  • +
  • -

#6 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: Problem getting properties in EAR file

Posted 03 October 2007 - 11:22 AM

Well... problem NOT solved.

adding the the class-path entries to the ejb-jar file's manifest seemed to work just fine for me. BUT, when the customer installed the application on their Websphere application server, they got the same ClassNotFound exceptions that I had.

I don't really know how to troubleshoot this since everything works on the development environment. All I can say is that I there must be something I don't understand about how the ClassLoader on websphere looks for libraries.

Why would it not check the ear\WEB-INF\lib directory by its self? Why would it use the ejb-jar's manifest class-path entries on windows, but not on AIX?

Where should those libraries be deployed so that the class loader will find them, without interfering with other applications?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1