Ant not using my JAVA_HOME to find tools.jar - java

I realize that this question is going to initially sound like a duplicate of some other questions, but none of the other questions I've looked at fixed my issue.
The short story is that I have a JRE and a JDK installed at different locations. I set JAVA_HOME to point to the JDK, yet ant seems to still search for tools.jar in the JRE location. I'm not sure if this has more to do with my ant installation/configuration, or my JDK installation/configuration.
Here's the long story:
Environment
I am running on Red Hat Enterprise Linux Server release 7.5 (Maipo) on an account that does not have sudo privileges to anything.
My JDK install
The machine already has the JRE installed at /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.171-7.b10.el7.x86_64/jre/bin/java. I am not allowed to modify that install, but I can create my own java installation in a separate location.
The security requirements on my network are very stringent and I am only allowed to put specific pieces of software on the machine. It is possible to ask permission for other pieces of software, but I have to go through an approval process that is a huge pain. I have obtained permission to put JDK 1.8 from openJDK on the machine.
Since openJDK does not release tar balls of the whole installation and since I don't have sudo permissions I attempted to install the JDK by downloading java-1.8.0-openjdk-1.8.0.172-13.b11.fc29.x86_64.rpm and java-1.8.0-openjdk-devel-1.8.0.172-13.b11.fc29.x86_64.rpm to /local/apps/openJDK8 and running (from /local/apps/openJDK8)
rpm2cpio < java-1.8.0-openjdk-1.8.0.172-13.b11.fc29.x86_64.rpm | cpio -i --make-directories
rpm2cpio < java-1.8.0-openjdk-devel-1.8.0.172-13.b11.fc29.x86_64.rpm | cpio -i --make-directories
For some reason this doesn't seem to do a complete JDK install (I think I must be missing some dependencies). I discovered that libjava.so, libjvm.so, jvm.cfg, and the server folder were missing, so I just created some links to the JRE install that had them (run from local/apps/openJDK8):
ln -s /usr/lib/jvm/jre-1.8.0-openjdk-1.8.0.171-7.b10.el7.x86_64/lib/amd64/jvm.cfg usr/lib/jvm/java-1.8.0-openjdk-1.8.0.172-13.b11.fc29.x86_64/lib/amd64/jvm.cfg
ln -s /usr/lib/jvm/jre-1.8.0-openjdk-1.8.0.171-7.b10.el7.x86_64/lib/amd64/libjava.so usr/lib/jvm/java-1.8.0-openjdk-1.8.0.172-13.b11.fc29.x86_64/lib/amd64/libjava.so
ln -s /usr/lib/jvm/jre-1.8.0-openjdk-1.8.0.171-7.b10.el7.x86_64/lib/amd64/libjvm.so usr/lib/jvm/java-1.8.0-openjdk-1.8.0.172-13.b11.fc29.x86_64/lib/amd64/libjvm.so
ln -s /usr/lib/jvm/jre-1.8.0-openjdk-1.8.0.171-7.b10.el7.x86_64/lib/amd64/server usr/lib/jvm/java-1.8.0-openjdk-1.8.0.172-13.b11.fc29.x86_64/lib/amd64/server
That step makes me a little nervous, so it could be part of my problem. At any rate, javac and java seem to work fine.
ANT INSTALL
For my ant installation, I just downloaded a tar ball and extracted it to /local/apps/ant and then did:
export JAVA_HOME=/local/apps/openJDK8/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.172-13.b11.fc29.x86_64
export ANT_HOME=/local/apps/ant/apache-ant-1.10.4
I didn't bother updating the path because for now I'm just using fully qualified paths for everything. My ultimate goal is to have Jenkins running all of this anyway and it just uses fully qualified paths.
Results
Now if I try to run ant, I get:
> /local/apps/ant/apache-ant-1.10.4/bin/ant
Unable to locate tools.jar. Expected to find it in /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.171-7.b10.el7.x86_64/lib/tools.jar
Buildfile: build.xml does not exist!
Build failed
The build.xml does not exist part is of course expected, but the inability to locate tools.jar needs to be resolved. The strangest part for me is that according to the error message, it is actually looking for it in the JRE installation location.
Since ant itself is just a shell script, I modified it to print out a little more information by adding the following lines to the end of the file so that it looks like:
echo Running $ant_exec_command $ant_exec_args
echo JAVA_HOME = $JAVA_HOME
echo JAVACMD = $JAVACMD
echo CLASSPATH = $CLASSPATH
echo LOCALCLASSPATH = $LOCALCLASSPATH
echo ANT_HOME = $ANT_HOME
echo ANT_LIB = $ANT_LIB
eval "$ant_exec_command $ant_exec_args"
Now the output is:
$ /local/apps/ant/apache-ant-1.10.4/bin/ant
JAVA_HOME is /local/apps/openJDK8/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.172-13.b11.fc29.x86_64
Running exec "$JAVACMD" -classpath "$LOCALCLASSPATH" -Dant.home="$ANT_HOME" -Dant.library.dir="$ANT_LIB" org.apache.tools.ant.launch.Launcher -cp "$CLASSPATH"
JAVA_HOME = /local/apps/openJDK8/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.172-13.b11.fc29.x86_64
JAVACMD = /local/apps/openJDK8/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.172-13.b11.fc29.x86_64/bin/java
CLASSPATH =
LOCALCLASSPATH = /local/apps/ant/apache-ant-1.10.4/lib/ant-launcher.jar
ANT_HOME = /local/apps/ant/apache-ant-1.10.4
ANT_LIB = /local/apps/ant/apache-ant-1.10.4/lib
Unable to locate tools.jar. Expected to find it in /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.171-7.b10.el7.x86_64/lib/tools.jar
Buildfile: build.xml does not exist!
Build failed
So JAVA_HOME is clearly set. I can even verify further that ant is actually using the JAVA_HOME value that I set for at least some things because if I remove all of those links I created in my JDK install, it will complain that it can't find libjava.so.
So ant is clearly using my JDK install for at least a little bit, but still looking in the old JRE install for tools.jar.
Any ideas on what is going on?

Figured it out :)
This fix still feels a little more like a workaround than an actual fix, so if someone else has a better solution, I'd be interested to hear it.
I've browsed through the ant source code a bit and discovered that ant has a --launchdiag property which is helpful. Running with that gave me:
/local/apps/openJDK8/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.172-13.b11.fc29.x86_64/bin/java -classpath /local/apps/ant/apache-ant-1.10.4/lib/ant-launcher.jar -Dant.home=/local/apps/ant/apache-ant-1.10.4 -Dant.library.dir=/local/apps/ant/apache-ant-1.10.4/lib org.apache.tools.ant.launch.Launcher --launchdiag
Launcher JAR= "/local/apps/ant/apache-ant-1.10.4/lib/ant-launcher.jar"
Launcher JAR directory= "/local/apps/ant/apache-ant-1.10.4/lib"
java.home= "/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.171-7.b10.el7.x86_64/jre"
Unable to locate tools.jar. Expected to find it in /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.171-7.b10.el7.x86_64/lib/tools.jar
tools.jar= "null"
Setting "java.class.path" to "/local/apps/ant/apache-ant-1.10.4/lib/ant-launcher.jar:/local/apps/ant/apache-ant-1.10.4/lib/ant-antlr.jar:/local/apps/ant/apache-ant-1.10.4/lib/ant-swing.jar:/local/apps/ant/apache-ant-1.10.4/lib/ant-testutil.jar:/local/apps/ant/apache-ant-1.10.4/lib/ant-apache-xalan2.jar:/local/apps/ant/apache-ant-1.10.4/lib/ant-junitlauncher.jar:/local/apps/ant/apache-ant-1.10.4/lib/ant-xz.jar:/local/apps/ant/apache-ant-1.10.4/lib/ant-junit.jar:/local/apps/ant/apache-ant-1.10.4/lib/ant.jar:/local/apps/ant/apache-ant-1.10.4/lib/ant-apache-log4j.jar:/local/apps/ant/apache-ant-1.10.4/lib/ant-javamail.jar:/local/apps/ant/apache-ant-1.10.4/lib/ant-apache-bsf.jar:/local/apps/ant/apache-ant-1.10.4/lib/ant-apache-resolver.jar:/local/apps/ant/apache-ant-1.10.4/lib/ant-apache-oro.jar:/local/apps/ant/apache-ant-1.10.4/lib/ant-apache-bcel.jar:/local/apps/ant/apache-ant-1.10.4/lib/ant-jai.jar:/local/apps/ant/apache-ant-1.10.4/lib/ant-jsch.jar:/local/apps/ant/apache-ant-1.10.4/lib/ant-jmf.jar:/local/apps/ant/apache-ant-1.10.4/lib/ant-commons-logging.jar:/local/apps/ant/apache-ant-1.10.4/lib/ant-launcher.jar:/local/apps/ant/apache-ant-1.10.4/lib/ant-jdepend.jar:/local/apps/ant/apache-ant-1.10.4/lib/ant-commons-net.jar:/local/apps/ant/apache-ant-1.10.4/lib/ant-netrexx.jar:/local/apps/ant/apache-ant-1.10.4/lib/ant-junit4.jar:/local/apps/ant/apache-ant-1.10.4/lib/ant-apache-regexp.jar"
Buildfile: build.xml does not exist!
Build failed
In the code I found that that java.home property comes from System.getProperty("java.home") which apparently is not the same thing as JAVA_HOME
By adding -Djava.home=$JAVA_HOME I was able to get it working

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!

On Windows 8.1 "javac -version" works but Cordova doesn't recognize it

I'm trying to set up Cordova. When I run cordova build android I receive the following error:
(node:6816) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): Failed to run "javac -version", make sure that you have a JDK installed.
You can get it from: http://www.oracle.com/technetwork/java/javase/downloads.
Your JAVA_HOME is invalid: C:\Program Files\Java\jdk1.8.0_161;C:\Program Files\Java\jdk1.8.0_161\bin
However I can run javac -version just fine.
C:\>javac -version
javac 1.8.0_161
I've tried to set JAVA_HOME in various ways: pointing to the root; pointing to the bin folder; set it as a User variable and also a System one; add %JAVA_HOME%\bin to Path (both for User and System) and so on.
I've checked several articles about this issue, and they say that if I'm able to use javac -version then that's supposed to indicate that my environmental variables are set correctly. Even if that's the case, Cordova still doesn't seem to recognize it.
Update
Following Stephen C's instructions, I've reset my variables, yet my error still persists. As you can see in the image I can call javac just fine, and both JAVA_HOME and Path seem to be set in the right way. (Note that actually it is %JAVA_HOME%\bin and not C:\Program Files\Java\jdk1.8.0_161\bin in the Path.)
I think you are confusing JAVA_HOME, PATH and CLASSPATH
The JAVA_HOME variable should be set to the name of a single directory. Not multiple directories with separators. The JAVA_HOME directory is the top directory of your JDK or JRE installation. Not the "bin" directory.
The PATH and CLASSPATH variables consist of a list of paths. For PATH the paths are pathnames for directories. For CLASSPATH the paths are pathnames for directories or JAR / ZIP files (or a particular kind of wildcard).
The PATH is where the shell searches for commands if you use a command name that is a simple name; e.g. java or javac.
`
The CLASSPATH is one of the ways that you can tell Java tools to look cor compiled classes to load, compile against, etcetera.
Your error message says this:
Your JAVA_HOME is invalid:
C:\Program Files\Java\jdk1.8.0_161;C:\Program Files\Java\jdk1.8.0_161\bin
This is wrong for two reasons:
JAVA_HOME should not be a path
C:\Program Files\Java\jdk1.8.0_161\bin is not a Java home. The Java home is C:\Program Files\Java\jdk1.8.0_161 .... probably.
The other thing you may have gotten wrong is that changes to environment variables do not necessarily propagate. For example, if you start a shell and then change an environment variable via the Windows GUI, the change may not propagate to the shell. You may need to exit the shell and restart it to pick up the new value.
If you are unsure, you can run (for example) echo %PATH% to see the current value of PATH in the current shell or script.
UPDATE
According to http://cordova.apache.org/docs/en/7.x/guide/platforms/android/index.html, you need to:
install Android Studio
set the ANDROID_HOME environment variable to the location of your Android SDK installation.
It is also recommended that you add the Android SDK's tools, tools/bin, and platform-tools directories to your PATH.
Remove java.dll from C:\Windows\System32 (if any).
I'm not sure but It may work correctly.

Maven Java Home Path: \bin, or not to \bin

So I find myself kind of stuck in between.
I set JAVA_HOME to C:\Program Files\Java\jdk1.8.0_121 then Maven works but
java -version
shows 'java' is not recognized as an internal or external command,
operable program or batch file.
If I set JAVA_HOME to C:\Program Files\Java\jdk1.8.0_121\bin, Maven doesn't work but at least I get java -version right.
I'm pretty sure I'm doing something fundamentally wrong but I can't put my finger on it, obviously :(
java -version will look at your $PATH, so normally we need to add %JAVA_HOME%/bin to $PATH for any ad-hoc command. For maven, internally it checks $JAVA_HOME/bin. Hope it helps.

Modifying JDK Path for Oracle SQL Developer in Ubuntu

I have Oracle SQL Developer installed at the following location:
/opt/sqldeveloper
Every time I run it using the command ./sqldeveloper, I get the following error from the GUI:
You are attempting to run with Java 1.6.0_31. Running this product is
supported with a minimum Java version of 1.7.0_51 and less than 1.8
I updated the sqldeveloper.conf file with the following paths:
/usr/java/jdk1.8.0_05
and
/usr/java/jdk1.7.0_55
And despite all that, same error again.
I know I am a bit late with the response, but I had exactly the same issue until this morning.
What I did is changed:
~/.sqldeveloper/4.0.0/product.conf
FYI:
How I figure it out:
1. Go to OracleSQLDeveloper -> Help(menu) -> Properties
2. Find 'java.home'
(For some reason it was pointing to /usr/lib/jdk1.7.0 (ver 1.7.45), even I've changed sqldeveloper.conf)
3. Since I didn't recognized that specific version, I just ran:
sudo "find / .... -name '*.conf' -exec grep 'jdk1.7.0' ..."
goodluck
Configure Path to Java
SQL-Developer needs to know how to find your Java Developer Kit:
Note: You need to specify /usr/lib/jvm/java-6-openjdk-i386 on 32-bit OS installations.
Terminal
cd $HOME
mkdir -p .sqldeveloper
cd $HOME/.sqldeveloper
echo "/usr/lib/jvm/java-6-openjdk" > jdk

Unable to run threaded java jar file on Mac OS X 10.6.8 with Java 1.7.0.45 using pacifist

I have installed Java 1.7.0_45 on Mac OS X 10.6.8 using Pacifist [http://www.charlessoft.com/] however I am unable to run a jar file which I have downloaded. The jar file is a threaded application.
The error message I am getting is:
java -jar context.jar
2013-10-31 14:14:41.898 java[330:a07] *** NSInvocation: warning: object 0x109356390 of class 'ThreadUtilities' does not implement methodSignatureForSelector: -- trouble ahead
2013-10-31 14:14:41.900 java[330:a07] *** NSInvocation: warning: object 0x109356390 of class 'ThreadUtilities' does not implement doesNotRecognizeSelector: -- abort
Trace/BPT trap
Is there anyway I can run the jar. I have set the JAVA_HOME path properly and java -version is showing 1.7.0_45 as the version.
The same application works properly on Windows Java 1.7.0_45 and also on Linux Java 1.7.
The web search for the solution and the given keywords return very few results and none of them have any specific solution in it. I am new to mac so I am not fully able to understand the issue.
Alternatively, is there anyway I can run Java from folder in Mac like I can do in windows and Linux by just extracting the Java contents and changing the JAVA_HOME. If that is possible then I should be able to run my JAR.
I had kind of same problem while installing an application through jar file, my Java was not detected by the jar application installer,
Over here I see that one of the method is not accessible, could be a same problem. But I am not very sure of it.
make sure you have rt.jar in your JAVA_HOME/jre/lib/ folder
In case you don't have then you are required to have it through the process of creation of symbolic link.
In the command below replace your_java_version with proper version matching your requirement.
sudo mkdir -p /System/Library/Frameworks/JavaVM.framework/Versions/<your_java_version>/Home/jre/lib
Go into the directory:
cd /System/Library/Frameworks/JavaVM.framework/Versions/<your_java_version>/Home/jre/lib/
Create symbolic link :
sudo ln -s ../../../Classes/classes.jar rt.jar
Hope this solves your problem.
I get the exact same problem with MacOS 10.6.8 and JDK 7. In order to run the jar I had to use the System JRE which is 1.6.x (In my case I wanted to install Squirrel
/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home/bin/java -jar >squirrel-sql-3.6-MACOSX-install.jar

Categories