I've tried to make a python script to switch between 32bit and 64 bit java but for some reason
os.system('export JAVA_HOME=/usr/java/path')
os.system('export PATH=$JAVA_HOME/bin:$PATH')
does nothing, but manually it works. How can I fix this? (BTW, this is running on a Linux system.)
The export line will set an environment variable for the shell in which it's executed and all its sub-shells. But what's happening here is that Python creates a new shell, executes the line to set the environment variable, and then the shell terminates. That means the environment variable is no longer in force. In fact, the JAVA_HOME environment variable you set in the first line isn't even in force for the second line when that gets executed, because that's in its own shell that also terminates immediately!
The way round it is to run a whole shell script that sets the environment variable and then launches Java:
#!/bin/bash
JAVA_HOME=/usr/java/path
PATH=$JAVA_HOME/bin:$PATH
java ...
Environment variables are local to each process. If you want to make a permanent change then you can follow the official java PATH instructions. They recommend adding the export variable command to your .bashrc file.
In ~/.bashrc:
export JAVA_HOME=/usr/java/path
export PATH=$JAVA_HOME/bin:$PATH
You can chain the commands together - jdk set + script execution like this:
setJdk4Gradle = 'export JAVA_HOME=/home/jdkPath && export PATH=$JAVA_HOME/bin:$PATH'
os.system(setJdk4Gradle + ' && executeSomething')
Related
BACKROUND:
I am using a program that uses an older jkd, jdk-9.0.4, to be specific. It needs the JAVA_HOME value to be set to jdk-9.0.4. I don't want to use jdk-9.0.4 otherwise, so I wrote a batch file to switch to jdk-9.0.4, run the program, and switch back to jdk-14 at the end.
CODE EXCERPT:
SETX /m JAVA_HOME "C:\Program Files\Java\jdk-9.0.4"
GRADLEW runClient
SETX /m JAVA_HOME "C:\Program Files\Java\jdk-14"
PROBLEM:
I believe cmd only reads environment variables at the launch of the file, so even though I update the JAVA_HOME value with SETX it does not acknowledge it. I am looking for some sort of command that could refresh cmd's environment variable values. I am looking for a solution that is not running two separate batch files (One to update JAVA_HOME to jdk-9.0.4 and one to run the program and reset JAVA_HOME).
RESEARCH:
When I look this up online all I can find us the whole cmd only reads environment variables at the launch of the file bit and the only solution it offers is to restart cmd to get the updated values. Ideally though I do not want to do that.
Scenario
While executing a series of commands using the ProcessBuilder I noticed I am currently not able to set an environment variable such that it remains "known" after a set of commands has been executed.
Question
How do I recreate the effects* of the export TASKDDATA=/var/taskd command in a .jar file?
Attempt 0
ProcessBuilder environment variable in java Provides a way to set the environment variable for each specific command, but when I execute the .jar of that solution and check if the environment variable $u is still set after execution, I find it is not. Whereas $TASKDDATA does remain set after execution. To illustrate:
a#DESKTOP-desktopName:/mnt/e$ echo $TASKDDATA
a#DESKTOP-desktopName:/mnt/e$ TASKDDATA=/var/taskd
a#DESKTOP-desktopName:/mnt/e$ echo $TASKDDATA
/var/taskd
a#DESKTOP-desktopName:/mnt/e$ sudo java -jar autoInstallTaskwarrior.jar
[sudo] password for a:
Process ended with rc=0
Standard Output:
util/
Standard Error:
a#DESKTOP-desktopName:/mnt/e$ echo $TASKDDATA
/var/taskd
a#DESKTOP-desktopName:/mnt/e$ echo $u
Attempt 1
For a single command the environment variable can be using the solution I wrote in: Java ProcessBuilder how to get binary output from command. However that does not conserve the taskvariable for a second command in which it needs to be set again. However, when the export command is used, the environment variable is not required to be set again. Specifically this difference comes to light when the java code is finished and the user wants to enter any additional commands that require the environment variable. In that case, the user needs to first type the export command again.
Attempt 2
An additional difference occurs when a new shell is opened to acquire root priviliges with sudo -s. Not only does setting the environment within the .jar file require setting it again for every separate command but additionally the environment variable is not passsed along to the new shell with root priviliges. For example executing the following commands:
commandLines[53] = new String[4];
commandLines[53][0] = "sudo";
commandLines[53][1] = "-s";
commandLines[53][2] = "taskdctl";
commandLines[53][3] = "start";
commands[53].setCommandLines(commandLines[53]);
commands[53].setEnvVarContent("/var/taskd");
commands[53].setEnvVarName("TASKDDATA");
commands[53].setWorkingPath("/usr/share/taskd/pki");
commandLines[54] = new String[5];
commandLines[54][0] = "sudo";
commandLines[54][1] = "-s";
commandLines[54][2] = "task";
commandLines[54][3] = "sync";
commandLines[54][4] = "init";
commands[54].setCommandLines(commandLines[54]);
commands[54].setEnvVarContent("/var/taskd");
commands[54].setEnvVarName("TASKDDATA");
commands[54].setWorkingPath("/usr/share/taskd/pki");
returns:
53RUNNINGCOMMAND=sudo -s taskdctl start
The TASKDDATA variable must be set.
54RUNNINGCOMMAND=sudo -s task sync
Could not connect to 0.0.0.0 53589
Sync failed. Could not connect to the Taskserver.
Syncing with 0.0.0.0:53589
Note 1
Setting the environment variable $TASKDDATA=/var/taskd before executing the .jar with: TASKDDATA=/var/taskd sudo java -jar autoInstallTaskwarrior.jar does not ensure the environment variable $TASKDDATA remains available after the execution of the .jar file. Furthermore it is beyond the scope of the question as it is not set within the .jar file but outside the .jar file.
Note 2
I understood it is not appropriate to use the export command as a command that is executed by the processbuilder, much like the cd command.
*That is why the question focuses on reproducing the "longterm/lasting" effect/availability of setting the environment variable, rather than "how to execute the export command.".
when I execute the .jar of that solution and check if the environment variable $u is still set after execution, I find it is not.
That is the expected behavior. Some operating systems support the concept of global "environment" variables. But not UNIX. In UNIX like operating systems every process has its own private copy of environment vars. A process cannot modify the environment of another process. This is also why changing the current working directory in a process does not change the cwd of its parent process. Each process has its own cwd.
The usual way to workaround that limitation is for the child process to write the var=val pairs to stdout and the parent shell then evaluates that output to set the vars in its environment. For illustration assume the command is the following shell script, named myscript.sh, rather than a Java program:
#!/bin/sh
echo VAR_A=val_a
echo VAR_B=val_b
Then the parent shell does
export $(./myscript.sh)
I am working on a ubuntu 14.0.4 machine.
I exported a variable TEST_HOME in my .bashrc file using
export TEST_HOME=/home/dev/code/test
When I tried echo $TEST_HOME from terminal, it returned /home/dev/code/test
So far, so good.
When I try from Java code :
String value = System.getenv("TEST_HOME");
value is null.
Am I missing something here?
Mentioning the variable in .bashrc will work only for programs started from shell. For system wide environment variables mention it in /etc/environment.
Refer Ubuntu Environment variables
.bashrc would set environment variable only for bash shell. To set it system wide set it in /etc/environment file.
Since you are using eclipse, and it does not run within bash shell, it is not getting the variable you are setting. If you run your programme using java command line in your terminal then it should get it.
Setting variable in /etc/environment would make it available to eclipse. You will need to restart your machine once you update /etc/environment.
I did the following steps but java does not seem to be working for me, do I need to restart my system if yes then why?
1. Right click My Computer->Advence Option->Environment Varibles->System Variables
2. variable : Path->Edit
3. Copy your jdk bin directory i.e.C:\Program Files\Java\jdk1.7.0_51\bin
4. Paste it after putting a semi-colon(;) in value section
i am getting below error while running java in cmd
C:\Users\User>java -version
Error: opening registry key 'Software\JavaSoft\Java Runtime Environment'
Error: could not find java.dll
Error: Could not find Java SE Runtime Environment.
No, but you will need to close and recreate any cmd windows, running java programs, or the like.
To check it's correct, open a new cmd window and type set -> review the information for PATH and JAVA_HOME.
You should set two things:
JAVA_HOME
PATH
The first lies under the "User variables for [your user name]" section. Add the JAVA_HOME variable and set it to where your jdk is installed. i.e. C:\Program Files\Java\jdk1.7.0_51\
The second lies under the "System variables" section. You should find the "path" variable, and edit it. Then, append ";%JAVA_HOME%\bin" (minus the quotes) to the end of the path variable.
After you have done this, save the variables and close that window clicking the "ok" button. Close down all instances of the command prompt (and any IDE you may be developing with such as Eclipse or NetBeans), and reopen one command prompt. Then, if you would like to test whether or not your changes worked and are in effect, try the following:
echo %JAVA_HOME%
This should output where you set your java home variable to.
echo %PATH%
At the end of what is output, you should see your java home\bin addition to the path variable
java -version
If you can run this command from the command line, it means that your environment was set up correctly and java is now in your path.
Not necessary. Quit and Open the command prompt again (if any) that runs the java process and type java -version to check if it installed successfully.
If you are using mac, source ~/.bashrc or source ~/.zshrc should refresh your environmental variable. No need to re-start the terminal even.
No, you don't need to restart your system.
However, you have to restart your command prompt application to update the changes done on Environment variables.
NO.
You don't need to restart the system, but just the application like cmd or any IDE you are using for java development (i.e Eclipse) need to restart.
And to confirm that the java path is set that you have mentioned in JAVA_HOME environment variable, you can open cmd and you can check with commands echo %JAVA_HOME% or echo %PATH%.
I reinstall the JDK and set the JAVA_HOME and PATH variable again. now it's working.
In my case under user variable section(for particularuser) JAVA_HOME set to jre not jdk path . I change it to JDK path , It worked for me
yes need to restart cmd prompt after env variable change
I followed all the steps on pig.apache.org ,but not able to remove this error by setting java variable. I set the variable earlier while installing java jdk but its asking again for the variable.
You need to understand how environment variables work in Linux (or Windows).
The chances are that you only set JAVA_HOME temporarily in the shell that you used to do the installation. To set JAVA_HOME permanently (on Linux / UNIX) you need to set it in a shell "rc" file that gets run each time a new shell is created. (It depends on which shell you are using, but man can tell you about that ...)
if you are installing PIG on ubuntu do this
open bashrc file using below command and and lines to the end of the file
vi ~/.bashrc
export JAVA_HOME=/usr/lib/jvm/java-1.6.0-openjdk-i386
then log out or Restart your system and try again.
For CentOS
Create a new file called java.sh
vim /etc/profile.d/java.sh
Within this file, initialize the necessary environment variables
export JRE_HOME=/usr/java/jdk1.5.0_12/jre
export PATH=$PATH:$JRE_HOME/bin
export JAVA_HOME=/usr/java/jdk1.5.0_12
export JAVA_PATH=$JAVA_HOME
export PATH=$PATH:$JAVA_HOME/bin
Replace java path with your java installation.
Source
To set your JAVA_HOME variable for your current shell, use the export command. This will immediately create an environment variable for JAVA_HOME, but the variable is lost when your shell is closed.
export JAVA_HOME=<path_to_java_sdk>
The best way to set the JAVA_HOME environment variable for Pig and Hadoop use is by adding it to your local rc file. The reason for this is that some distributions of Hadoop (Cloudera, Hortonworks, MapR) may include their own Java installation for the Hadoop application itself. You may not want to create a global environment variable, since it may interfere with Hadoop/Hive/etc.
To have the variable set for individual Hadoop/Pig users on the server, they need to run the following command:
echo "export JAVA_HOME=<path_to_java_sdk>" >> ~/.bashrc
Afterward executing the command, they can source ~/.bashrc to instantiate the environment variable.