How do I Set Environment variable for a multithread program? - java

I would like to run a parallel Java threaded program and take advantage of multiprocessor execution.
However I need to set the environment variable, to enable a multi-threaded environment. I understand that you can set the environment by issuing setenv PARALLEL 4 OR setenv OMP_NUM_THREADS 4 (for an OpenMP program).
This should enable 4 processors to run concurrently if you have 4 processor.
My Question is:
where do you issue the above command (SETENV) and how do you do it?
In java you can call System.getenv("NUM_THREADS") to get the NUM_THREADS. However there is no clear way of setting the environment. I am running AMD-x64 machine: OS: Windows 8, Processor: AMD E-300 APU Dual-Core processor, Ram: 4.00GB, System Type: 64-bit OS.

Below is the link which explains on how to set environment variable in a Windows machine manually:
http://www3.ntu.edu.sg/home/ehchua/programming/howto/Environment_Variables.html
A piece taken from above link (must read the link completely, its very rich in knowledge):
Display Variables and their Values
To list all the variables and their values, start a CMD shell (Click "Start" button ⇒ Run ⇒ Enter "cmd") and issue the command "set". To display a particular variable, use command "set varname". For examples,
// Display all the variables (in NAME=VALUE pairs)
prompt> set
COMPUTERNAME=xxxxxxx
OS=xxxxxxx
PATH=xxxxxxx
.......
// Display a particular variable
prompt> set COMPUTERNAME
COMPUTERNAME=xxxxxx
// OR use echo command with variable enclosed within a pair of '%'s
prompt> echo %COMPUTERNAME%
COMPUTERNAME=xxxxxx
Try issuing a set command on your system, and study the environment variables listed. Pay particular attention to the variable called PATH.
Set/Change/Unset a Variable
To set (or change) a variable, use command "set varname=value". There shall be no spaces before and after the '=' sign. To unset an environment variable, use "set varname=", i.e., set it to an empty string.
prompt> set varname
prompt> set varname=value
prompt> set varname=
prompt> set
Display the value of the variable
Set or change the value of the variable (Note: no space before and after '=')
Delete the variable by setting to empty string (Note: nothing after '=')
Display ALL the environment variables. For examples,
// Set an environment variable
prompt> set MY_VAR=hello
// Display
prompt> set MY_VAR
MY_VAR=hello
// Unset an environment variable
prompt> set MY_VAR=
// Display
prompt> set MY_VAR
Environment variable MY_VAR not defined
A variable set via the "set" command under CMD is a local variable, available to the current CMD session only.
If you want to set the same using Java code, below is one example:
public static void main(String[] args) throws IOException {
ProcessBuilder pb = new ProcessBuilder("CMD", "/C", "SET");
Map<String, String> env = pb.environment();
env.put("MYVAR", "myValue");
Process p = pb.start();
InputStreamReader isr = new InputStreamReader(p.getInputStream());
char[] buf = new char[1024];
while (!isr.ready()) {
;
}
while (isr.read(buf) != -1) {
System.out.println(buf);
}
}
If you want to pass some value to your program, you could also do that in command line:
java -DMyVar=varValue <main program>
This value could be read as:
String myVar= System.getProperty("MyVar");

I believe setenv is a command for linux/unix.
In windows 7, you can use the setx command in command prompt to set a User Environment Variable. e.g:
setx myvariablename myvariablevalue
Or you can do it through the GUI:
Right click My Computer -> Properties -> Advanced -> Environment Variables

Related

How to set environmental variable that can be get by System.getenv("name") in JAVA

I get some environmental variables in this way:
String javaHome=System.getenv("JAVA_HOME");
String androidHome=System.getenv("ANDROID_HOME");
However, it always return null to the androidHome.
I set it in ~/.bash_profile and it can be print in terminal by echo $ANDROID_HOME.
Where should I set this variable?
Try to verify that you really don't get it from the system environment by:
Map env = System.getenv(); and checking all the keys in this map.

Can we read the OS environment variables in Java?

My OS is windows7. I want to read the environment variables in my Java application. I have searched google and many people's answer is to use the method System.getProperty(String name) or System.getenv(String name). But it doesn't seem to work. Through the method, I can read some variable's value that defined in the JVM.
If I set an environment variable named "Config", with value "some config information", how can I get the value in Java?
You should use System.getenv(), for example:
import java.util.Map;
public class EnvMap {
public static void main (String[] args) {
Map<String, String> env = System.getenv();
for (String envName : env.keySet()) {
System.out.format("%s=%s%n",
envName,
env.get(envName));
}
}
}
When running from an IDE you can define additional environment variable which will be passed to your Java application. For example in IntelliJ IDEA you can add environment variables in the "Environment variables" field of the run configuration.
Notice (as mentioned in the comment by #vikingsteve) that the JVM, like any other Windows executable, system-level changes to the environment variables are only propagated to the process when it is restarted.
For more information take a look at the "Environment Variables" section of the Java tutorial.
System.getProperty(String name) is intended for getting Java system properties which are not environment variables.
In case anyone is coming here and wondering how to get a specific environment variable without looping through all of your system variables you can use getenv(String name). It returns "the string value of the variable, or null if the variable is not defined in the system environment".
String myEnv = System.getenv("env_name");

What does -Dauto means

I saw the fallowing command for starting Selenium:
java -Xmx256m -Dauto=1 -jar selenium-server-standalone-2.25.0.jar -log /home/test/selenium.log -trustAllSSLCertificates
I googled to find what -Dauto=1 means but failed.
I'm pretty sure auto is no valid parameter in a current version of Selenium server. It might have been in the past or it was just used by some custom implementation.
java -jar selenium-server-standalone-2.25.0.jar -h
will list you all available parameters for Selenium, which you can set by adding
-D<variable>=<value>
to your start command.
-D is an important switch that allows you to set environment properties.
-Dproperty=value
Set a system property value. If value is a string that contains spaces, you must enclose the string in double quotes:
java -D<propertyName>=<propertyValue>
You can call the following from anywhere in the code to read it.
String value = System.getProperty("propertyName");
or
String value = System.getProperty("propertyName", "defaultValue");

How to set an environment variable in Java using exec? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
How do I set environment variables from Java?
I'm trying to set an environment variable, and read it back to verify it was actually set.
I've got the following :
import java.io.IOException;
public class EnvironmentVariable
{
public static void main(String[] args) throws IOException
{
Runtime.getRuntime().exec("cmd.exe set FOO=false");
String s = System.getenv("FOO");
System.out.println(s);
}
}
However, it appears that FOO is always null, meaning its probably not set correctly.
Do I have the exec command correct? The javadocs state it can take a string argument as the command.
Any ideas?
There are overloaded exec methods in which you can include an array of environment variables. For example exec(String command, String[] envp).
Here's an example (with proof) of setting an env variable in a child process you exec:
public static void main(String[] args) throws IOException {
String[] command = { "cmd", "/C", "echo FOO: %FOO%" };
String[] envp = { "FOO=false" };
Process p = Runtime.getRuntime().exec(command, envp);
BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
String s = reader.readLine();
System.err.println(s);
}
However, that sets the variable in the env of the created process, not of your current (Java) process.
Similarly, if you are creating a process from Ant (as you mention in comments to aix) using the exec task, then you can pass environment variables to the child process using nested env elements, e.g.
<exec executable="whatever">
<env key="FOO" value="false"/>
</exec>
This won't work. When you start a new process, that process receives a copy of the environment. Any changes it then makes to environment variables are made within that copy, and at no point will become visible to the caller.
What are you actually trying to achieve?
By running "cmd.exe", you start a new process, which receives the new environment variable, however the java process does not get that new environment variable set this way.
In Unix/Windows, each process has it's own set of environment variables and inherits the environment variables from it's parent during process creation.
System.getenv() only returns the environment variables that were set when the process was started, as far as I see there is no way to change the environment variables of the java process itself.
The only way you can check if the set works is by starting a small batch script where you set and do the check in one process.
It's null because you launch another cmd.exe: it's a different environment from the one of your Java application (cf aix answer).
I don't think the Java runtime can change an environment variable: it can read them, but can't change them.
If you want to change a system property available in your executing JVM, use System.setProperty(String key, String value).

How to enumerate all environment variable in Java

System.getenv(name) needs the name of environment variable.
I am trying to call Runtime.exec(String[], String[], File), the secondary parameter need an array of environment variable, I am not sure whether subprocess will inherit environment variables from current process if I specified this parameter.
For example, if I pass new String[]{"NEWDIR=/home"} as secondary parameter and current java process has environment OLDDIR=/var, what is the return value of System.getenv("OLDDIR") in the subprocess?
updated:
Sorry, I have to use Java 1.4 and it seems that System.getenv() was introduced in 1.5?
Map<String, String> env = System.getenv();
for (String envName : env.keySet()) {
System.out.format("%s=%s%n", envName, env.get(envName));
}
System.getenv() will return a Map<String,String> with all of the environment variables.
But you could just as easily switch to ProcessBuilder which is a more convenient API to start new processes.
With ProcessBuilder you can simply call environment() and get a Map that contains existing environment variables and which you can manipulate how you want: i.e., if you add something to it, then that will be added to the new processes environment variables. If you remove something from it, it will not be present in the new process.
If you run an external shell, you can use it to set environment variables. e.g.
bash -c ENV1=hi ENV2=bye echo $ENV1 $ENV2
This only works if you have a UNIX shell (or cygwin)
You should migrate away from Java 1.4 and Java 5.0. Even Java 6 you might consider upgrading to Java 7.

Categories

Resources