lookfire
lookfire

Reputation: 338

Read a Environment Variable in Java with 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?

Upvotes: 14

Views: 48580

Answers (11)

gic186
gic186

Reputation: 836

As reported by the official IBM documentation, an environment variable in WebSphere should be set this way:

Select Servers > (Expand Server Types) > WebSphere application servers > server_name > (Expand Java™ and Process Management) > Process Definition > Environment Entries > New

Then you can access the variable like a regular application argument.

Spring users can access the variable with the @Value annotation:

@Value("${YOUR_ENVIRONMENT_VARIABLE_NAME}") 
private String environmentVariable

Upvotes: 0

user2125853
user2125853

Reputation: 1315

The thread is kind of old but just wanted to provide some info. This is with WebSphere 8.5.5

I tried getting WebSphere variables defined in the console via [Environment > WebSphere variables] using

System.getProperty("Variable");

It did not give the variable to me. I looked around a bit on the web and came across the following: https://www.setgetweb.com/p/WAS855/ae/was2873.html

The following function listed there returns the variables

private static String expandVariable(String s) throws
javax.management.JMException 
{   
    com.ibm.websphere.management.AdminService as = com.ibm.websphere.management.AdminServiceFactory.getAdminService();
    String server = as.getProcessName();
    java.util.Set result = as.queryNames(new javax.management.ObjectName("*:*,"
                     + "type=AdminOperations,process=" + server), null);
    return (String)as.invoke((javax.management.ObjectName) result.iterator().next(),"expandVariable",
            new Object[] {"${"+s+"}"}, new String[] {"java.lang.String"});
}

Then call

expandVariable("Variable");

Upvotes: 1

JeremyCanfield
JeremyCanfield

Reputation: 673

I would just like to elaborate on creating a variable in WebSphere that can be used by a Java app, to hopefully help others, as I had to do a bit of additional research to figure this out.

Let's say you want to create a variable in WebSphere named ENV which contains a value of dev (or int, or prod, or any other value).

  1. In the left panel of the WebSphere admin console, select Servers > Server Types > WebSphere application servers.
  2. Select the application server that contains the app.
  3. Expand Java and Process Management and select process definition.
  4. Select Java Virtual Machines.
  5. Select Custom properties.
  6. Select New.
  7. Create the name and value of the variable and select OK.
  8. Select Save.
  9. Restart the application server for this change to take effect.

In this example, a variable named ENV with a vaule of "dev" was created.

enter image description here

Next, the Java app will need to be configured to use the ENV variable in WebSphere. In the below markup, the Java app has a class named "Environment". This class creates a variable named env. System.getProperty("ENV") is the magic that gets the variable from WebSphere. It is noteworthy that this Java code should also work with other application servers, such as JBoss or Tomcat, so you don't need to customize the Java code to a particular platform.

While definitely not required, I also am returning env. I am just doing this for demonstration, so that we can get the variable in a JSP page, so that we can see the variables with our own eyes in a JSP page, for validation that this works as expected.

package com.example.main;

public class Environment {  
    public String env;

    public Environment() {
        env = System.getProperty("ENV");
    } 

    public String getEnvironment(){
        return env;
    }
}

Inside of the tags of a JSP page, I add the following markup to get the env variable from the Environment class, which in turn gets the ENV variable from WebSphere.

<%@page import="com.sample.main.Environment"%>

<%
  Environment foo = new Environment();
  String env = foo.getEnvironment();
  out.print("Environment : " + env;
%>

Now, once the app has been deployed to WebSphere, the environment should be displayed, which is how I know that I was able to successfully get the variable from the application server.

enter image description here

Upvotes: 1

Krishnam
Krishnam

Reputation: 849

Websphere 7.0 - 8.5

Set Variable Admin Console ---> Websphere Application servers -----> Your_sever_name ---> Java and process management ---> Process definition -->Java Virtual Machine --> Custom properties

Get Value in Java System.getProperty("Your_Variable")

Upvotes: 4

Kaltresian
Kaltresian

Reputation: 971

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");

Upvotes: 9

Thakhani Tharage
Thakhani Tharage

Reputation: 1298

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

Upvotes: 5

dblazeka
dblazeka

Reputation: 71

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" );

Upvotes: 7

svachon
svachon

Reputation: 7716

If what you want is to define and manage your own variables, have a look at Environment->Naming->Name space bindings. You can bind jndi names to String constants there. see String binding settings

Upvotes: 4

Nicola Musatti
Nicola Musatti

Reputation: 18218

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.

Upvotes: 4

McDowell
McDowell

Reputation: 108859

You should be able to resolve these via WebSphere's AdminOperations MBean:

//sample code from WAS 7 Infocenter
private String expandVariable(String s) throws
                         javax.management.JMException {  
  com.ibm.websphere.management.AdminService as = 
     com.ibm.websphere.management.AdminServiceFactory.getAdminService();  

  String server = as.getProcessName();
  String mBeanName = "*:*,type=AdminOperations,process=" + server;

  java.util.Set result = as.queryNames(
     new javax.management.ObjectName(mBeanName) , null);  

   return (String) as.invoke((javax.management.ObjectName)
                             result.iterator().next(),
                             "expandVariable",
                             new Object[]{"${"+s+"}"},
                             new String[]{"java.lang.String"});
}

See Creating, editing and deleting WebSphere variables.

Upvotes: 3

user207421
user207421

Reputation: 310840

I don't see anything there that says that those entries can be read via ctx.lookup( "java:comp/env/..." );

Upvotes: -3

Related Questions