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
Related
I am writing an application in Java and I need to use environmental variables in AWS EC2 machine (Linux). I am using System.getenv("myvariable") in the application to get the particular environment variable. I need to set the permanent environmental variables and currently I have set the variable in ~/.bashrc , ~/.profile and also ~/.bash_profile.
I am giving export myvariable=xyz in al the 3 files and then I ran source ~/.bashrc , source ~/.profile and source ~/.bash_profile. However I am getting null in the application.
I still don't understand why I am not able to get any environmental variables even though I am exporting the variable in ~/.bash_profile.I have even checked by running echo $myvariable and I can see xyz.
If I set the same environment variable in my local MAC machine in the same way I set above and use the same shell to run the same java code, I can see the variable with the value.
So basically I am getting null every time in my AWS EC2 linux machine.
Is there any other place I need to set the variable? I have even restarted the machine but it didn't help.
It sounds like the problem is how you are setting the env variables. Sudo does not preserve them by default, so you need to use the sudo -E option.
This is explained in more detail in: How to keep Environment Variables when Using SUDO
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.
Everywhere I search, it says you can get an environment variable by using System.getenv(str).
It's not working for me. Here's what I am doing:
OS : Mac OS x 10.7
Java 1.6.x
If I do export abc=/hello/ in my terminal and then echo $abc, it gives me the variable. If I close the terminal, reopen it again and do echo $abc, it's gone. To overcome this, I edited my .bash_profile file and inserted export abc=/hello/. Close the terminal, do echo $abc and it works. So I understood that the env variable is permanent now.
Now if in my java console app, I print System.getenv("abc"), it returns null. What am I missing?
The reason that you needed to put the export in your .bash_profile is that setting environment variables in a shell only persist the variables in that shell, and - since you used export - to children of that shell, or in other words, other programs launched by that shell.
If you're running your java code from Eclipse, and you launch Eclipse from a shell with your environment variables set, then your program should see the added environment variables. To launch Eclipse from the shell, you'll need to use the OS X open command:
$ open /Applications/eclipse/Eclipse.app
Alternately, you can set the environment variables within your Eclipse project, and you'll need to do this if you're not launching Eclipse from a shell with the proper environment. In the Run Configurations dialog, look for a tab named Environment. Here you'll find a table for adding environment variables that will be passed to your program.
It's better to add the environment variables to the Run Configuration since that way they'll always be available to your project. Your code doesn't actually care where the environment variables are coming from, and adding them to the project is simpler, and will work the same way on different platforms.
Of course, when you run your program outside Eclipse, you'll need to make sure that the same environment variables exist in the shell where you e.g. run java.
Eclipse does not use the system's env variables unless launched directly from the shell (which is how it is generally launched, by clicking its icon). In that case you will have to explicitly set the required env variables in the environment tab of the run configuration of the program.
I too faced The same issue , I resolved it this way.
Open Terminal
cd to the folder where eclipse.app is located E.g cd /Users/Shared/eclipse/jee-2020-09
Type open Eclipse.app/
Eclipse will now open and will be able to access the system environment variables as well.
Check it using the code:
System.getenv().forEach((k, v) -> {
System.out.println("ENV : " + k + ":" + v);
});
I want to write java program which adds environment variables and when I open environment tab from MyComputer,then I should be able to see that...
This is what I have tried,but it gives java.lang.unsupportedexception
Map env = System.getenv();
env.put("abc", "pqr");
And One more try is below,it is not giving any error...But I can not see any value added when I open environment variables tablenter code here from My Computer.But When i sysout "env" variable it gives me all paths including myone also...But I need same thing to be shwon in environment variables tab...
ProcessBuilder processBuilder = new ProcessBuilder("cmd.exe","/c","set");
Map env = processBuilder.environment();
env.put("abc", "pqr");
Please help me guys...Thanks in Advance...
There are two ways. The first one is to call the Windows API to change/set the environment variable. You must look for the right Windows API function and call it from Java. However calling any Windows API from Java is a topic on its own.
The second way is to call the setx.exe program with the correct arguments to set environment variables. Check the manual of the setx.exe program how to use it (and when it is installed) to set your environment variables.
In both ways you obviously restrict your Java program to run on Windows systems only.
Does anyone know how to access the environment variables of the standard shell environment in Java? I am using the ProcessBuilder class and I have to specify specific environment variables used in a shell script I am running, these variables exist in the standard shell environment.
Accessing ProcessBuilder environment does not work.
You can get at the environment variables that existed when your program was created through System.getenv():
http://download.oracle.com/javase/tutorial/essential/environment/env.html
When a Java application uses a ProcessBuilder object to create a new process, the default set of environment variables passed to the new process is the same set provided to the application's virtual machine process. The application can change this set using ProcessBuilder.environment.
It looks like your child process should get your environment automatically.
processBuilder.environment().get("variablename");