Set JDK path in linux - java

Something really weird occurs. When I type in:
which java
the output is like:
/private/me/jdk1.8.0_20/bin/java
and when typing in:
echo $JAVA_HOME
the output is:
/usr/java/jdk1.6.0_24
I want to use 'jdk1.6.0_24' and I change all the things in '/etc/profile' and '~/.bashrc' to point it to 'jdk1.6.0_24', such issue still existed. The java I use is still 1.8. Why?

I am pretty sure you need to update-alternatives:
sudo update-alternatives --config java
and select java 1.6.0

Try updating your path as follows:
export PATH=/usr/java/jdk1.6.0_24/bin:$PATH
don't use export PATH=$PATH:/usr/java/jdk1.6.0_24/bin unless you uninstall first the "default" java (if you use this then the java binary in /usr/bin will be found first, which is not what you want).
There is a caveat on this: the binaries in /usr/java/jdk1.6.0_24/bin will be found before than the ones in the rest of the path, which is harmless because you only have java-related binaries on /usr/java/jdk1.6.0_24/bin
caveat #2: make sure you are not redefining PATH after this line or in another script

Go to your home, and show hidden file, then you will find the file .bashrc. Edit and go to the end of the file, then add
export PATH=$PATH:/usr/java/jdk1.6.0_24/bin
$PATH means the current path, in order to append the new value
Then, you will use jdk1.6.
Each time you write java -version, you will find the most recent version (jdk1.8), but your program will use jdk1.6

Related

Maven installation issues: JAVA_HOME should point to a JDK not JRE?

I'm super new to CS and very unfamiliar with UNIX/Bash vocab.
I'm currently trying to install Maven, and I made a few mistakes by copy pasting old installation instructions (with incorrect version numbers) and now the whole process is so messed up. I first tried to follow the given instructions on the website, that didn't work, then I tried a few other ones, and now I'm worried that I'm left with a mess by following different sets of instructions.
I've reached a point now where if I type in mvn -version I get the following:
The JAVA_HOME environment variable is not defined correctly
This environment variable is needed to run this program
NB: JAVA_HOME should point to a JDK not a JRE
Originally, my JAVA_HOME was set to jdk-install-dir, which still gave me the above error message. I tried redownloading the jdk (version 13.0.1) from Oracle, dragged that file to my home directory, and unzipped it. Then I set my JAVA_HOME to that unzipped file, jdk-13.0.1.jdk, and updated my PATH variable. After all this, I'm still getting this same error message, and I'm not sure what to do.
For the reference, here are what some relevant environment variables are set to (I didn't include irrelevant info from PATH):
~ echo $JAVA_HOME
jdk-13.0.1.jdk
~ echo $PATH
/usr/local/apache-maven/apache-maven-3.6.3/bin:/opt/apache-maven-3.6.3/bin:jdk-install-dir/bin:/usr/local/apache-maven/apache-maven-3.3.9/bin:/opt/apache-maven-3.6.3/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Users/allyson/apache-maven-3.6.3/bin:/Users/allyson/apache-maven-3.6.3/bin:jdk-12.0.1.jdk/bin:/Users/allyson/apache-maven-3.6.3/bin:JDK-13.0.1.jdk/bin
~ echo $M2
/usr/local/apache-maven/apache-maven-3.6.3/bin
~ echo $M2_HOME
/Users/allyson/apache-maven-3.6.3
One thing I'm curious about: for the M2 and M2_HOME variables, is it supposed to be that one is in my home directory (/Users/allyson) and one is for /usr/local?
OK, take a deep breath, and we'll walk through this. Each of these environment variables has a purpose, and once you understand what those purposes are, this makes a lot more sense. Mixing tutorials is not necessarily a problem, but you'll want to understand what you're doing, rather than just blindly copy values from the internet.
JAVA_HOME is intended to identify to the system environment where to find a java runtime environment. It needs to be set to the full path of where your JDK has been installed. On windows, this might be C:\Program Files\Java\jdk-13.0.1. On a Linux system, you have a bit more flexibility. Common locations might be /opt/java/jdk-13.0.1 or /usr/local/java/jdk-13.0.1. If you installed your JDK somewhere else, then you need to use that path instead. The message NB: JAVA_HOME should point to a JDK not a JRE refers to a common mistake when installing maven -- maven requires a JDK, not a plain JRE. This error is so common that any time JAVA_HOME points to a folder that isn't a JDK, it prints this warning (even if the folder in question isn't actually a JRE).
M2_HOME is supposed to be set to the full path where maven is installed (i.e. the place where you unzipped it). This more or less helps maven "find itself" if it should need to for whatever reason. Strictly speaking, this one isn't necessary. (It's not set on my system, and maven works fine for me). It's mostly a convenience for setting the next environment variable.
M2 is the full path to the folder where the maven executable is. This will almost always be $M2_HOME/bin, but it's certainly possible to do weird things, and this will let you work around those situations. Obviously, this won't work if you didn't specify $M2_HOME. This one isn't strictly necessary, either, and is mostly a convenient way of setting up the next one.
PATH is where your Linux system looks to find programs to run when you type their name on the command line. For ease of use, you'll want to make sure that the maven and java executables are included somewhere in the : delimited list. Most Linux distributions already have a default PATH set up for you in a shell resource file of some kind. You'll want to refer to their documentation for how to add another entry to the path, but a common idiom would be PATH=$PATH:$M2 (which would append the value of $M2 to the value of $PATH and then store the result back into PATH. If you didn't set up $M2 or $M2_HOME, you'll need to do something else.
So, TL;DR, if you installed your JDK in /opt/java/jdk-13.0.1 and unzipped maven into /opt/maven/apache-maven-3.6.3, your bear minimum working values are:
export JAVA_HOME=/opt/java/jdk-13.0.1
export PATH=$PATH:$JAVA_HOME/bin:/opt/maven/apache-maven-3.6.3/bin
And if you wanted a complete set
export JAVA_HOME=/opt/java/jdk-13.0.1
export M2_HOME=/opt/maven/apache-maven-3.6.3
export M2=$M2_HOME/bin
export PATH=$PATH:$JAVA_HOME/bin:$M2
It's worth noting that most Java IDEs will include a GUI for setting up maven and Java within the IDE (the settings will typically only work within that IDE). It's often much easier for beginners to get up and running that way.
$JAVA_HOME should be a full path, not the dir name.
scream#kafka1:~$ echo $JAVA_HOME
/opt/java/jdk-11.0.5/
scream#kafka1:~$
I'm working on Ubuntu 20, and my own issue was about the jdk version located in the JAVA_HOME variable, which wasn't the right version. This was my trip around that issue:
First, What is the JAVA_HOME value?
$ echo $JAVA_HOME
$ /usr/lib/jvm/jdk-11.0.10
Next question/step, where is the Java app pointing to?
(Source: https://stackoverflow.com/a/42706056/2373421)
$ java -XshowSettings:properties -version 2>&1 > /dev/null | grep 'java.home'
java.home = /usr/lib/jvm/jdk-11.0.12
So, the JAVA_HOME variable wasn't keeping the right value. Let's change that:
$ export JAVA_HOME=/usr/lib/jvm/jdk-11.0.12
The test outcome is right now:
$ mvn -v
Apache Maven 3.8.1 (05c21c65bdfed0f71a2f2ada8b84da59348c4c5d)
Maven home: /opt/apache-maven-3.8.1
Java version: 11.0.12, vendor: Oracle Corporation, runtime: /usr/lib/jvm/jdk-11.0.12
I'm doing this for future me's bc after all the links in StackOverflow, the solution was to check mvn.cmd file on C:\Program Files\Maven\apache-maven-3.6.3\bin...
On line 52 it sets the JDK, but it didn't had the "/bin" where my OpenJDK 15 stored the java.exe
This fixed the issue on Windows 11 but it could serve as an idea for other OS. Hope that it helps!

error when installing Java EE SDK on Linux

I did have JDK installed, and also put $JDK_HOME/bin on my PATH. However, I still have problem to install Java EE. Could you guys give me some hints? Thanks.
dav#Bruno:~$ ls
[some stuff...]
33511405_3645.jpg glassfish stuff
bea hplip-3.11.12-plugin.run temp
bin hpscan001.png Templates
C03_08.txt java_ee_sdk-6u4-jdk7-linux.sh Tomcat
c4c jdk1.7.0_10 Ubuntu One
[some stuff2...]
dav#Bruno:~$ echo $PATH
/home/dav/jdk1.7.0_10/lib:/usr/share/java:/home/dav/jdk1.7.0_10:/home/dav/jdk1.7.0_10/bin:/home/dav/Fluent.Inc/bin:/home/dav/bin:/usr/lib/lightdm/lightdm:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/home/dav/jdk1.7.0_10/lib:/usr/share/java
dav#Bruno:~$ sudo sh java_ee_sdk-6u4-jdk7-linux.sh
Could not locate a suitable jar utility.
Please ensure that you have Java 6 or newer installed on your system
and accessible in your PATH or by setting JAVA_HOME
dav#Bruno:~$
I think the problem you're facing is that the PATH variable you're looking at is set for the user 'dav', but because you're sudoing the sh command, the PATH changes to that of root, which probably doesn't contain the JDK.
I think you should try explicitly setting the JAVA_HOME environment variable, try running:
sudo JAVA_HOME=/home/dav/jdk1.7.0_10 sh java_ee_sdk-6u4-jdk7-linux.sh
in case someone still gets frustrated over this:
("Could not locate a suitable jar utility.
Please ensure that you have Java 7 or newer installed on your system
and accessible in your PATH or by setting JAVA_HOME")
First of all make sure you are not using "sudo" to install java ee.
when you use sudo you use a different environment (root, with not necessarily the same
environment variables). in this case (ee installation) sudo is not required.
In other (which ever) cases when root access is required make sure the required user/system variables are set on system level, not (just) on user level.
to set variables at system level instead of user level define the variables in a system level script, i.e. /etc/profile or /etc/profile.d
otherwise, if you are not using SUDO trying to install jee, and still have this "JAVA_HOME not set"-problem, try this:
the cause seems to be the JAVA_HOME variable that can not be found by the install script.
therefore adjust ~/.bashrc to define JAVA_HOME.
JAVA_HOME of course has to point to your oracle java (se/jdk) installation. If you haven't installed it, that's the first thing to do. JAVA_HOME should point at the root directory of the installation (not to the bin directory in it), i.e.:
export JAVA_HOME=/usr/local/jdk1.7.1_02.
Next change path like so:
PATH=$JAVA_HOME:$JAVA_HOME/bin:$JAVA_HOME/jre/bin:$PATH
keep the faith
don't let frustrations get the better of you (perhaps making you take it out on your colleagues or loved ones :o )
don't allow stress to build up too much. take a break in time to watch a funny video or
take a stroll to the coffee machine, or have a little chat with that cute person a couple of desks away from you.
You'll need to create an update alternative for the "jar" executable like:
sudo update-alternatives --install "/usr/bin/jar" "jar" "/usr/lib/jvm/jdk1.6.0/bin/jar" 1
Note: You may need to use appropriate path in your case.

setting up classpath. javac is not recognized

I am trying to run my java program from command line.
I read an article about setting up classpath, but I get an error of javac is
not recognized as internal or external command. What should I do? (I dont want to set a permanent CLASSPATH)
This is what I have done in my command line
D:\user> set path=%path%;C:\Program Files\Java\1.7.0_07\bin
D:\user> cd testing
D:\user\testing> javac firstProgram.java
'javac' is not recognized as an internal or external command,
operable program or batch file.
Thank you
Assuming that the PATH is correct1, the most likely cause is that you have a JRE installation ... and a JRE doesn't include a java compiler. You need a JDK installation if you want to compile from the command line.
(You can confirm this by looking in the C:\Program Files\Java\1.7.0_07\bin directory to see if it contains a javac.exe file. A JRE won't ...)
Where can I find the Java compiler to download..
You need to download one of the JDK installers; see http://www.oracle.com/technetwork/java/javase/downloads/index.html
1 - I don't think quotes are required in a PATH variable on Windows. At least that's what various examples that Google found for me seem to imply. But I've never really understood the logic behind quoting in Windows ...
Its an issue related to Program Files.
First make sure that your JDK Folder is installed in Program Files or Program Files(x86) or any other folder.
Then you should use the path of bin folder in " ". Because command prompt does break the string at space. When you will write it in " " then it will take is as a whole String.
You try these commands
set path=%path%;"C:\Program Files\Java\1.7.0_07\bin"
or
set path=%path%;"C:\Program Files(x86)\Java\1.7.0_07\bin"
It might help you to get out of this.
Better do it in Environmental variable and check it!
try below command is recognized from command prompt
C:\Program Files\Java\1.7.0_07\bin\javac ab.java
This is just to verify your javac
Here's how you can set the path temporary, meaning if you close and reopen "command prompt" you will have to set the path again.
Assuming the path is C:\Program Files\Java\jdk1.6.0\bin
TYPE IN C:\Program Files\Java\jdk1.6.0\bin AND HIT ENTER
that's it.
The commands D:\user> set path=%path%;C:\Program Files\Java\1.7.0_07\bin works well for me
Adding few more information to this:
Please check the version of JDK and JRE installed on your computer. Recently I faced the same problem even after setting the PATH. It gives the error "javac - command is not recognised"
Solution is there must be similar versions of JDK as well as JRE
E.g.: JDK 1.7.75 along with JRE 1.7.75

Setting java home

I'm trying to use the command "ant build".The message says java home is not defined correctly we cannot execute /usr/bin/java//bin/java <notice the 2 slashes>
If i use the command echo $JAVA_HOME it returns usr/bin/java . What needs changing here?
The $JAVA_HOME variable does not refer to the java executable, but to the parent directory of the bin/java executable itself. This is the reason Ant complains of not being able to execute some cryptic /usr/bin/java//bin/java.
For example, in my case (Ubuntu 12.04, OpenJDK) the java home is set to
/usr/lib/jvm/java-6-openjdk-amd64/jre
where obviously there exists a /usr/lib/jvm/java-6-openjdk-amd64/jre/bin/java, of which /usr/bin/java ultimately represents a symbolic link.
Looks like Ant is assuming /usr/bin/java to start. Perhaps you don't need to set it.
Or try adding a leading slash (/usr instead of usr).
JAVA_HOME shouldn't be pointed to the java executable, rather it should be the parent directory of 'bin' where javac the compiler is located.
Usually the '/usr/bin/java' is a symbolic link to the actual executable in somewhere else, e.g., '/usr/lib/java/jdk*/bin/java', in which case the java home should be /usr/lib/java/jdk*.

regarding javac

javac is not internal or external command error is coming. I have set the path. then also it is giving the same error.
Are you sure you installed the JDK?
The JRE (aka "Java Runtime") does not contain javac, this is only part of the JDK ("Development Kit")
You should first examine the PATH by executing this command:
echo %PATH%
Among these folders should be at least one that looks similar to this:
C:\Program Files\Java\jdk1.6.0_20\bin
Maybe you left out the bin at the end, maybe there isn't such a folder name at all in your PATH? We cannot know. If you tried this and are still unsure, post the value of the PATH so we can give you real tips instead of just guessing.
If you're on windows, and running from a command prompt you need to reopen it to force any PATH changes to take effect

Categories