Not able to get WebSphere environment variable in Java - java

I've set the environment variable under Application servers > server1 > Process definition > Environment Entries
My Property name is region and value in dev.
I'm reading this property value like below:
String environment= System.getProperty("region");
But I'm getting null, what I'm doing wrong here? I tried to set the variable under this path as well: This also returning null - how can I read the environment variable in Java?
Environment > WebSphere Variables
I'm using WebSphere version 8.0.0.3

Environment entries on the server's process definition are set as OS-level environment variables, not Java system properties. It's the equivalent of calling "set region=dev" on the command line before starting your JVM. Two potential approaches:
1) Use System.getenv() instead of System.getProperty() - getenv() is the method used to retrieve environment variables.
2) Instead of defining the property as an environment variable on the process, define it as a system property in the Process Definition -> Java Virtual Machine panel. You can either create a new System Property or add it to your generic JVM arguments as a -D option ("-Dregion=dev").

Related

what and where is "jdbc.drivers" system property? [duplicate]

What's the difference between system properties System.getProperties() and environment variables System.getenv() in a JVM?
System properties are set on the Java command line using the -Dpropertyname=value syntax. They can also be added at runtime
using System.setProperty(String key, String value) or via the various
System.getProperties().load() methods.
To get a specific system property you can use System.getProperty(String key) or System.getProperty(String key, String def).
Environment variables are set in the OS, e.g. in Linux export HOME=/Users/myusername or on Windows SET WINDIR=C:\Windows etc,
and, unlike properties, may not be set at runtime.
To get a specific environment variable you can use System.getenv(String name).
I think the difference between the two boils down to access. Environment variables are accessible by any process and Java system properties are only accessible by the process they are added to.
Also as Bohemian stated, env variables are set in the OS (however they 'can' be set through Java) and system properties are passed as command line options or set via setProperty().

Mac environment variables not available to java runtime class

Within java, I am trying to access two environment variables that I previously set on my Mac computer (running Yosemite). I have set values for two variables: JAVA_HOME and R_HOME. When setting R_HOME, I did it by editing: ~/Library/launchagents/environment.plist.
Using Terminal on the mac, I can use the 'printenv' command and get values for both variables.
When I run my java application within Eclipse, I can get the JAVA_HOME value by using:
System.out.println("javahome: " + System.getProperty("java.home") );
I have used the following code to list environment variables available to my java application (the list of available values that this code outputs includes neither JAVA_HOME nor R_HOME):
Map<String, String> env = System.getenv();
for (String envName : env.keySet()) {
System.out.format("%s=%s%n", envName, env.get(envName));
}
My application will need to be standalone, so setting environment variables within the Eclipse Run configuration won't be helpful. Furthermore, my application won't know the value of R_HOME when it runs; the value must already be set so the application can access it at runtime and use the value to start an executable file, as in:
ProcessBuilder pb = new ProcessBuilder("$R_HOME/library/Rserve/libs/Rserve");
Process p = pb.start();
So, my question is: how can my java application access the value of the R_HOME environment variable. Either I need a new way to setting the variable so that it is available to the JVM or I need a new java method to call to access the value.
See Java Options -Dproperty=value for setting a system property value.
java -DR_HOME=<the-location> -jar application.jar
Also note that with
System.getProperty("java.home")
you're not reading $JAVA_HOME, it just prints the Installation directory for Java Runtime Environment (JRE), see
https://docs.oracle.com/javase/tutorial/essential/environment/sysprop.html
My guess is be that the env-Variables are not correctly set to be accessible in your class

Java environment variables under Windows 2012R2

We are trying to run Apache-Tomcat which would start / stop as a service. However, when trying to set it up, we get error messages as for unset variables, making it impossible to set it as a service. Looking at the file, and given that this is a .BAT file, as provided by the distribution, is there an easy way to set the JAVA-HOME and other required variables for Java to be set globally for the box ? Our main challenge is to get Apache-Tomcat copied / setup through a script, and the environment variables globally (and permanently) set so that when the server is rebooted, or the service started/stopped/restarted.
The JAVA_HOME environment variable is only used when running the service.bat file. Once the service has been created, it has embedded the value, so changing a global JAVA_HOME environment variable will make no difference.
When you want to use a different JAVA_HOME value, you have to deregister the service and reregister with the new value.
This is regardless of Windows version. Also remember to run service.bat "as admin".
There is a good reason why a registered Tomcat instance is not using global environment values. As an example, we're running 6 different Tomcat instances on our UAT server, each with a different combination of Tomcat version and Java version. We couldn't do that if all the instances were using a single global JAVA_HOME or CATALINA_HOME value.

Is it possible to "fool" java that an environment variable is set from the command line

I'm running in an environment where I can pass parameters but not set environment variables using a normal commandline.
I would like to set environment variables nevertheless. Is there an alternative way to "fool" java that an environment variable is set, e.g. using -D parameters?
(I'm running spark in oozie through hue; all in the Cloudera stack).
The java command itself doesn't seem to allow that. It has a -D parameter, but that sets Java 'system properties':
$ java -help 2>&1 | grep -A1 '\-D'
-D<name>=<value>
set a system property
Java system properties are a sort of Java properties. Like environment variables, java properties are key-value pairs, but aren't the same thing as environment variables: If your Java application reacts to a specific environment variable, setting a system property of the same name won't have any effect unless the application explicitly reacts to that property, too.
If your environment allows you to run arbitrary Java applications and if it allows your Java applications to execute other processes, you can write a little wrapper that sets the environment variables on a ProcessBuilder (see the question Arnon linked in his comment: How do I set environment variables from Java?) to then invoke java with your actual JAR from it. You could either hard-code the environment variables to set, or set them according to system properties the wrapper receives. (Or you could even implement your own shell in Java and pass a script to it.)
Though, if you can modify the source of that actual JAR, a much more idiomatic solution would be to make it itself react to properties instead of (or additionally to) environment variables. (Unless you have to control environment variables that the java command / the JVM reacts to rather than the JAR you'd like to run. Then this approach would not be applicable, of course.)
If you can run arbitrary Java code, you can create and run a Process using ProcessBuilder, including an environment of your choice.
Hence, write a java program that parses a command-line like
java -cp .... your.Prog FOO=BAR BAZ=BOOM command arguments ...
and starts command with the environment extended by FOO and BAZ
(Note that command could be java ...)

is there any command option in java to set environment variable?

I'm working on Java.
is there any option in the "java" to set the user defined environmental variable to the executable that we will execute using "java" command.
e.g. we have one option -XX:+AllowUserSignalHandlers.
You can set tuning parameter to JVM at the start of the program itself.
ex -
java -XMS50m -XX:+AllowUserSignalHandlers Test.class
Once jvm is started you can not set environment variables but you can view them System.genEnv it retuns an unmodifiable string map view of the current system environment.
If you are creating a new process then you can set environment variables using ProcessBuilder.environment.
There is no environment variable that java.exe parses and used to set command line arguments.
You have to write a program/script that would process your custom environment variable and then pass to actual java program.

Categories