I don't mess with WebSphere often enough to know if this will work. I would like my J2EE or plain Java apps to know on which box it is running, i.e PRODUCTION, DEVELOPMENT or TEST, and use PROD, DEVL or TEST resources accordingly. Since this info is common to all apps, I don't want to store it within any particular app. Is it possible to store an (environment?) variable on the Linux box or within WebSphere (outside of all apps) that is accessible to all J2EE (Struts2) apps and plain Java apps? I would appreciate any pointers in this regard. Thanks.
1 Answers
Yes. I'm not a WebSphere user, but a quick search provides this: Setting environment entries in WebSphere Application Server.
WebSphere Application Server 7.0, 8.0, 8.5 and 8.5.5
Open the administrative console.
Select Servers > (Expand Server Types) > WebSphere application servers > server_name > (Expand Java™ and Process Management) > Process Definition > Environment Entries > New.
Add entries to the Name/Value pairs.
Click OK.
Save changes to the master configuration and restart the Application Server.
WebSphere Application Server 6.1
Open the administrative console.
Select Servers > Application Servers > server_name > (Expand Java™ and Process Management) > Process Definition > Environment Entries > New.
Add entries to the Name/Value pairs.
Click OK.
Save changes to the master configuration and restart the Application Server.
I do nearly the same thing in JBoss/Wildfly. You are simply setting up an entry the JVM's Environment.
Edit in response to 1st comment:
I think it's a safe guess that your Java app doesn't run in the same JVM as your J2EE app on WebSphere. Separate JVMs = separate environments. We use Java Web Start to distribute and kick off local Java apps from different environments - each server environment writes a JNLP containing the appropriate app setup. If you just need apps on the same machine to know where they are you can set up an shell variable, as you said, that could be read by both WebSphere and Java apps.
Edit #2, regarding WebSphere - after further investigation:
I also found this, which is a partial duplicate of this question: Place for setting environment variables for WebSphere JVM process. The answer there notes that using the WebSphere GUI to enter the env variables is also preferable for clustering.
You might want to use use the WebSphere specific method in combination with the shell variables.

- 1
- 1

- 1,473
- 1
- 21
- 31
-
thanks. I tried this and the suggestion at http://stackoverflow.com/questions/6979958/read-a-environment-variable-in-java-with-websphere, but when I do a System.getenv("VAR"), it works from my J2EE app, but not from my Java app. System.getProperty doesn't work in either app – user1005585 Mar 08 '17 at 15:24
-
Also, System.getProperty isn't appropriate here, as you are not passing in information via switches to the java command, for example `# Set a system property. -Dcom.ibm.example.system.property=ExampleValue` – Jon Sampson Mar 08 '17 at 16:10
-
OK Thank you. I'll keep trying. I have always used a shell variable, but on a machine restart, it seems like WAS initiates before the shell variables are, and therefore I get a NULL for the shell variable via WAS. If I restart the WAS server, then it picks up the shell variable correctly. – user1005585 Mar 08 '17 at 16:43