Cygwin runs wrong version of java - 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.

Related

Can't run java applications

I have trouble when trying to open my jar files with the following command:
java -jar software.jar
They load until a certain point and then nothing happens.
I deinstalled default-jdk and OpenJDK with synaptic and re-installed it with apt-get, but nothing changed. I had a similar problem one month ago and solved it (apparently only temporarly) by deleting the package gcj-5-jre-lib (5.4.0-6ubuntu1~16.04.4) (Java runtime library for use with gcj (jar files))
The following outputs will help:
java -version
openjdk version "1.8.0_131"
OpenJDK Runtime Environment (build 1.8.0_131-8u131-b11-2ubuntu1.16.04.3-b11)
OpenJDK 64-Bit Server VM (build 25.131-b11, mixed mode)
javac -version
1.8.0_131
My variables $JAVA_HOME and $JRE_HOME were correctly added to my $PATH.
I'm on Ubuntu 16.04
EDIT
Here is an example of what I get when I try to start PhyDE.jar
java -jar PhyDE.jar
Welcome to PhyDE 0.9971!
Reading preferences.
Creating PhyDE-Application window
Checking for updates...
You are using the latest version of PhyDE
Done with checking for updates
When checking the core use with the command top I see that the process is active, but the user interface does not open as it usually does. I am located in the folder where the jar file is, and same happens with other jar files.

Eclipse (and STS) using wrong JDK to start up

I have several versions of Java on my system (OS X El Capitan, version 10.11.6). My current JAVA_HOME variable is set to a Java 8 JDK, and the bin directory is in my path:
$ 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)
Nevertheless, whenever I try to start Eclipse or STS, I get a crash, and inside the .metadata/.log file in the workspace it includes the line:
java.version=9-ea
I can edit the eclipse.ini file and add a -vm flag to get it to start with the proper JVM, but what mystifies me is where Eclipse is getting the wrong information in the first place. Isn't setting JAVA_HOME enough? Where else might it be finding Java?
I think the native launcher of Eclipse uses /usr/libexec/java_home to determine the current JVM to use and I am not aware of a way to change that command to point to a different VM as a default.

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.)

how to tell which java version cgywin use?

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.

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