Gradle does not find tools.jar
Asked Answered
S

31

196

I am using javadoc doclets with gradle, so I need to use the package tools.jar, which is in the lib folder from the jdk (1.6.0_26 in my case).

The point is that gradle does not take it automatically, so I was adding that tools package to my libs folder, and then adding it to dependencies.gradle .

Now I want to take it directly from my JDK home into my dependencies.gradle. Is there a way to do that? I have tried the next in my dependencies.gradle:

compile files("${System.properties['java.home']}/lib/tools.jar")

But it does not find it while compiling.

Safar answered 5/7, 2012 at 13:19 Comment(1)
It is worth mentioning that, as pre-cursor to solving this and as I ran into when I was getting this, you'll need to make sure you've actually got a JDK installed :P And, on some systems, you'll need to install the devel JDK package to get tools.jarPeppermint
S
82

Found it. System property 'java.home' is not JAVA_HOME environment variable. JAVA_HOME points to the JDK, while java.home points to the JRE. See that page for more info.

Soo... My problem was that my startpoint was the jre folder (C:\jdk1.6.0_26\jre) and not the jdk folder (C:\jdk1.6.0_26) as I thought(tools.jar is on the C:\jdk1.6.0_26\lib folder ). The compile line in dependencies.gradle should be:

compile files("${System.properties['java.home']}/../lib/tools.jar")
Safar answered 5/7, 2012 at 13:47 Comment(2)
so, how to set java.home in idea with gradle ?Transposal
Any help for mac users?Refract
S
163

I had this problem when I was trying to run commands through CLI.

It was a problem with system looking at the JRE folder i.e. D:\Program Files\Java\jre8\bin. If we look in there, there is no Tools.jar, hence the error.

You need to find where the JDK is, in my case: D:\Program Files\Java\jdk1.8.0_11, and if you look in the lib directory, you will see Tools.jar.

What I did I created a new environment variable JAVA_HOME: enter image description here

And then you need to edit your PATH variable to include JAVA_HOME, i.e. %JAVA_HOME%/bin; enter image description here

Re-open command prompt and should run.

Satem answered 25/2, 2016 at 9:25 Comment(5)
the tool.jar is in %JAVA_HOME%\lib; not in bin folder.Scathe
does this mean that we should be adding both %JAVA_HOME%/bin and %JAVA_HOME%/lib to the Path variable then?Titicaca
Even though tools.jar is indeed in \lib, adding only \bin to path as in solution worked for me but don't ask why.Bander
Usually a restart/logout is required for changes to have an effect.Knoxville
I had to remove the PATH entry C:\ProgramData\Oracle\Java\javapath and replace it with C:\ProgramData\Oracle\Java\jdk1.8.xxx it was defaulting to the sister JRE directoryJemie
S
82

Found it. System property 'java.home' is not JAVA_HOME environment variable. JAVA_HOME points to the JDK, while java.home points to the JRE. See that page for more info.

Soo... My problem was that my startpoint was the jre folder (C:\jdk1.6.0_26\jre) and not the jdk folder (C:\jdk1.6.0_26) as I thought(tools.jar is on the C:\jdk1.6.0_26\lib folder ). The compile line in dependencies.gradle should be:

compile files("${System.properties['java.home']}/../lib/tools.jar")
Safar answered 5/7, 2012 at 13:47 Comment(2)
so, how to set java.home in idea with gradle ?Transposal
Any help for mac users?Refract
P
74

I got the same error using Eclipse trying to execute a Gradle Task. Every time I run a command (i.e. war) the process threw an exception like:

Could not find tools.jar. Please check that C:\Program Files\Java\Jre8" is a valid JDK install.

I tried the solution listed in this post but none of them solved this issue. Here my solution :

  1. Go to the "Gradle Task" view
  2. Right Click on the task you want to execute
  3. Select Open Gradle Run Configuration
  4. In the tab "Java Home" select your local JDK repository then click OK

Run again, Enjoy!

Pasia answered 19/8, 2016 at 13:21 Comment(3)
I've found this answer after hours of searching, and this finally worked. Thank you so much!Nath
I cant find Open Gradle Run and Java Home. Can you explain further?Burnette
Being new to this, how would one "Go to the "Gradle Task" view"? I don't see anything Gradle under the View menu, nor anything labeled anything like that?Betray
G
62

I just added a gradle.properties file with the following content:

org.gradle.java.home=C:\\Program Files\\Java\\jdk1.8.0_45
Gosh answered 29/6, 2016 at 12:30 Comment(1)
On linux, you can also edit ~/.gradle/gradle.properties file, adding org.gradle.java.home=/usr/lib/jvm/java. Works for me on Fedora 24.Vernievernier
K
44

I had a similar case using Ubuntu. The machine had only the JRE installed. So, I just executed the command below to install the JDK.

sudo apt install openjdk-8-jdk

Kippie answered 27/7, 2017 at 15:47 Comment(3)
Yes, installing JDK instead of JRE also solved the problem for me.Ducky
I was getting the same issue even though i had java installed and i saw a folder for openjdk. Later only i realized that the folder had only JRE installed. Not JDK.Squalid
So I had the jdk installed, my JAVA_HOME set -- I ran this exact command and verified with java -version (Debian). Then after seeing this comment, for kicks I re-ran it, and then my build worked fine. I'm not sure what happened, it must have missed some packages on the initial install. Thank you though!Southerland
H
38

In macOS Big Sur

The issue happens because of the environment variable JAVA_HOME is not correctly set in macOS Big Sur.

Step 1 - Confirm that you have issue with JAVA_HOME by printing its value in the terminal. You will most likely get an empty string.

echo $JAVA_HOME

Step 2 - Find the correct path on your machine

/usr/libexec/java_home -V

Copy that path associated with "Java SE 8" which usually looks like /Library/Java/JavaVirtualMachines/jdk1.8.0_202.jdk/Contents/Home

Step 3 - Edit .zshenv using nano

nano ~/.zshenv

Step 4 - Add the path from step 2 to the file as follows

export JAVA_HOME=YOUR_JAVA_PATH

example:

export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_202.jdk/Contents/Home

Step 5 - Source the updated .zshenv file to activate the environment variable

source ~/.zshenv

Step 6 - Print to confirm the path

echo $JAVA_HOME
Humanitarianism answered 20/5, 2021 at 10:42 Comment(1)
These 6 steps WORKED! after 3 days of nada -> r3 Corda docs & various tutorials say to modify ~/.zshrc which does not work on my Mac.Unsuspected
W
27

I was struggling as well for this Solution. Found a better way to it with Gradle as described here. We can get the JVM/JDK information from Gradle itself.

dependencies {
   runtime files(org.gradle.internal.jvm.Jvm.current().toolsJar)
}

So simple.

Wellheeled answered 11/3, 2016 at 11:44 Comment(4)
This works for me if I use compile instead of runtimeQuirinal
I like this solution. It's nice and portable.Knoll
This solution does not work with Gradle 4.x. I get a NullPointerException.Fredi
I can imagine it returning null when there is no tools.jar available with latest JDK. See github.com/gradle/gradle/blob/master/subprojects/base-services/…Wellheeled
L
22

In CentOS7 the development package will install tools.jar. The file is not present in java-1.8.0-openjdk

sudo yum install java-1.8.0-openjdk-devel

Landbert answered 19/3, 2018 at 21:19 Comment(3)
Thanks Richard! It really helped me! I was struggling running "react-native run-android" and getting the error: * What went wrong: Execution failed for task ':app:compileDebugJavaWithJavac'. > Could not find tools.jar. Please check that /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.161-5.b14.fc27.x86_64/jre contains a valid JDK installation. Now it's working! Thanks again!Dupion
Thanks Richard..It worked for me as well. Basically it was a JAVA_HOME path issue which tried to find tools.jar in the desired java jdk path.Hornbill
I am using Amazon Linux AMI and this solution worked perfectly.Harbert
L
19

Add this to gradle.properties:

org.gradle.java.home=C:\Program Files\Java\jdk1.8.0_91

Limoli answered 8/8, 2016 at 9:25 Comment(1)
Don't forget to use double back slashes. For example: org.gradle.java.home=C:\\Program Files\\Java\\jdk1.8.0_144\ .Hewe
S
13

My solution on Mac:

add this line to gradle.properties:

org.gradle.java.home=/Library/Java/JavaVirtualMachines/jdk1.8.0_271.jdk/Contents/Home

not this one:

org.gradle.java.home=/Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home

you can open the last home directory and will find that there is no lib/tools.jar file existence, so change the path to JavaVirtualMachines/jdk1.8.0_271.jdk and it works for me.

By the way, in the terminal, I echo the $JAVA_HOME and it gets the first path, not the second one, I think this is why my Gradle cannot work properly.

Solleret answered 8/12, 2020 at 6:17 Comment(1)
Thanks! Adding the $JAVA_HOME environment to my .zshrc and adding it to gradle.properties did it for me :DUngley
W
12

With Centos 7, I have found that only JDK has tools.jar, while JRE has not. I have installed the Java 8 JRE(yum install java-1.8.0-openjdk), but not the JDK(yum install java-1.8.0-openjdk-devel).

Installing the latter solves the problem. Also, remember to set JAVA_HOME.

Wilden answered 16/8, 2018 at 14:46 Comment(0)
E
11

It may be two years too late, but I ran into the same problem recently and this is the solution I ended up with after finding this post:

import javax.tools.ToolProvider

dependencies {
  compile (
    files(((URLClassLoader) ToolProvider.getSystemToolClassLoader()).getURLs()),
    ...
  }
}

It should work if java.home points to a directory that's not under the JDK directory and even on Mac OS where you'd have classes.jar instead of tools.jar.

Elul answered 2/3, 2015 at 8:31 Comment(1)
Cannot invoke method getURLs() on null objectKearse
O
10

On windows 10, I encounter the same problem and this how I fixed the issue;

  1. Access Advance System Settings>Environment Variables>System Variables
  2. Select PATH overwrite the default C:\ProgramData\Oracle\Java\javapath
  3. With your own jdk installation that is JAVA_HOME=C:\Program Files\Java\jdk1.8.0_162
Omniscience answered 13/2, 2018 at 17:56 Comment(1)
this should be the accepted answer for Windows :) --thanksCopywriter
D
9

On my system (Win 10, JRE 1.8.0, Android Studio 3.1.2, Gradle 4.1) there is no tools.jar in the JRE directory (C:\Program Files\Java\jre1.8.0_171).

However, I found it in C:\Program Files\Android\Android Studio\jre\lib and tried setting JAVA_HOME=C:\Program Files\Android\Android Studio\jre

That works (for me)!

Doublereed answered 22/5, 2018 at 10:4 Comment(2)
Same issue, fixed it for me, also Win 10/Android Studio. Also, none of the above solutions actually seems to download a "Tools.jar" ... just doesn't come with the jre)Specialism
Worked for me too. I created a gradle.properties in the project root containing org.gradle.java.home=E:\\Android Studio\\jreVargas
D
5

What solved it for me was the following:

  1. found tools.jar in C:\Program Files\Android\Android Studio\jre\lib
  2. copy paste to C:\Program Files (x86)\Java\jre1.8.0_271\lib
  3. ran the code again and it worked.
Drafty answered 2/11, 2020 at 11:36 Comment(2)
Hi there! Thanks for the answer :) I am sure that worked.... However, please check the other answers in this question. It will help you in the future to understand the differences between the Java Runtime Environment (JRE) and the Java Development Kit (JDK).Safar
It works for me!! So this comment helps me to solve my problem as well. Thanks bro!Takeshi
F
4

If you use terminal to build and you have this error you can point to jdk bundled with android studio in your gradle.properties file:

org.gradle.java.home=/usr/local/android-studio/jre
Faggoting answered 21/6, 2018 at 12:21 Comment(0)
L
3

Linux

Open /etc/environment in any text editor like nano or gedit and add the following line:

JAVA_HOME="/usr/lib/jvm/open-jdk"

Windows

  1. Start the System Control Panel applet (Start - Settings - Control Panel - System).
  2. Select the Advanced tab.
  3. Click the Environment Variables button.
  4. Under System Variables, click add button, then past the following lines:

    in Variable Name : JAVA_HOME

    in Variable Value : C:\Program Files\Java\jdk1.x.x_xxx

    where x.x_xxx jdk version you can get your jdk version from here C:\Program Files\Java

  5. Under System Variables, select Path, then click Edit,then click new button then past the following line:

    %JAVA_HOME%/bin;

Ledaledah answered 13/11, 2017 at 11:35 Comment(1)
Paths may vary slightly. In Fedora this worked for me with JAVA_HOME="/usr/lib/jvm/java-openjdk"Erosion
G
3

This worked for me:

I was getting message

Execution failed for task ':app:compileDebugJavaWithJavac'.

Could not find tools.jar. Please check that C:\Program Files\Java\jre1.8.0_121 contains a valid JDK installation.

  • In Android Studio, check your SDK Location.
  • File, Project Structure, SDK Location, JDK Location.
  • Example: C:\android-studio\android-studio\jre
  • Copy the tools.jar file in the C:\android-studio\android-studio\jre\lib folder into the C:\Program Files\Java\jre1.8.0_121\lib folder.
  • Retry.
Glenglencoe answered 7/1, 2018 at 2:26 Comment(0)
C
2

Like other answers I set org.gradle.java.home property in gradle.properties file. But path with \ separators did not work (building on windows 10):

Java home supplied via 'org.gradle.java.home' is invalid. Invalid directory: C:Program FilesJavajdk1.8.0_65

So instead of

org.gradle.java.home=C:\Program Files\Java\jdk1.8.0_65

i had to use

org.gradle.java.home=C:/Program Files/Java/jdk1.8.0_65

then the build was successful

Problem is that project is build with JRE instead of JDK and since I was building it from eclipse this also worked:

  • In Window>Preferences>Gradle>Arguments specify Workspace JRE and specify your JDK.
  • In Window>Preferences>Java>InstalledJREs specify your JDK as default
Calyx answered 10/9, 2016 at 4:48 Comment(0)
M
2

In my case (Windows 10) after Java update I lost my Enviroment Variables, so I fixed added the variables again, based in the following steps https://confluence.atlassian.com/doc/setting-the-java_home-variable-in-windows-8895.html

Marcum answered 25/4, 2017 at 14:19 Comment(0)
W
2

I solved problem on this way:

Wadding answered 13/3, 2019 at 17:17 Comment(1)
Had to copy it to C:\Program Files (x86)\Java\jre1.8.0_211 and C:\Program Files (x86)\Java\jre1.8.0_251Breviary
F
2

Put in gradle.properties file the following code line:

org.gradle.java.home=C:\\Program Files\\Java\\jdk1.8.0_45

Example image

Finitude answered 17/10, 2020 at 9:50 Comment(0)
H
2

Use this with modern versions of gradle:

def compiler = javaToolchains.compilerFor { languageVersion = JavaLanguageVersion.of(java.sourceCompatibility.majorVersion) }.get()
implementation compiler.metadata.installationPath.files('lib/tools.jar')
Heartthrob answered 7/4, 2022 at 11:12 Comment(3)
the most elegant answer inspired as I understand by official Gradle documentation (docs.gradle.org/current/userguide/toolchains.html)Hunfredo
now amended so that the language version need not be hard-codedHeartthrob
NB. this is not used for Java 17 - there is no tools.jar any more..Heartthrob
R
1

Did you make sure that tools.jar made it on the compile class path? Maybe the path is incorrect.

task debug << {
    configurations.compile.each { println it }
}
Recommendation answered 5/7, 2012 at 13:34 Comment(1)
Yes it did made it to class path, but still getting the issue.Lydalyddite
M
1

Adding JDK path through JAVA_HOME tab in "Open Gradle Run Configuration" will solve the problem.

Monkish answered 18/12, 2017 at 3:35 Comment(0)
S
1

I've tried most of the top options but it seems that I had something wrong with my environment setup so they didn't help. What solved the issue for me was to re-install jdk1.8.0_201 and jre1.8.0_201 and this solved the error for me. Hope that helps someone.

Sawdust answered 22/2, 2019 at 18:17 Comment(1)
Years later: With Arch Linux I still had java-8-openjdk set as the default. Simply setting a newer version as the default solved the issue for me: sudo archlinux-java set java-20-openjdk. I added this as a comment to your solution since I believe it is very similar to your solution.Foetus
D
1

Could not find tools.jar

MacOS

echo export "JAVA_HOME=\$(/usr/libexec/java_home)" >> ~/.bash_profile

And restart Shell

Decorator answered 16/4, 2021 at 10:23 Comment(0)
H
0

For me this error ocurred after trying to use audioplayers flutter library. To solve i got tools.jar of the folder:

C:\Program Files\Android\Android Studio\jre\lib

and pasted on

C:\Program Files\Java\jre1.8.0_181\lib.

After this the build worked fine.

Headspring answered 17/1, 2021 at 13:28 Comment(0)
D
0

My Android Studio Version: 4.2.1

The "tools.jar" is provided by Oracle JDK which is required by android studio for compilation - I have faced this issue after updating android studio to latest version in my PC.

To Resolve the issue follow below steps:

  1. In Android studio File -> Project Structure -> SDKs (Under Platform Settings)

    A) Add JDK path by pressing '+' symbol in middle pane if suppose JDK/JDK home path is not present in the middle pane already (Middle pane also contains the Downloaded Android SDK's)

    B) Java sdk will be usually present/installed in the path 64 bit => "C:\Program Files\Java\jdk1.X.Y_ABC" (In my PC it is 1.8.0_202) or 32 bit => "C:\Program Files (x86)\Java\jdk1.X.Y_ABC"

  2. If suppose you don't have JDK installed in your PC, please download and install from Oracle Java website https://www.oracle.com/java/technologies/javase/javase-jdk8-downloads.html

  1. Set JDK and JRE Path(Download both from webpage mentioned in step 2) in system environment variable

    A) Press windows key type "Edit the system environment variables" and open the application

    B) Go to Advanced -> Environment Variables Under system variables add JAVA_HOME and JRE_HOME as below

Set Windows system environment variable

  1. Add jdk lib path on the Path environment variable under user variables (this step is required only if the error not resolves with the previous steps) C:\Program Files\Java\jdk1.X.Y_ABC\lib
Dogtooth answered 19/6, 2021 at 12:56 Comment(0)
G
0

For Windows add JDK home path to the Gradle property file as org.gradle.java.home. If you don't have gradle.properties file then create a new one and add

Ex: org.gradle.java.home=C:\Program Files\Java\jdk1.8.0_241

Galvanoscope answered 15/8, 2021 at 13:25 Comment(0)
W
0

In Case, Non of the above solutions work. Simply copy tools.jar file from jdk folder into the jre folder. Make sure you have elevated privileges before you make this move.

Wifehood answered 29/6, 2023 at 16:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.