[java] Gradle does not find tools.jar

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.

This question is related to java gradle groovy build

The answer is


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.


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!


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 }
}

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.


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

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


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.


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.

Add this to gradle.properties:

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


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


Put in gradle.properties file the following code line:

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

Example image


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.


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


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.

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


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

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

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.


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;


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


I solved problem on this way:


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

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

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.


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.


Examples related to java

Under what circumstances can I call findViewById with an Options Menu / Action Bar item? How much should a function trust another function How to implement a simple scenario the OO way Two constructors How do I get some variable from another class in Java? this in equals method How to split a string in two and store it in a field How to do perspective fixing? String index out of range: 4 My eclipse won't open, i download the bundle pack it keeps saying error log

Examples related to gradle

Gradle - Move a folder from ABC to XYZ A failure occurred while executing com.android.build.gradle.internal.tasks Gradle: Could not determine java version from '11.0.2' Android Gradle 5.0 Update:Cause: org.jetbrains.plugins.gradle.tooling.util Deprecated Gradle features were used in this build, making it incompatible with Gradle 5.0 Failed to resolve: com.android.support:appcompat-v7:28.0 Failed to resolve: com.google.firebase:firebase-core:16.0.1 com.google.android.gms:play-services-measurement-base is being requested by various other libraries java.lang.NoClassDefFoundError:failed resolution of :Lorg/apache/http/ProtocolVersion Error - Android resource linking failed (AAPT2 27.0.3 Daemon #0)

Examples related to groovy

Jenkins pipeline how to change to another folder groovy.lang.MissingPropertyException: No such property: jenkins for class: groovy.lang.Binding Run bash command on jenkins pipeline Try-catch block in Jenkins pipeline script How to print a Groovy variable in Jenkins? Jenkins pipeline if else not working How to set and reference a variable in a Jenkinsfile Jenkins: Can comments be added to a Jenkinsfile? How to define and use function inside Jenkins Pipeline config? Jenkins: Cannot define variable in pipeline stage

Examples related to build

error: This is probably not a problem with npm. There is likely additional logging output above Module not found: Error: Can't resolve 'core-js/es6' WARNING in budgets, maximum exceeded for initial How can I change the app display name build with Flutter? Error - Android resource linking failed (AAPT2 27.0.3 Daemon #0) Still getting warning : Configuration 'compile' is obsolete and has been replaced with 'implementation' Could not find com.android.tools.build:gradle:3.0.0-alpha1 in circle ci Error:Execution failed for task ':app:compileDebugKotlin'. > Compilation error. See log for more details Component is part of the declaration of 2 modules Maven build Compilation error : Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (default-compile) on project Maven