Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read a Environment Variable in Java with Websphere

Tags:

java

websphere

I've a little problem with Websphere application server 7.0 (WAS7) and the reading of Environment Varaibles.

With TomCat, I've defined a variable as

<Environment name="myVar" type="java.lang.String" value="myVarOnServeur"

and I read it with a lookup on the initialContext :

Context ctx = new InitialContext();
String myVar = (String) ctx.lookup( "java:comp/env/myVar" );

and it works!

But with Websphere, I define a environment variable on the GUI but I can't read it in my java code. I've a NamingException.

enter image description here
(source: fullahead.org)

How can I do to fix my problem?

like image 856
lookfire Avatar asked Aug 08 '11 09:08

lookfire


People also ask

How do you read an environment variable in Java?

How to get the value of Environment variables? The System class in Java provides a method named System. getenv() which can be used to get the value of an environment variable set in the current system.

What is environment variable in WebSphere application server?

Environment variables, also called native environment variables , are not specific to WebSphere Application Server and are defined by other elements, such as UNIX, Language Environment® (LE), or third-party vendors, among others. Some of the UNIX-specific native variables are LIBPATH and STEPLIB.

What is environmental variable in Java?

Like properties in the Java platform, environment variables are key/value pairs, where both the key and the value are strings. The conventions for setting and using environment variables vary between operating systems, and also between command line interpreters.

What is liberty Java?

IBM WebSphere® Liberty is a Java™ EE application server with a low-overhead Java runtime environment designed for cloud-native applications and microservices. WebSphere Liberty was created to be highly composable, start fast, use less memory, and scale easily.


2 Answers

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

like image 92
Thakhani Tharage Avatar answered Sep 28 '22 11:09

Thakhani Tharage


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" );
like image 25
dblazeka Avatar answered Sep 28 '22 11:09

dblazeka