Access WebSphere Variables in J2EE Applications

Accessing WebSphere Variables from inside your J2EE application isn’t as easy as it looks. They’re not environment variables or system properties, but a portion of the WebSphere configuration that is used by the WebSphere Application Server Runtime.

You can access these variables by invoking an operation on “AdminOperations” MBean as follows:


    	try
    	{
    		AdminService adminService = AdminServiceFactory.getAdminService();
    		ObjectName queryName = new ObjectName( "WebSphere:*,type=AdminOperations" );
    		Set objs = adminService.queryNames( queryName, null );
    		if ( !objs.isEmpty() )
    		{
    			ObjectName thisObj = (ObjectName)objs.iterator().next();
    			String opName = "expandVariable";
    			String signature[] = { "java.lang.String" };
    			String params[] = { "${VARIABLE_NAME}" } ;
    			Object retVal = adminService.invoke( thisObj, opName, params, signature );
    			System.out.println( retVal );
    		}
    	} catch (MalformedObjectNameException e) {
		e.printStackTrace();
	} catch (InstanceNotFoundException e) {
		e.printStackTrace();
	} catch (MBeanException e) {
		e.printStackTrace();
	} catch (ReflectionException e) {
		e.printStackTrace();
	}

This code will only work from an application running inside the server.

4 comments ↓

#1 Shrek on 04.02.07 at 10:01 pm

You know I was just thinking the same thing the other day…

#2 Michael Ransley on 03.10.08 at 11:44 pm

An easier method is that you can bind them to a JVM variable through the administration console (server -> process definition -> jvm -> properties). Then on startup they will be automatically populated and then you can access them as jvm variables.

This can a be pretty handy feature.

#3 dev on 03.24.08 at 4:04 pm

Will there be any change in accessing env var when application is deployed in cluster environment?
Becoz i tried accessing this way but its returning the variable name itself not its value!

#4 Rajesh on 09.12.08 at 3:01 am

Cool! this works. The example in IBM website doesnt. Awesome!!

Leave a Comment