Where does Ant look to find your Java Home when using Eclipse? - java

My Ant script won't compile code. It says it doesn't find a "C:\Program Files\Java\jdk1.6.0_25". Which is correct, it should be looking for "C:\Program Files\Java\jdk1.6.0_24"
However, I don't know where it is getting the bad reference from. I seem to have 24 on Java_home and in all my Eclipse settings. Where can it be getting this other reference from? How can I fix it?

Eclipse gets his JAVA_HOME for Ant from the Project settings > Java Build Path > Libraries > JRE System Library not from Windows %JAVA_HOME% environment variable.
Normally it uses the default Eclipse JRE setting which should be C:\Program Files\Java\jdk1.6.0_24 in your case.Maybe some team member altered the project setting to C:\Program Files\Java\jdk1.6.0_25 and made a commit !?
So you have to check the project settings also, because they overwrite the general Eclipse settings.
Did you already run your antfile in Eclipse with debugmode via Run As Ant Build... and
Main Tab > Arguments > -debug > Run to get more information ?
Is there some fork=true involved in your script ?
Also put some more diagnostic output in your antfile via :
<echoproperties prefix="java"/>
<echoproperties prefix="ant"/>
to get the relevant settings logged.

you can specify which jre the ant uses in eclipse: preferences --> ant

Related

Gradle not found java class path [duplicate]

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.
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:
And then you need to edit your PATH variable to include JAVA_HOME, i.e. %JAVA_HOME%/bin;
Re-open command prompt and should run.
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")
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 :
Go to the "Gradle Task" view
Right Click on the task you want to execute
Select Open Gradle Run Configuration
In the tab "Java Home" select your local JDK repository then click OK
Run again, Enjoy!
I just added a gradle.properties file with the following content:
org.gradle.java.home=C:\\Program Files\\Java\\jdk1.8.0_45
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
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
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.
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
Add this to gradle.properties:
org.gradle.java.home=C:\Program Files\Java\jdk1.8.0_91
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.
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.
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.
On windows 10, I encounter the same problem and this how I fixed the issue;
Access Advance System Settings>Environment Variables>System
Variables
Select PATH overwrite the default
C:\ProgramData\Oracle\Java\javapath
With your own jdk installation that is JAVA_HOME=C:\Program Files\Java\jdk1.8.0_162
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)!
What solved it for me was the following:
found tools.jar in C:\Program Files\Android\Android Studio\jre\lib
copy paste to C:\Program Files (x86)\Java\jre1.8.0_271\lib
ran the code again and it worked.
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
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
Start the System Control Panel applet (Start - Settings - Control
Panel - System).
Select the Advanced tab.
Click the Environment
Variables button.
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
Under System Variables, select Path, then click Edit,then click new button then past the following line:
%JAVA_HOME%/bin;
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.
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
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
I solved problem on this way:
download file tools-1.8.0.jar on http://www.java2s.com/Code/Jar/t/Downloadtools180jar.htm
unzip file and rename to tools.jar
copy to C:\Program Files\Java\jre1.8.0_191\lib folder
Retry
Put in gradle.properties file the following code line:
org.gradle.java.home=C:\\Program Files\\Java\\jdk1.8.0_45
Example image
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')
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 }
}
Adding JDK path through JAVA_HOME tab in "Open Gradle Run Configuration" will solve the problem.
Could not find tools.jar
MacOS
echo export "JAVA_HOME=\$(/usr/libexec/java_home)" >> ~/.bash_profile
And restart Shell
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.
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.
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:
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"
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
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
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
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

javac not reconigzed for Jenkins Windows batch command

Not sure what the issue is here but javac is not recognized when I try to compile a java file. I am assuming I need a special plugin for this or I am required to use apache ANT but not too sure.
cd src
javac HelloWorld.java
'javac' is not recognized as an internal or external command,
operable program or batch file.
java HelloWorld
Error: Could not find or load main class HelloWorld
In Jenkins goto Manage Jenkins -> Configure System , scroll down to JDK installations and click the ADD JDK button.
I recommend trying the Install Automatically option, and let it install a fresh copy and give it a name you can recognize in project setting like jdk8_update51, and agree to the license.
In each project check that the JDK Setting is the correct version.
In windows batch command text box, you may want to add
echo "PATH is:"
echo %PATH%
echo "JAVA_HOME is:"
echo %JAVA_HOME%
javac -version
If this fails as above, try :
eg.
set PATH="C:\Program Files\Java\jdk1.8.0_31\bin"
javac -version
should print the following in Console Output:
javac 1.8.0_31
Note: "C:\Program Files\Java\jdk1.8.0_31\bin" should have the file javac.exe in it.
For Resolving this issue Go to :
Jenkins --> Manage Jenkins --> Global tool configuration.
You can add the full path JAVA_HOME there or click on "install automatically" for automatically downloading the Java.
I just tried this and issue is resolved now.
Thanks to Stackoverflow as well !!
Select "Manage Jenkins" and then "Configure System" on the Dashboard.
On the screen that opens, there is the "Environment variables" section. The tick next to it is marked. Then click the "Add" button. The «name» and «path» fields that appear after this marking are filled by entering the java-jdk path as in the image. Then click "Apply" and "Save" to complete the process.
That's all. I hope this will fix your problem. :)
**This solution can use for all similar command errors for python,maven, etc. **

Cordova Android platform - ant installation not recognized

I keep recieving the following error when attempting to add a platform to a cordova project.
ERROR: executing command 'ant', make sure you have ant installed and added to your path.
I have added my JAVA_HOME, ANT_HOME and ANDROID_HOME (both \tools and \platform-tools) environment variables and can confirm they are all installed correctly be executing 'java', 'ant -version' and 'android' commands in my console and getting responses.
Right now, I have the above mentioned environment variables as both User and System variables, but have also tried just having them configured as System variables.
Ant version: 1.9.2
java: jdk1.8.0_11
I'm quite stumped right now because everything seems to be configured/installed correctly. Is there a specific location I should install/un-zip these components?
You have to show full path to bin folder of ANT. see for the variable name path, edit it. add one of the following to the end of it's value.
Suppose your ant folder is in d drive like this D:\ant then your ANT_HOME value should be D:\ant. Then add this to your path variable
%ANT_HOME%\bin
OR add the full path like
D:\ant\bin

How to specify Eclipse's java.home setting for Ivy/Ant

When running an ivy resolve via IvyDE in Eclipse, which contains a packager.xml that executes a javac ant task, I get the following:
Unable to find a javac compiler;
com.sun.tools.javac.Main is not on the
classpath. Perhaps JAVA_HOME does not
point to the JDK. It is currently set
to "C:\Program
Files\Java\jdk1.6.0_22\jre"
at org.apache.tools.ant.taskdefs.compilers.CompilerAdapterFactory.getCompiler(CompilerAdapterFactory.java:108)
In eclipse.ini I have
-vm
C:\Program Files\Java\jdk1.6.0_22\bin\javaw.exe
and in Help - About Eclipse - Installation Details - Configuration I see
java.home=C:\Program Files\Java\jdk1.6.0_22\jre
How can I make java.home point to C:\Program Files\Java\jdk1.6.0_22 instead?
Eclipse uses its own javac implementation internally. Therefore for most of the purpose you just need a compatible JRE. But javac task in ANT considers the JAVA_HOME or check for the local path variable. On windows machine as temporary fix you can do:
Open a command prompt
then type
SET path=C:\Program Files\Java\jdk1.6.0_22\;%path% hit enter
then type
<eclipsehome>\eclipse.exe
Now any process spawned from your eclipse will have your JDK version in the path variable.
for a permanent solution..
1. go to advance system setting
2. select advanced tab
3. select environment variables
4. modify the system path variable and JAVA_HOME (if required)
You can do the similar thing for *nix system (just check how to set environment setting on them)
Hope this will work.
UPDATE
The PROJECT classpath isn't the same as the ANT classpath. To set up an Ant classpath, navigate to window->preferences->ant->runtime here check whether the Global Entries points to C:\Program Files\Java\jdk1.6.0_22\lib\tools.jar if not then add it. Normally it works in most of the cases where ant cries for javac task. if it fails then please check whether some other script is not resetting your JAVA_HOME variable and ANT global entries setting.
If IvyDE is not using the ANT bundled with eclipse as plugin (and referring to some standard installation) or if you have defined a environment variable like ANT_HOME then copy
tools.jar file from C:\Program Files\Java\jdk1.6.0_22\lib\ to ANT_HOME\lib.
Also check this previous SO post ( Ant can't find Javac. No matter what I do it always claims JAVA_HOME is "C:\Program Files\Java\jre6" )
UPDATE 2
ok..
Check this page... http://ant.apache.org/ivy/ivyde/history/latest-milestone/preferences.html
Order of the classpath entries: By
default, the classpath container
entries are ordered by order of
declaration in the ivy.xml. When there
are many entries, it can be useful to
have them ordered lexically for
enhanced readability.
Is tools.jar present in ivy.xml? please check once.

How to install Apache Ant?

I want to install SMSlib (http://smslib.org/) in installation instruction (http://code.google.com/p/smslib/wiki/Installation).
In here I must install Apache Ant, but I didn't understand how to do that.
I already download Apache Ant 1.7.1 , read manual (http://ant.apache.org/manual/index.html)
In here I must have RPM version from jpackage.org right? I already go to http://www.jpackage.org/ after that what must I do? I already try (http://mirrors.dotsrc.org/jpackage/1.7/generic/free/repodata/) and (http://www.jpackage.org/browser/browse.php?jppversion=1.7) but too many link. I don't know witch one must I download
If you're on Windows, you can use WinAnt, a Windows installer for Apache Ant that I made.
Step 1: Download and install
Download Ant. Go to the Ant homepage and click to download the binary. Because we’re talking about Windows, choose to download the ZIP file rather than any of the others. Scroll down to where it says “Current release of Ant” and click on the ZIP filename.
Once downloaded, unzip the file. You’ll now need to choose a permanent home for Ant on the computer. c:\java\ant is often used, but you can put it wherever you want.
Step 2: Set environment variables
For Windows XP: To set environment variables on Windows XP, right click on My Computer and select Properties. Then go to the Advanced tab and click the Environment Variables button at the bottom.
For Windows 7: To set environment variables on Windows 7, right click on Computer and select Properties. Click on Advanced System Settings and click the Environment Variables button at the bottom.
The only environment variable that you absolutely need is JAVA_HOME, which tells Ant the location of your JRE.
If you’ve installed the JDK, this is likely
c:\Program Files\Java\jdk1.x.x\jre
on Windows XP and
c:\Program Files(x86)\Java\jdk1.x.x\jre
on Windows 7. You’ll note that both have spaces in their paths, which causes a problem. You need to use the mangled name[3] instead of the complete name. So for Windows XP, use C:\Progra~1\Java\jdk1.x.x\jre and for Windows 7, use C:\Progra~2\Java\jdk1.6.0_26\jre if it’s installed in the Program Files(x86) folder
That alone is enough to get Ant to work, but for convenience, it’s a good idea to add the Ant binary path to the PATH variable.
This variable is a semicolon-delimited list of directories to search for executables. To be able to run ant in any directory, Windows needs to know both the location for the ant binary and for the java binary. You’ll need to add both of these to the end of the PATH variable. For Windows XP, you’ll likely add something like this:
;c:\java\ant\bin;C:\Progra~1\Java\jdk1.x.x\jre\bin
For Windows 7, it will look something like this:
;c:\java\ant\bin;C:\Progra~2\Java\jdk1.x.x\jre\bin
Done
Once you’ve done that and applied the changes, you’ll need to open a new command prompt to see if the variables are set properly. You should be able to simply run ant and see something like this:
Buildfile: build.xml does not exist!
Build failed
That means Ant is installed properly and is looking for a build.xml file.
Source: http://www.nczonline.net/blog/2012/04/12/how-to-install-apache-ant-on-windows/
You can follow these instrunctions-
1)Download the latest version of ant from http://ant.apache.org/bindownload.cgi
2)Unzip and save it to your C:\ directory as ant.
3)Add the bin directory to your PATH environment variable.
4)Add the ANT_HOME environment variable set to C:\ant.
you can use following commands to set ANT_HOME variable
C:>set ANT_HOME=C:\ant
C:>set JAVA_HOME=C:\jdk
C:>set PATH=%ANT_HOME%\bin;%JAVA_HOME%\bin
C:>ant -version
Apache Ant version 1.8.1 compiled on April 30 2010
if you have java installed on your machine
5)Add the ANT_OPTS environment variable set to -Xmx256M.
This is all you need to install ant on your machine.
1) Download "apache-ant-1.8.4-bin.zip" from "http://ant.apache.org/bindownload.cgi"
2) Unzip it and copy "apache-ant-1.8.4" in "c:\Program Files"
3) Right Click "My Computer" -> properties -> Advanced -> Environment variables -> Edit variable "PATH" and append value "C:\Program Files\Java\jdk 1.7.0_04\bin;C:\Program Files\apache-ant-1.8.4\bin" and Click "OK".
4) open cmd and type "ant" for checking.
the installation of ant and smslib is pretty simple. All you need to do is extract it to some directory. Export the path in case of Linux or set the ANT_HOME\bin to the PATH variable to access it from any directory in the shell. ANT_HOME is the root directory where ant is installed.
For SMSLib Download the zip file and this link should guide you on a step by step basis
Sound's like you're using Linux. If so, the easiest way would be to use a package management frontend like Synaptic and install ant in there.
Its very simple just install Apache ant installer for windows give on [link][1]
[1]: http://code.google.com/p/winant/ and all is set.No need to set Environment variables.It will automatically do every thing for you.
You can install Apache Ant with only single command on Ubuntu:
sudo apt install ant
Check the version of ant installed with:
ant -version

Categories

Resources