how to tell which java version cgywin use? - java

i have cgywin and java 7 installed on window 7, also updated the system environment variable to point to the right java version, however when I run commands in cgywin, I m not sure which java it use, here is the commands and results:
$ java -version
java version "1.7.0_09"
Java(TM) SE Runtime Environment (build 1.7.0_09-b05)
Java HotSpot(TM) Client VM (build 23.5-b02, mixed mode)
$ javac -version
javac 1.7.0_17
$ which java
/cygdrive/c/Windows/system32/java
export
declare -x JAVA_HOME="C:\\Program Files\\Java\\jdk1.7.0_17"
why all the versions are different?

Your PATH has two directories where it can find java one is under windows which picks a version installed (I imagine using the registry) and another is in your JDK. There is no javac in your Windows directory so it finds the on in your JDK.
To keep things simple I would just have one version of Java 7 JDK installed unless you really need multiple versions. I would change your path so it have the version of Java you want first, rather than near the end.

Related

Java appears to be outdated when using java -jar command

I have recently been writing some Java programs on my Windows computer. I have been trying to use java -jar to run compiled jars in order to see errors more clearly, but when I try to do this, I get the following error:
Error: A JNI error has occurred, please check your installation and try again
Exception in thread "main" java.lang.UnsupportedClassVersionError: com/company/app/GUI has been compiled by a more recent version of the Java Runtime (class file version 57.0), this version of the Java Runtime only recognizes class file versions up to 52.0
This obviously means that my java version is outdated. However, when I took a look at the Java Updater, it shows that I am running the latest version.
Running java -version shows this:
java version "1.8.0_261"
Java(TM) SE Runtime Environment (build 1.8.0_261-b12)
Java HotSpot(TM) Client VM (build 25.261-b12, mixed mode)
So why is Command Prompt using an older version, and how do I change it?
Thank you for your help.
This error clearly indicates that you try to run a .jar file built with JDK 13 (major version 57) on a JRE/JDK 8 (major version 52) which is provided in PATH setting and thus invoked when running java -jar / java -version commands.
If you have JDK 13 installed on your machine, you need to check environment variable PATH and/or JAVA_HOME:
C:\Users\hp1>echo %JAVA_HOME%
C:\Java\jdk-13.0.2
C:\Users\hp1>echo %PATH%
C:\Windows\system32;C:\Java\jdk-13.0.2\bin
C:\Users\hp1>java -version
openjdk version "13.0.2" 2020-01-14
OpenJDK Runtime Environment (build 13.0.2+8)
OpenJDK 64-Bit Server VM (build 13.0.2+8, mixed mode, sharing)
If PATH refers JRE/JDK 8, you may create another variable and update PATH (copy non-java paths):
>set JAVA_13=**path_to_your_jdk_13**
>set PATH=C:\Windows\system32;%JAVA_13%\bin
If you do not have JDK 13 on your machine, to resolve this issue you should rebuild the .jar file to make it compatible with JDK 8 providing that the code is not using any features from the newer versions.

JDK Version inconsistent

My os is Ubuntu 16.04 LTS.
$ cat /etc/profile # part content of this file
JAVA_HOME=/usr/local/java/jdk1.8.0_101
JRE_HOME=$JAVA_HOME/jre
PATH=$PATH:$JAVA_HOME/bin:$JRE_HOME/bin
export JAVA_HOME
export JRE_HOME
export PATH
$ echo $JAVA_HOME
/usr/local/java/jdk1.8.0_101
$ java -version
java version "1.7.0_79"
Java(TM) SE Runtime Environment (build 1.7.0_79-b15)
Java HotSpot(TM) 64-Bit Server VM (build 24.79-b02, mixed mode)
$ ls /usr/local/java/
jdk1.7.0_79/ jdk1.8.0_101/
Why is my java version still jdk7?
Ubuntu has ability to work with multiple java versions. In your case, it just means that it has both JDK 7 and JDK 8 installed in your system but is using JDK 7.
To switch from one java version to another, you can use sudo update-alternatives --config java. This will list all JDKs installed in your system, just enter the number corresponding to JDK 8 and it should switch to JDK 8.
Run which java in your command shell. That will tell you where the java command you are running is coming from.
I suspect that is will tell you "/usr/bin/java" ... because this:
PATH=$PATH:$JAVA_HOME/bin:$JRE_HOME/bin
puts your new bin directories onto the end of the search path, not the beginning.
However, the better way to do this would be to use alternatives as suggested by another Answer. (With a custom installation in "/usr/local" this will require some fiddling around to get "alternatives" to understand the alternative.)

Error: Could not find or load main class version

I have downloaded Java on mac mavericks system. But when I type on terminal to see the Java version using command:
java version
I get the following error
Could not find or load main class version
I went to oracle website and tried a check to see if Java is installed on my system,which confirmed the Java7 is installed on the system. But why can I not see version in terminal?
Try
java -version
Without the minus sign it is trying to load a program called version.
java –version
will cause the same error (copied from webpage) as "–" is an em dash
java -version is the command to check java version
Download latest Java SE from Oracle site and install it. Then reopen cmd and check java version using java -version.
Set the Java path to bin and check it's version also. Both java and javac should be on same version.
In my case:
C:\Users\darshan>java -version
java version "1.8.0_121"
Java(TM) SE Runtime Environment (build 1.8.0_121-b13)
Java HotSpot(TM) 64-Bit Server VM (build 25.121-b13, mixed mode)
C:\Users\darshan>javac -version
javac 1.8.0_121

Cygwin runs wrong version of java

I have this problem with Cygwin. I have Java 1.6 and 1.7 installed. I want to use maven 3.0.4 with Java 1.7 but I don't want to uninstall Java 1.6. My JAVA_HOME looks like:
C:\Program Files\Java\jdk1.7.0_09
when I run
java -version
in Cygwin I get:
java version "1.6.0_29"
Java(TM) SE Runtime Environment (build 1.6.0_29-b11)
Java HotSpot(TM) 64-Bit Server VM (build 20.4-b02, mixed mode)
when I run the same command in cmd.exe I get:
java version "1.7.0_09"
Java(TM) SE Runtime Environment (build 1.7.0_09-b05)
Java HotSpot(TM) 64-Bit Server VM (build 23.5-b02, mixed mode)
Does anyone knows how to solve this?
There are two separate questions here.
The first question is why java -version is finding different Java installations on Cygwin and the classical Windows command interpreter.
The answer is most likely that your Cygwin and Windows environments have different values for $PATH and %PATH% respectively. If you want java -version on Cygwin to run Java 7, you need to make sure that the Cygwin $PATH includes the Java 7 bin directory ... in the appropriate syntax.
The second question is how to get the mvn command under Cygwin to use Java 7.
The answer is not so straight-forward:
Setting $PATH might solve your problem.
According to the Maven installation documentation, the mvn wrapper scripts should use the $JAVA_HOME environment variable in your (Cygwin) shell to decide on which Java to use.
The way to find out what is really going on is to look at the wrapper scripts and see what they are actually doing. And if reading the scripts is too hard, try "hacking" the scripts to include set -vx. That will tell you what lines of the script are being read, and what commands are being executed.
Finally, the POM file can influence the source and target levels for your build ... independently of the JVM that runs the build.

Why this version of java?

On my Red Hat server, java -version outputs;
$ java -version
java version "1.6.0_27"
Java(TM) SE Runtime Environment (build 1.6.0_27-b07)
Java HotSpot(TM) 64-Bit Server VM (build 20.2-b06, mixed mode)
$
However, neither PATH, nor JAVA_HOME environment variables are set. Likewise, JAVA_HOME is not set on ~/.bash* files.
Why and how my server uses this version of Java while two other versions of Java are installed as well?
Firstly, JAVA_HOME is not involved in this. (JAVA_HOME is used conventionally by wrapper scripts, etc for applications that use Java ... but not by any of the Java executables themselves.)
Second, you are probably running java via a symlink managed by the alternatives program. (RHEL and similar distros use this utility to allow you to select different versions of utilities installed on the same system.)
Either way, running the following will help you figure out what is going on.
$ ls -l `which java`
(Then following the chain of symlinks until you get to the actual executable.)
Which executable runs depends on PATH variable. Double check it. It can't run if not set it is impossible unless you have some strange Linux config.
If Java executable is in current directory, it would run by ./java. Since it runs with just java it is somewhere in the PATH.

Categories