Confusion about Java Versions - java

So I'm trying to get the latest version of Java. When I run:
java -version
I get:
java version "1.6.0_65"
Java(TM) SE Runtime Environment (build 1.6.0_65-b14-462-11M4609)
Java HotSpot(TM) 64-Bit Server VM (build 20.65-b04-462, mixed mode)
When I run:
javac -version
I get:
javac 1.6.0_65
Now I've just downloaded and installed JDK 8. When I go into System Preferences --> Java --> Update, I see:
Your system has the recommended vesion of Java.
Java 8 Update 05.
I guess I have a few questions:
1) Don't I want the JDK and my version of Java to match up?
2) Why does my Java Control Panel claim I have Java 8, but my work in the terminal (when checking my Java version) says otherwise?
Thanks for the help,
Mariogs

The probably "simple" answer is you have two versions of java installed. On the command line you currently use 1.6. Thus the old one is active. In system preferences you see the version of java 8.
Windows:
You can change the version of the command line to java 8. Set PATH environment variable and JAVA_HOME or via windows preferences. I recommend the first one.
Mac: /usr/libexec/java_home is the starting point for switching java versions on the command line. Check out this post to understand how to handle different java versions on the Mac. IMHO this answer is a good solution.

The Java Platform offers both the JRE and the JDK in order for users to run Java programs. The JRE stands for the Java Runtime Environment, and the JDK stands for the Java Development Kit.
The JDK is meant for Java developers - that is, those who build applications/write programs in Java. It contains tools that are needed for Java coding, including -javac to compile programs.
The JRE is meant for regular users - those who only need to run Java programs on their computer and are not interested in development.
The reason for the discrepancy in your case is because you're looking at the JRE and JDK and trying to compare the two. The current JDK that you have is Java 8, whereas the current JRE that you have is 1.6.0_65. It is problematic that your JRE version does not match your JDK version, but without your PATH variable or other information about your install, we can't help you fix your installation.

1) Yes, if you use the JDK at all, you want the JRE (runtime environment) to come from the JDK (development environment) (a JDK necessarily includes a JRE).
2) Likely your path variable is set so that you invoke Java from your Java 6 installation; you need to find the equivalent for your Java 8 installation and set the path for that. Without information about your operating system, we can't help you do that.

We should know the reason for this
Our OS comes with a predefined (built-in)set of tools and utilities. When we try to execute the command e.g. cls in the Windows command line then it is already present in system path variable and os will refer the corresponding binary of cls to execute the command.
However, when we install any third party tool/software then path variable is not updated accordingly.
When we install different versions of java on your system then installations go to different directories. E.g. JDK installation directory for Windows will be
C:\Program Files\Java\jdk1.8.0_161
Similarly, JRE installation directory for Windows will be JDK installation directory for Windows will be
C:\Program Files\Java\jre1.8.0_161
We need to update the path variable of OS to point to the appropriate directory. If we set the path of JDK then it will execute a binary from JDK bin directory.
Solution
we need to update JDK or JRE version specific directory location into PATH Environment variable.

Let me see if I can clear it up for you.
1)Yes, arguably you nearly want this to be true.
2)It could be few things, but most likely that a previous instillation was not properly removed. So one gets called instead of the other.

Related

Confused about what jdk and jre my command prompt is using for java

I was super confused about compatibility on a java tool I was using and ended up downloading different versions of java so now I do not know what versions of jdk or jre I am actually using. When I compile I use -- release 8 in my command prompt, what does that exactly mean?
Currently my programs folder has jdk-18 in the java folder
and my program files(x86) folder has
jdk1.6.0_45
jdk 1.7.0_80
jre 1.8.0_ 321
jre6
jre7
My java home environment variable is C:\Program Files\Java\jdk-18
My classpath has the file C:\Program Files (x86)\Java\jdk1.7.0_80\lib\tools.jar
when I type java -version in my command prompt I get
java version "1.8.0_321"
Java(TM) SE Runtime Environment (build 1.8.0_321-b07)
Java HotSpot(TM) Client VM (build 25.321-b07, mixed mode)
So does that mean my cmd uses jre 1.8.0_ 321? What about the jdk? How do I change it if I need to?
When you run java -version - first java found in PATH environment variable is used. To be sure use commands where java (Windows) or which java (Linux) - it will tell you the location.
When you run mvn install - JDK found in JAVA_HOME environment variable is used. To be sure use mvn -version - it will tell you the location.
When you run from IDE - IDE settings matter, usually you specify JDK per project.
When you run javac --release 8 Something.java you are asking compiler to produce output compatible with the version you specified - it has nothing to do with JDK you are actually running it on. This flag was added in JDK 9, so if it doesn't fail for you then it means you are running on JDK>=9.
If you want to be 100% sure just fully qualify the path - for example on Windows "C:\Program Files\Java\jdk1.8.0_152\bin\javac.exe" Something.java
So does that mean my cmd uses jre 1.8.0_ 321?
Yes.
What about the jdk?
Unclear.
Run javac -version and that will tell you what version compiler you will run by default.
The versions of the Java tools that you get when you type java, javac, jar (etcetera) at the CMD prompt are solely determined by the PATH environment variable setting in effect at the command prompt.
The other variables do other things ... but not this.
How do I change it if I need to?
If you want to change what typing java does, change PATH.
Note that the PATH variable is a standard Windows environment variable that affects all* commands, not just the Java tools. There should be lots of tutorials on how to set PATH, and on the Windows CMD prompt in general. You should probably take the time to read them.
You can also just use the absolute path for the Java tools; e.g. "C:\Program Files (x86)\Java\jdk1.7.0_80\bin\java.exe" is probably the correct path for the Java 7 java command. (You can easily check ... and find the correct path by looking at the installation tree.)
Regarding your other variables:
My java home environment variable is C:\Program Files\Java\jdk-18
That is presumably JAVA_HOME. That tells (many) 3rd-party tools which Java tools to use. But unless you have configured PATH to depend on JAVA_HOME, it won't alter what typing java does.
My classpath has the file C:\Program Files (x86)\Java\jdk1.7.0_80\lib\tools.jar
That is presumably CLASSPATH. That variable provides a default path that java will use to find classes to load. For example, if you run java com.acme.Example, it will use CLASSPATH to search for the compiled Example class.
So "C:\Program Files (x86)\Java\jdk1.7.0_80\lib\tools.jar" is almost certainly incorrect. (That JAR file contains the classes that implement various Java tools. It doesn't need to be on the classpath, unless your Java application is running one of those tools. And even then there would need to be other things on the classpath for your application to work.
You need to do some reading on what the Classpath is, how it works, and how to set it correctly. Setting it to stuff randomly is a sure fire way to make your Java code not work ...

Cannot use Java 7 installation if Java 8 is installed

I normally still use Java 7 for all my coding projects (it's a company "politics" issue), but I installed Java 8 for one third-party project I am contributing to. Now, it seems I cannot have Java 8 installed in Windows 7 x64, and still use Java 7 by default:
C:\>"%JAVA_HOME%\bin\java.exe" -version
java version "1.7.0_55"
Java(TM) SE Runtime Environment (build 1.7.0_55-b13)
Java HotSpot(TM) 64-Bit Server VM (build 24.55-b03, mixed mode)
C:\>java.exe -version
java version "1.8.0_05"
Java(TM) SE Runtime Environment (build 1.8.0_05-b13)
Java HotSpot(TM) 64-Bit Server VM (build 25.5-b02, mixed mode)
As you can see, JAVA_HOME is completely ignored.
I also have Java in the path, using "%JAVA_HOME%\bin", which resolve correctly to Java 7 when I check the path in a DOS box, but it still makes no difference.
I checked in the "Java Control Panel" (not sure if this affects the default command-line Java version). Under the "Java" tab, the "View..." button, you get to see "registered" Java versions. I can add all the versions under the "User" tab, but under "System" there is only Java 8, and no way to change it.
Am I missing something, or did Oracle just make it impossible to use Java 7, unless I de-install Java 8? I don't want to have to specify the "source" and "target" everywhere, and I don't even know if it is possible for me to specify it everywhere, where Java is used.
EDIT: What I did is I de-installed all Java. Then installed the latest Java7 (both 86 and x64), and then the latest Java8 (both 86 and x64). After I did that, I noticed that the x64 JDK was gone. It seems Java8 killed it. So I re-installed the JDK 7 x64, after the JDK 8 x64. Still, JDK7 x64 did not seem to "replace" the "java.exe" which is copied into the "Windows" directory itself (I assume THAT is the problem).
When you install jdk8 it adds an entry like this
C:\ProgramData\Oracle\Java\javapath
to beginning of your PATH environment variable, removing this entry should resolve your problem.
You can select the JRE version from the command line with the -version: option.
> java -version:"1.7" MyClass
should select the 1.7 JRE if installed properly.
The list of the properly installed JRE is in the registry, see the key :
HKEY_LOCAL_MACHINE\SOFTWARE\JavaSoft\Java Runtime Environment (32bit)
HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\JavaSoft\Java Runtime Environment (64bit)
You can set the "CurrentVersion" there if you want a different default version than the latest.
See http://docs.oracle.com/javase/7/docs/technotes/tools/windows/java.html#options
Don't modify your PATH to point to a particuliar JRE, let the special java.exe in Windows/system32 do the job.
Windows and Unix both find programs using their PATH environment variable. You have an java.exe in your Windows\System32 which is appearing before your "preferred" version of Java.
Change the PATH to be the one you need, or specify the full path when you need a different version.
2 Steps
1
Change registry key **HKEY_LOCAL_MACHINE\SOFTWARE\JavaSoft\Java Runtime Environment\CurrentVersion** to point to 1.7
2
Copy java.exe,javaw.exe and javaws.exe from your java 1.7 version to Windows\System32 folder
(Since the corresponding files of java 1.8 are already there, you might have to overwrite with admin permissions)
3
(OOps actually not required 3rd step )
Open a new cmd window and check
java -version
Looks like you have to check where in your PATH is located your JAVA_HOME variable, the PATH is evaluated from left to right. A tip for you is to do all your Java system variables configuration at the beginning of your PATH.
PATH = %M2_HOME%\bin;%JAVA_HOME%\bin;C:\ProgramData\Oracle\Java\javapath;...
Probably that's why after doing this:
- java -version
you are getting this:
- java version "1.8.0_05"
because there are other areas in your PATH that are pointing to other java.exe, for example C:\Windows\System32 or C:\ProgramData\Oracle\Java\javapath etc.
I had to make 2 changes for it to work:
Changed the Registry key 'Software\JavaSoft\Java Runtime Environment'\CurrentVersion' to 1.7 from 1.8
The Java 8 installation adds a new entry to the PATH environment variable 'C:\ProgramData\Oracle\Java\javapath'. I removed this entry from the PATH.

Switch between 32-bit and 64-bit JDK platform on Windows 7

I am using Windows 7 on my laptop. I am working with jpcap libraries which work only on 32-bit JDK. Having both 32 and 64 bit JDKs installed on my machine. How can I switch between 64-bit and 32-bit JDK?
I tried changing the %JAVAHOME% and %PATH% environment variables, but it didn't work. Should I change anything in the registry?
#Srikant Sahay, Thank you very much! It did work on widnows 8.1 64 bit OS.
I am able to Switch between 32-bit and 64-bit JDK platform on Windows 8.1 on demand. The trick, as Srikant suggested, is to set the path before all other path variables. Make sure you put fist in the path variable.
java -d32 -version ( or simply )
java -version
java version "1.7.0_51"
Java(TM) SE Runtime Environment (build 1.7.0_51-b13)
Java HotSpot(TM) Client VM (build 24.51-b03, mixed mode, sharing)
However, only one instance works at a time. Either you choose the 32 bit version or the 64 bit version. So, if you type "java -d64 -version" you will get
Error: This Java instance does not support a 64-bit JVM.
Please install the desired version.
Therefore, if you instead want a 64bit JVM you should put the JDk installation folder(in my case "C:\Program Files\Java\jdk1.7.0_51\bin" since the OS is 64 bit by default) first in the path variable.
cheers!
You can either set your path, or switch between JDK's in your IDE. Personally I use JetBrains IntelliJ IDEA and set the JDK via the IDE.
Setting via IDE:
From within a project:
click File -> Project Structure
Select Project under Project Settings
Select the Project SDK. If yours is not listed, click New -> JDK and browse for the JDK
From a new project:
Click File -> New Project
Select the Project SDK. If yours is not listed, click New -> JDK and browse for the JDK
If you want to set your path:
Follow the directions listed on the java.com site.
Keep in mind that your jar will run using whatever version of java is specified in your PATH.
If your PATH variable is not working, try cleaning up your path variable by removing unnecessary entries and rebooting. After reboot add Java to the path and reboot again.
If your still having issues, try executing using the absolute path of the java version your trying to use to ensure it works. If it does work, check your path variable against it to ensure it is accurate.
No, don't touch the registry. Each running Java application gets a single unique JVM instance. So a single java program cannot run some parts of the code in one JVM and other parts of the code in another JVM, as far as I know. You can, however, choose which JRE a particular java program runs in, so you can run the entire jpcap program in your 32 bit java version. Use a startup script (batch file) that points to the exact location of java.exe. When you run "java MyClassName" it's really just finding the java executable on the PATH environment variable, so by explicitly specifying your path, you get to choose your version. In other words, if you run a java program using "java MyClassName" (or if a program on your machine does so) it will use the java.exe application found on your PATH environment variable. Applications themselves can use whatever environment variables they choose: typically the ones used for java are JAVA_HOME and JRE_HOME. So you might want to try setting both of those and then trying to run your program again.
Set the path of your java in System Environment Variables PATH variable. Set it to be before any other path (even System 32 if java or javaw is present there).

Which of these is my JDK path, and which is JAVA_HOME?

I just installed Linux Mint and am trying to configure SpringSource Tool Suite which requires you to direct it to the system "JDK path". Although I haven't explicitly downloaded/installed Java on this system yet, when I ran the update manager I did see Open JDK get installed and found several directories under /usr/lib/jvm (see screenshot below). My questions are:
Spring Tool Suite requires you to specify a path to the JDK, and my question is, which one do I choose, and why?; and
How do I tell which of these is my JAVA_HOME?; and
What's the difference between the "JDK path" and JAVA_HOME?
Thanks in advance!
Edit:
echo $PATH --> /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games
java -version -> java version "1.7.0_03"
OpenJDK Runtime Environment (IcedTea7 2.1.1pre) (7~u3-2.1.1~pre1-1ubuntu3)
OpenJDK 64-Bit Server VM (build 22.0-b10, mixed mode)
which java -> /usr/bin/java
Spring Tool Suite requires you to specify a path to the JDK, and my question is, which one do I choose, and why?; and
This can only be answered according to the needs of your application. Do you need the stability of Java 6 or the features added in Java 7? Are there APIs you intend to use that are only available in Java 7 or are the Java 6 libraries sufficient? Do you have customers that require the use of one JVM over another? Only you can answer these questions.
How do I tell which of these is my JAVA_HOME?; and
You will want to make JAVA_HOME reference the specific JVM environment that you've decided on for your project. For example, if you decide on Java 6 you'll probably want to set your JAVA_HOME to /usr/lib/jvm/java-6-openjdk-amd64.
What's the difference between the "JDK path" and JAVA_HOME?
The JDK path refers to the Java Development Kit - which includes the java compiler, debugger and other tools associated with developing java programs. JAVA_HOME can reference either the JDK root or the JRE (Java Runtime Environment) root directory when running java programs; but you should, for development purposes, always have JAVA_HOME reference the JDK root.

Installing JDK and setting JAVA_HOME

What exactly is the purpose of JDK - running java programs or developing?
There seems to be too many packages that include java and jdk/sdk in their title. Which one should I install if I want to develop Java applications?
Who uses JAVA_HOME and what should I set it to be?
JDK is development environment. JVM is runtime environment.
You should download java installer from oracle web site or install it from repositiry manager
JAVA_HOME system variable usually is required for enterprice java software (application servers, DBs, development tools and so on). You should set JAVA_HOME variable to root directory of JDK or JVM(setting it to JDK always works, but maybe some software will run as expected with JAVA_HOME pointing to JRE). If you set JAVA_HOME right then $JAVA_HOME/bin/java -version command should pring version of java.
JRE means Java Runtime Environment. Most Java Software only needs a JRE to run.
JDK means Java Development Kit. A JDK allways includes a JRE. If you develop you most certainly allways use the JDK, since it contains source and docs of java's own Classes.
Also there is software (usually software for development) that uses tools of the JDK to run, e.g. this software needs the JDK as runtime environment (for example: ant).
JVM means Java Virtual Machine and is the program that runs your java program no matter if in JRE or JDK.
JAVA_HOME is the path to the JDK
JRE_HOME is the path to the JRE
JDK - Java Development Kit (in contrast to JRE - Java Runtime Environment).
If you don't know which version of Java you need, just get the latest (currently Java Platform (JDK) 7u5). Unless you already have a development tool, you might want the Netbeans bundle instead.
JAVA_HOME is used by some software that depends on Java - ant, which is a build tool, is one that comes to mind. It should be set to the directory which contains the bin directory, which in turn contains the java executables.

Categories