How to set java environment variable on mac - java

How do I set a java environment variable that can be read with System.getenv() on Mac OS X Lion? I developing in Eclipse.
I used this command in the terminal export VAR_NAME=Value but System.getEnv(VAR_NAME) returns null.
I tried java -DVAR_NAME=Value but nothing is executed. I only get the java arguments help.
I created in my current project folder the file .bash_profile with the content: export VAR_NAME=Value. It's not working.
I also created the folder .MacOSX with a single file inside environment.plist and in this file I wrote the property and its value using XCode. Nothing is working.
How do I do it?

It is not clear what you are actually doing here. If you are trying to set an environment variable for a Java command you are launching from Eclipse, then the simple solution is to set it via the Eclipse command launcher configuration. Another alternative is to:
exit Eclipse,
run export VAR_NAME=Value in a command shell instance, and
launch Eclipse from that same shell instance.
Re the things you tried.
I used this command in the terminal export VAR_NAME=Value but System.getEnv(VAR_NAME) returns null.
If you run the export command from a command prompt, and then immediately launch the command from the same command prompt, that should work. The export command is telling the shell instance used by the command prompt to add VAR_NAME to this list of environment variables it exports to child processes that started after running the export command. Child processes that have all ready been started won't see the changes.
I strongly suspect that you ran the export after you launched Eclipse ... or that you didn't start Eclipse from that shell instance. If either of those two is true, the export command wouldn't affect Eclipse's environment variables which it (by default) passes on to any Java program you launch from Eclipse.
I tried java -DVAR_NAME=Value but nothing is executed. I only get the java arguments help.
That fails for a couple of reasons:
the -DVAR_NAME=... is setting a system property not an environment variable, and
you haven't told java the name of the class that you want to start!
I created in my current project folder the file .bash_profile with the content: export VAR_NAME=Value
That only affects new shell instances that are launched after you created the file. It doesn't affect the existing one.
I also created the folder .MacOSX with a single file inside environment.plist and in this file I wrote the property and its value using XCode.
I've no idea what that would do. Where did you create that folder?

1.open the Terminal
2.export VAR_NAME=Value
3.open /Applications/Eclipse.app

Related

System.getenv() call in JVM

I am setting environment variable in my machine using export MY_KEY=foo. And I am trying to fetch it in JVM using System.getenv("MY_KEY"). This returns null. But running echo $MY_KEY shows foo on the terminal.
I have tried restarting the IDE. Doesn't work, still.
The environment variable is only available to sub processes of the shell that exported it. Did you start your IDE from that shell?
If you want the variable to be available all the time, you need to add it
to the /etc/profile file or create a extra file in /etc/profile.d. It depends on your operating system.

'JAVA_HOME is not set' error while installing pig. What to do about it?

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.

setting classpath using setenv.sh for a project

I want to write an sh file that will set classpath in Linux.
I tried using:
export ClASSPATH=$CLASSPATH:$ABC_HOME
However, when I run the .sh file it doesn't set the classpath.
Executing the bash script only sets the environment for the child process. The "source" command may do what you want...
https://askubuntu.com/questions/53177/bash-script-to-set-environment-variables-not-working
You should make it a shell function, or source it. Because the environment variables are local to the shell (the one started by invoking your shell script).
Please read the advanced bash scripting guide. See also this answer to a similar question.

reading environment variables from java using Eclipse

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);
});

Set default classpath to use in `java` command in command prompt

What I'm trying to do is running a .java source by compiling and running it from command prompt (not using any IDE) using commands javac and java and the program connects with MySQL, so everytime I run the program from cmd, I need to specify path of the MySQL connector using -classpath switch of java. And entire command to run the program gets something like below:
java -class .;path/to/connector/mysql-connector.jar MySQLConnect
where I want it to be as simple as for other programs like java MySQLConnect
and it should run the program.
Is there any way I can add the connector's path to environment variables of Windows that java make use of it. I already tried by creating a new CLASSPATH variable in Windows environment variables and added absolute path of the connector with file name along, but that didn't worked.
Please provide me the workaround of this Windows and Ubuntu as well.
Thanks.
WIndows : Copy mysql-connector.jar to C:\Program Files\Java\jdk1.6.0\jre\lib\ext
and copy the same file to C:\Program Files\Java\jre1.6.0\lib\ext
go to My Computer -> Properties -> Advanced -> Environment Variables
Set these paths
JAVA_HOME
C:\Program Files\Java\jdk1.6.0
PATH
C:\Program Files\Java\jdk1.6.0\bin;
CLASSPATH
.;C:\Program Files\Java\jre1.6.0\lib\ext\mysql-connector.jar;.;
open a fresh command propmpt
type
java -version press Enter
WINDOWS
Go to My Computer -> Properties -> Advanced -> Environment Variables
then find CLASSPATH variable in System variables and click on edit to add your jar file there.
LINUX or MAC
In your shell use a variable CLASSPATH in your .bashrc or .profile to set a default class path.
Set classpath=%classpath%;location of mysql-connector with connector file name.jar. For example:
set classpath=%classpath%;D:\TECHNICAL\JAVA WORLD\JDBC\mysql-connector-java-5.1.18-bin.jar;
D:\TECHNICAL\JAVA WORLD\JDBC\ is the location of mysql-connector.
To set this path go to Advanced System Settings->Environment variables->User variables->CLASSPATH->Edit, then type set classpath=%classpath%;D:\TECHNICAL\JAVA WORLD\JDBC\mysql-connector-java-5.1.18-bin.jar; and finally press OK.
I have done in this style. After that, I got result of programs correctly. Then there is noCLassNotFoundException.

Categories