An easy question about java and environment variables in ubuntu - java

I have Ubuntu 10.10 with java already installed. I am able to execute java command from any folder. I supposed that could be because I had java Classpath setted. But neither JAVA_HOME nor CLASSPATH are setted.
If I look at /etc/environment content I can see that PATH is setted to /usr/bin/ (among others). As 'which java' returns /usr/bin/java, is that the reason why I can execute java from anywhere? If not, why is it?

You can execute java because the command is on your path.
echo $PATH
will show you which directories are searched, in which order to find a particular program. Since /usr/bin is on your path, when you type java it will eventually look at /usr/bin/java. Note that in many systems this is a symbolic link (a file that points to another file) so /usr/bin/java often points to /etc/alternatives/java (which is also a symbolic link that points to the real executable).
Where the environmental variable JAVA_HOME comes into play is in tools and programs that check for JAVA_HOME and act on it instead of relying on the path. In most modern Linux systems, the work done by the alternatives subsystem replaces the earlier (more problematic) JAVA_HOME technique. That said, you might want to set JAVA_HOME anyway, should you encounter a tool that demands it.
One reason why JAVA_HOME is not as popular as it could be is that to access JAVA_HOME you need to run a shell, and not everyone wants to wrap every single Java item in a shell command.

Yes, if java binary (or a link to it) is on a folder that is listed on the path then you can execute java without specifying the path to it (for example /usr/local/java/latest/bin/java -jar x.jar)
JAVA_HOME and CLASSPATH have nothing to do with system path.
JAVA_HOME allow other software (or scripts) to know where to look for java installation.
CLASSPATH tells java where to look for classes (.class files resulting of compiling .java files).

Related

Do I need to set JAVA_HOME on MacOS?

I'm fairly new to Java. Many of the tutorials on installing Java on a Mac recommend setting up the JAVA_HOME environment variable. However, Java works fine on my computer with the JAVA_HOME variable being empty!
I have successfully used Java from the command line (using javac and java), in IntelliJ IDEA (there in the "Project Structure Settings" I've set up the "JDK home path" to /Library/Java/JavaVirtualMachines/jdk-16.0.1.jdk/Contents/Home) and in Sublime Text to compile and run Java files.
So, my questions are:
Why does Java work without the JAVA_HOME on my Mac?
Do I need to set up JAVA_HOME at all? What would be the consequences of not having it? Maybe it is not essential for some things, but is required for other things to work? Then what are these things?
Is this behavior different on other operating systems?
Outputs of some commands are:
echo $JAVA_HOME outputs an empty line
echo $CPPFLAGS outputs an empty line
which java
/usr/bin/java
echo $PATH contains /usr/bin
Why does Java work without the JAVA_HOME on my Mac?
Setting JAVA_HOME is not required for Java to work in your system. Some applications (e.g. Tomcat, Maven etc.) however look for JAVA_HOME system variable and if its value is not set, they may prompt you to do so.
Do I need to set up JAVA_HOME at all? What would be the consequences
of not having it? Maybe it is not essential for some things, but is
required for other things to work? Then what are these things?
Already answered above.
Is this behavior different on other operating systems?
It's same across all operating systems.
It depends on the program.
Most programs that depend on Java follow logic like this
if $JAVA_HOME ; then
use JAVA_HOME/bin/java
elif $(which java); then
use java command on PATH
else
error
fi
That being said, to be consistent, you should set it , and using SDKman or Homebrew does that for you

Java SE Embedded in arm linux

According to ORACLE's doc Quick Start for Platform Developers I used Jrecreate and got JRE .
I copied JRE to my Arm Linux, cd ./bin and inputted java -version.
But terminal displayed:-sh:java:not found
Do I miss any Share Library?
If you want an executable from your current directory to be callable by name you need . to be included in your $PATH, which isn't the case by default due to security concerns. Otherwise you have to provide an absolute or relative path to the executable such as ./java.
Another solution, which is the most common for java and other language development tools / runtime environments, would be to include the absolute path to your Java install's bin directory in the $PATH so that you can run java from anywhere. This is often done by first setting a $JAVA_HOME environment variable, then including $JAVA_HOME/bin in your path :
export JAVA_HOME="/usr/bin/java/" # or whatever your install path is
export PATH="$JAVA_HOME/bin:$PATH"
Either enter the location where your java executable actually is or add the java binary to your PATH environment variable.
If you read Oracles documentation, you need to do this after having run Jrecreate. (Please adapt destDir)
Recursively copy destDir from the host to the device directory where you want the JRE installed. For example:
$ scp -r /tmp/SmallJRE/* root#target:/opt/local/ejdk<version>/
If necessary, update the device's PATH environment variable to include the bin/ directory of the JRE. For instance:
$ PATH=$PATH:/opt/local/ejdk<version>/bin/
$ export PATH
Need some lib.and i used uclibc that don't support JRE.

How to set Terminal paths in OSX 10.8

I have run into a spot of trouble configuring my paths in OSX 10.9.3
To give some context, the issues started when Maven was causing issues referring to my Java version as 1.7, from stackoverflow i took the advice of simply updating my JAVA_PATH
I ran "which java" which gave me:
/usr/bin/java
SO I ran:
export JAVA_HOME=/usr/bin/java
However "man -v" and "java -version" both now complain:
Error: JAVA_HOME is not defined correctly.
We cannot execute /usr/bin/java//bin/java
What I am confused about is that blogs, posts and people are saying check .profile and .bash_profile or .bashrc, only .profile exists on my machine, and nothing seems to take effect there. The only joy I have is setting paths in /etc/paths
So my question has a few part, how do I get my original java path back? Where are these "export" paths set or where can i find this reference to JAVA_HOME and lastly, despite defining "JAVA_HOME=/usr/bin/java" is it returning the Error that claims the path is "/usr/bin/java//bin/java"
Any help would be good
JAVA_HOME should be set to the name of the directory that contains the bin directory that contains the Java executables (in particular, java itself).
Assuming you do have a /usr/bin/java directory, it probably contains a jre subdirectory that contains the bin directory that contains java and related executables.
So you should probably be setting JAVA_HOME to /usr/bin/java/jre rather than /usr/bin/java.
On the side note: its a bit awkward place to put your java tbh. I would put it in usr/lib/java

Setting my JAVA_HOME variable for Hadoop

I have tried setting my JAVA_HOME variable for Hadoop to no avail.
My Java runtime is in /usr/bin/java, but Hadoop keeps trying to access it in /usr/bin/java/bin/java.
I have set the JAVA_HOME in my .bash_profile and in bin/hadoop, but neither works. I am guessing that some other path setter is overriding this. Is this a known problem and what can be done about it?
A couple of other things I tried:
echo $JAVA_HOME from the hadoop script
add $JAVA_HOME to the conf/hadoop-env.sh
In your case:
JAVA_HOME = /usr
The code assumes that the executable is located at:
$JAVA_HOME/bin/java
It's unlikely your Java installation is in /usr/bin/java.
The executable may be there, or may just be a link to the executable in your installation.
IMO it's cleaner to use your actual Java JDK/JRE directory and use that instead: that's what JAVA_HOME is supposed to be.
Using Hadoop is quite a bit more complicated than setting an environment variable correctly; you may be in for a rough road. Consider something like Kiji's Bento Box to ease initial setup.

Java JDK Installation

OK, what am I doing wrong, this is driving me nuts.
I am trying to install the latest JDK (1.6.0_23). So, I downloaded it from Oracle's awful site and then ran the installation. I installed it to C:\Java\jdk1.6.0_23
Then, I created a JAVA_HOME User Variable that pointed to C:\Java\jdk1.6.0_23. I then added a piece to the end of my Path environment variable that says %JAVA_HOME%\bin.
However, when I try to simply open a command prompt and run simple java commands, I am told it is not a recognizable command. I have to manually cd into the that bin directory to do anything.
Do I also need a Classpath variable that points to the JRE? I noticed there was a Classpath variable there previously that pointed to jre/lib/QTJava.zip, but I deleted it.
If you have the JDK installed and a JAVA_HOME variable setup, do I still need the JRE in the classpath? I am running Windows 7 and do all of my development in Eclipse.
Maybe the problem is because you set JAVA_HOME as a user variable, but trying to reference it from the PATH which is a system variable (or is it?). You cannot do this, because system variables are evaluated before user variables.
There are two possible solutions:
1. Set JAVA_HOME as a system variable instead
2. Create a new user variable PATH and set %JAVA_HOME%\bin there. The user PATH and the system PATH variables will be concatenated at runtime automatically.
From http://social.answers.microsoft.com/Forums/en-US/vistainstall/thread/48b23109-9fbc-47c5-a5d1-465773f94704
(at the end)
1) Enable 'delayed variable expansion'
in the registry (see
http://batcheero.blogspot.com/2007/06/how-to-enabledelayedexpansion.html)
2) Change the '%' signs around var2 to
'!', e.g. "%var2%" becomes "!var2!"
I've done some limited testing on
Windows 7 and this appears to fix the
problem.
Maybe try that, see if it fixes it (I don't have windows here to try)
Do I also need a CLASSPATH variable that points to the JRE?
Strictly speaking, no. The CLASSPATH variable may be used if you try to run a java class and you don't use the -cp or -jar options.
The CLASSPATH variable doesn't need to point to the JRE. The java.exe command etc all know where to find the JRE's runtime classes. (And they don't look on the CLASSPATH for them anyway.)
For the PATH problem, try running:
C:\Java\jdk1.6.0_23\bin\java.exe -version
If that doesn't work then there's a problem with your actual installation. If it does work, try looking at what JAVA_HOME and PATH are set to in the environment variables of the command shell.

Categories

Resources