Read a Environment Variable in Java with Websphere

On WebSphere follow this settings

On WAS follow the above setting where name is your key and value is your property value. in my example i used Name : Test Value : This is the test value. After setting this values restart your application server. on your Java code call System.getProperty("TEST") where test is the name for your property and the value will show


You can put something like the following in your web.xml file, which should be in your application's WEB-INF directory:

<env-entry>
    <env-entry-name>myVar</env-entry-name>
    <env-entry-type>java.lang.String</env-entry-type>
    <env-entry-value>myVarOnServeur</env-entry-value>
</env-entry>

By the way this is a standard syntax and should work across all the application servers. I'm using it with WebSphere, JBoss and WebLogic. It can be queried exactly as you do in your example.


You are looking at the wrong place.

You should add the variable in Environment->Naming->Name space bindings->New.

If you choose Binding type String, "Binding identifier" and "Name in namespace..." myVar, you can get variable's value with:

Context ctx = new InitialContext();
String myVar = (String) ctx.lookup( "cell/persistent/myVar" );

to define inside web.xml

<env-entry>
   <env-entry-name>varName</env-entry-name>
   <env-entry-value>56</env-entry-value>
   <env-entry-type>java.lang.String</env-entry-type>
</env-entry>

to see with java

Context envEntryContext = (Context) new InitialContext().lookup("java:comp/env");
String mydata = (String)envEntryContext.lookup("varName");

Tags:

Java

Websphere