Ant: Class not found: javac1.8 - java

I am trying to build a project using Ant in eclipse. I right-clicked on build.xml > Run As > Ant Build. However, I am getting the following error:
BUILD FAILED
C:\Users\David\eclipse\test-project\build.xml:26: Class not found: javac1.8
and also a warning:
compile:
[javac] C:\Users\David\eclipse\test-project\build.xml:26: warning: 'includeantruntime' was not set, defaulting to build.sysclasspath=last; set to false for repeatable builds
As I read in other posts that this might be due to having an ant version that is too old or not having set the environment variables correctly here is all the info:
C:\>java -version
java version "1.8.0_05"
Java(TM) SE Runtime Environment (build 1.8.0_05-b13)
Java HotSpot(TM) 64-Bit Server VM (build 25.5-b02, mixed mode)
C:\>ant -version
Apache Ant(TM) version 1.9.3 compiled on December 23 2013
C:\>echo %JAVA_HOME%
C:\Program Files\Java\jdk1.8.0_05
C:\>echo %JRE_HOME%
C:\Program Files\Java\jre8
EDIT:
Here is the whole build.xml, line 26 is the javac tag:
<?xml version="1.0"?>
<project name="test-project" default="main" basedir=".">
<!-- Sets variables which can later be used. -->
<!-- The value of a property is accessed via ${} -->
<property name="src.dir" location="src" />
<property name="build.dir" location="bin" />
<property name="dist.dir" location="dist" />
<property name="docs.dir" location="docs" />
<!-- Deletes the existing build, docs and dist directory-->
<target name="clean">
<delete dir="${build.dir}" />
<delete dir="${docs.dir}" />
<delete dir="${dist.dir}" />
</target>
<!-- Creates the build, docs and dist directory-->
<target name="makedir">
<mkdir dir="${build.dir}" />
<mkdir dir="${docs.dir}" />
<mkdir dir="${dist.dir}" />
</target>
<!-- Compiles the java code (including the usage of library for JUnit -->
<target name="compile" depends="clean, makedir">
<javac srcdir="${src.dir}" destdir="${build.dir}">
</javac>
</target>
<!-- Creates Javadoc -->
<target name="docs" depends="compile">
<javadoc packagenames="src" sourcepath="${src.dir}" destdir="${docs.dir}">
<!-- Define which files / directory should get included, we include all -->
<fileset dir="${src.dir}">
<include name="**" />
</fileset>
</javadoc>
</target>
<!--Creates the deployable jar file -->
<target name="jar" depends="compile">
<jar destfile="${dist.dir}\test-project1.jar" basedir="${build.dir}">
<manifest>
<attribute name="Main-Class" value="test.Main" />
</manifest>
</jar>
</target>
<target name="main" depends="compile, jar, docs">
<description>Main target</description>
</target>
</project>

The version of Ant bundled with your version of Eclipse is not compatible with Java 1.8.
Go to the Ant download page, and extract the latest version somewhere appropriate onto your filesystem.
In Eclipse, go to Window > Preferences > Ant > Runtime, click the Ant Home... button, and select the location that you extracted the newly downloaded Ant to.

To make it still more clear.
1>Set JAVA_HOME,JRE_HOME and Update Ant to 1.9
2>Over build.xml right click => run as (this takes into configuration)==>Now in this Edit Configuration and launch pop-up window
select Main tab then the third form field called "Arguments"
add:
-Dbuild.compiler=javac1.7
3> In build.xml add includeantruntime="false"
<javac srcdir="${src}" destdir="${bin}" debug="true" encoding="ISO-8859-1" includeantruntime="false">
<classpath refid="my.classpath"/>
</javac>
It should compile without any message

Mr. studro is right. I just confirmed an example on Ubuntu 14.04
sudo apt-get install ant
ant -version
Apache Ant (TM) Version 1.9.3 compiled on April August 2014
works perfectly in eclipse, just follow the steps described by Mr studro to configure the 'Ant Home' in eclipse with "/usr/share/ant".Regards, Stéphane.

I think, what you are seeing is Ant Bug 53347 (see https://issues.apache.org/bugzilla/show_bug.cgi?id=53347).
If so, try either pf the following workarounds:
Set the property "build.compiler" to a meaningful value like "javac1.7", or "javac1.3".
Set the "compiler" attribute of the "javac" element of your build script to either of the above values.
For all possible values, and their meaning, see http://ant.apache.org/manual/Tasks/javac.html

Make sure your source files are in "ProjectDirectory/src".

I already did some extensive googling before asking this question but continuing that I found a solution here:
This issue seems to only appear when using JDK 1.8 so using JDK 1.7 instead solves the problem. The following line needs to be added to eclipse.ini:
-vm "path-to-jdk-1.7\bin\javaw.exe"

Related

Jenkins Ant build failed "src does not exist!"

When I run my code in the console, it runs fine. It also runs fine on eclipse. But when I try to build on Jenkins, it is asking for a src folder:
"C:\Program Files (x86)\Jenkins\Work space\job1\src" does not exist!
build.xml
<project name="Sample Ant build" basedir=".">
<!-- ========== Initialize Properties =================================== -->
<!-- set global properties for build -->
<property name="basedir" value="." />
<property name="lib" value="${basedir}/lib" />
<property name="src" value="${basedir}/src" />
<property name="bin" value="${basedir}/bin" />
<property name="report-dir" value="${basedir}/Test-Report" />
<property name="testng-report-dir" value="${report-dir}/TestNGreport" />
<!-- ====== Set the classpath ==== -->
<path id="classpath">
<pathelement location="${bin}" />
<fileset dir="C:\WebDriver\Selenium">
<include name="*.jar" />
</fileset>
</path>
<!-- Delete directories -->
<target name="delete-dir">
<delete dir="${bin}" />
<delete dir="${report-dir}" />
</target>
<!-- Creating directories -->
<target name="create" depends="delete-dir">
<mkdir dir="${bin}" />
<mkdir dir="${report-dir}" />
</target>
<!-- Compile the java code from ${src} into ${bin} -->
<target name="compile" depends="create">
<javac srcdir="${src}" classpathref="classpath" includeAntRuntime="No" destdir="${bin}" />
<echo> /* Compiled Directory Classes */ </echo>
</target>
<!-- Runs the file and generates Reportng report for TestNG-->
<taskdef name="testng" classname="org.testng.TestNGAntTask" classpathref="classpath" />
<target name="testng-execution" depends="compile">
<mkdir dir="${testng-report-dir}" />
<testng outputdir="${testng-report-dir}" classpathref="classpath" useDefaultListeners="true">
<xmlfileset dir="${basedir}" includes="testng.xml" />
</testng>
</target>
Eclipse uses a workspace, Jenkins uses a workspace (and in our case, TFS also uses a workspace). They are not the same thing. (Your query is missing some details to answer authoritatively, some some assumptions were made)
Your build.xml script sets BASEDIR to ".", or the working directory. For a Jenkins job, that is as below - See Administering Jenkins:
JENKINS_HOME
+- workspace (working directory for the version control system)
+- [JOBNAME] (sub directory for each job)
In your case, JENKINS_HOME\workspace\[JOBNAME] is C:\Program Files (x86)\Jenkins\Work space\job1.
My money is that is not where your Eclipse workspace is mapped to.
I would make the following recommendations:
Develop your code and test it in Your Eclipse IDE. When satisfied, commit to your version control system (VCS).
Setup Jenkins to pull code from your VCS into the local workspace of your job and build there. As it's separate to your working copy it helps isolate changes and ensures you have committed everything to source control so your builds come from a know, reproducible source.
Set JEKNINS_HOME to a location outside of C:\Program Files ...; it's bad practice to place data (like source files and a build) in a location from installation binaries. Lots of goods reasons to do so, including security, easier space management, etc.
Choose an appropriate location and set the directory configurations for the Master (Jenkins | Manage Jenkins | Configure System; the first 3 values)
IF using distributed builds (master/node), within your node configuration (Jenkins | Manage Jenkins | Manage Nodes | Configure => Remote Root directory ) to an appropriate location

Class org.eclipse.jdt.core.JDTCompilerAdapter could not be loaded because of an invalid dependency

I am creating a java agent that will be used to to do some bytecode modification to some classes org.eclipse.jdt.core.JDTCompilerAdapter is one of them. I am using javassit to modify some the execute() method of org.eclipse.jdt.core.JDTCompilerAdapter. So I have included ecj as in my agent project (using gradle)
compile group: 'org.eclipse.jdt.core.compiler' ,name: 'ecj', version :'4.3.1'
As I need to use some classes from ecj.
The goal of the agent is to intercept the calls to execute method, modify the execute method to add some calls to some of my classes in the aim of triggering some processing.
I am testing the agent against a Simple java project with 2 classes. the project is builded with ant and uses JDTCompilerAdapter as a compiler.
Here is the build.xml file
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<project basedir="." default="build" name="TestProject">
<property file="build.properties" />
<property name="debuglevel" value="source,lines,vars"/>
<property name="target" value="1.7"/>
<property name="source" value="1.7"/>
<path id="PClasspath">
<pathelement location="bin"/>
</path>
<target name="init">
<mkdir dir="bin"/>
<copy includeemptydirs="false" todir="bin">
<fileset dir="src">
<exclude name="**/*.java"/>
</fileset>
</copy>
</target>
<target name="clean">
<delete dir="bin"/>
</target>
<target depends="clean" name="cleanall"/>
<target depends="init" name="build">
<javac debug="true" debuglevel="${debuglevel}" destdir="bin" includeantruntime="false" source="${source}" target="${target}">
<src path="src"/>
<classpath refid="PClasspath"/>
</javac>
</target>
<!--
<target description="copy Eclipse compiler jars to ant lib directory" name="init-eclipse-compiler">
<copy todir="${ant.library.dir}">
<fileset dir="${ECLIPSE_JDT_CORE}" includes="*.jar"/>
</copy>
</target>-->
<target name="build-e" >
<property name="build.compiler" value="org.eclipse.jdt.core.JDTCompilerAdapter"/>
<antcall target="build"/>
</target>
The agent is to be used when building a project.
So for testing the agent I use this command:
java -jar agent-wrapper.jar --outdir ./out --exec ./build_wrapper.sh
build_wrapper.sh contains this (I have added ecj dependency so I could compile the project with JDTCompilerAdapter as I have in bulid.xml <property name="build.compiler" value="org.eclipse.jdt.core.JDTCompilerAdapter"/> :
../ant/bin/ant -lib ../eclipse/plugins/ecj-4.3.1.jar build-e
The idea is that the agent-wrapper will parse the argument (outdir is used to generate some stuff and exec is a script used to launch a the build of my test project) get the command to be executed from build_wrapper.sh (in this case ../ant/bin/ant -lib ../eclipse/plugins/ecj-4.3.1.jar build-e) and add it self as java agent to the command.
The problem occurs during the execution of the agent. Here is the output:
java -jar custom-agent.jar --outdir ./out --exec ./build_wrapper.sh [10:18:53]
Picked up JAVA_TOOL_OPTIONS: -javaagent:/Users/dev/TestAgent/project/custom-agent.jar=OUTDIR=/Users/dev/TestAgent/project/./out
objc[30474]: Class JavaLaunchHelper is implemented in both /Library/Java/JavaVirtualMachines/jdk1.7.0_51.jdk/Contents/Home/jre/bin/java and /Library/Java/JavaVirtualMachines/jdk1.7.0_51.jdk/Contents/Home/jre/lib/libinstrument.dylib. One of the two will be used. Which one is undefined.
Buildfile: /Users/dev/TestAgent/project/build.xml
build-e:
init:
[mkdir] Created dir: /Users/dev/TestAgent/project/bin
build:
BUILD FAILED
/Users/dev/TestAgent/project/build.xml:47: The following error occurred while executing this line:
/Users/dev/TestAgent/project/build.xml:32: Class org.eclipse.jdt.core.JDTCompilerAdapter could not be loaded because of an invalid dependency.
Total time: 2 seconds
abnormal termination, exit code: 1
When I don't use ecj-4.3.1.jar inside my agent project, the build runs well I intercept the call to execute() method but I can't use the other classes from ecj jar.
The show stopper error is "Class org.eclipse.jdt.core.JDTCompilerAdapter could not be loaded because of an invalid dependency."
First hint at the fault might be found from reading this link
http://help.eclipse.org/mars/index.jsp?topic=%2Forg.eclipse.jdt.doc.user%2Ftasks%2Ftask-ant_javac_adapter.htm
The second hint might be that one of the jars required for running the JDTCompilerAdapter is missing.
To get the JDTCompilerAdapter to work I copied both the JDTCompilerAdapter.jar and org.eclipse.jdt.core.jar into the ant/lib folder.
There are differences based on version of eclipse and the version of java which are documented in the link mentioned above.

Java ant Eclipse run error [duplicate]

This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
ant error JAVA_HOME does not point to SDK
I'm getting the following error:
BUILD FAILED
C:\Users\myname\Documents\CMSC\Proj1\build.xml:22: 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 (x86)\Java\jre7"
Total time: 1 second
My build.xml file contains the following code:
<project name="Project1" default="compile" basedir=".">
<description>
Build file for Project1
</description>
<!-- global properties for this build file -->
<property name="source.dir" location="src"/>
<property name="build.dir" location="bin"/>
<property name="doc.dir" location="doc"/>
<property name="main.class" value="proj1.Project1"/>
<!-- set up some directories used by this project -->
<target name="init" description="setup project directories">
<mkdir dir="${build.dir}"/>
<mkdir dir="${doc.dir}"/>
</target>
<!-- Compile the java code in ${src.dir} into ${build.dir} -->
<target name="compile" depends="init" description="compile java sources">
<javac srcdir="${source.dir}" destdir="${build.dir}"/>
</target>
<!-- execute the program with the fully qualified name in ${build.dir} -->
<target name="run" description="run the project">
<java dir="${build.dir}" classname="${main.class}" fork="yes">
<arg line="${args}"/>
</java>
</target>
<!-- Delete the build & doc directories and Emacs backup (*~) files -->
<target name="clean" description="tidy up the workspace">
<delete dir="${build.dir}"/>
<delete dir="${doc.dir}"/>
<delete>
<fileset defaultexcludes="no" dir="${source.dir}" includes="**/*~"/>
</delete>
</target>
<!-- Generate javadocs for current project into ${doc.dir} -->
<target name="doc" depends="init" description="generate documentation">
<javadoc sourcepath="${source.dir}" destdir="${doc.dir}"/>
</target>
</project>
What am I doing wrong here? It appears that it's trying to find a javac file in my src folder but I don't know how to fix that.
The JAVA_HOME must be pointed to JDK and not JRE.
To test it, before executing ant type set JAVA_HOME=C:\Program Files (x86)\Java\jdk1.7.0_07 or whatever version you have.
Download the latest JDK here. Install it and notice where it goes. For example, the 32-bit JDK should install to C:\Program Files (x86)\Java\jdkSOMETHING.
In Eclipse, go to Window > Preferences > Java > Installed JREs and click Add > Standard JVM (then Next). Browse to the directory noted above for the JDK and then hit Finish. To check that your project is using this JDK, right-click the project and go to Properties. From there, look for Java Build Path. Under the Libraries tab you should see a JRE System Library. If it's not the JDK you just added, click the entry and hit the Edit button and change it.
Now run your Ant build.
Looking at your error, looks like its Windows environment and you are running ant through command prompt.
Follow Right Click(My Computer) -> properties -> Advanced
-> Environment Variables -> System Variables -> New
Add Variable Name = JAVA_HOME
Variable Value = Path to your Base folder of Java
e.g. C:\Program Files\Java\jdkxxx
Restart the command prompt, type java -version. If it prints your java version correctly, you should be all set.
it is saying
"Perhaps JAVA_HOME does not point to the JDK."
Perhaps you should check that :D

How to use GCJ with Ant?

I'm fairly new to both Apache Ant and GCJ, and I'm having a hard time trying to build with GCJ via Ant.
My app is in Scala, so I need to use GCJ to take .class files as source. No problem compiling .scala to .class with Ant.
First I figured out how to manually compile a .class file to .o (object), this way:
gcj --classpath=(...) -c (somepath)MouseClickListener.class -o (somepath)MouseClickListener.o
I see here that Ant supports GCJ compilation through the javac tag. So I figured this should work:
<target name="gcjCompile" depends="compile">
<mkdir dir="${object.dir}" />
<javac srcdir="${build.dir}"
destdir="${object.dir}"
compiler="gcj"
executable="C:/gcc/gcc-4.3/bin/gcj.exe"
classpathref="gcjProject.classpath">
<include name="**/*.class"/>
</javac>
</target>
But this javac task does nothing and I get no errors. Any clues?
Thanks
It sounds like you want to link your app into a native executable. That means that you've already compiled the source into JVM bytecode (as you've figured out to do by compiling .scala into .class files). You'll need to run the gcj command manually using the <exec> task to compile the bytecode into gcc object code files.
I'd recommend something like this:
<property name="main.class" value="Main" />
<property name="class.dir" value="${basedir}/classes" />
<target name="compile">
<mkdir dir="${class.dir}" />
<javac srcdir="${build.dir}"
destdir="${class.dir}"
compiler="gcj"
executable="C:/gcc/gcc-4.3/bin/gcj.exe"
classpathref="gcjProject.classpath">
<include name="**/*.java"/>
</javac>
</target>
<target name="link" depends="compile">
<mkdir dir="${object.dir"} />
<exec cmd="C:/gcc/gcc-4.3/bin/gcj.exe">
<arg value="-classpath=${object.dir}" />
<arg value="-c" />
<arg value="*.class" />
</exec>
</target>
Keep in mind that you need to define the build.dir and object.dir properties, and you may need to add a depends task before the javac in the compile target (or just recompile from scratch each time). I may have missed a lot of things, you should check the manual pages (for gcj, gcc, and ant) if it doesn't work at first.

ant deploy problem

I am working on a spring project. I use ant to deploy application and STS (eclipse based) IDE to develop. I set the CATALINA_HOME environment variable
echo $CATALINA_HOME
/home/username/springsource/apache-tomcat
When I run the deploy ant task from IDE it deploys to a folder under
/home/username/workspace/myproject/${env.CATALINA_HOME}/webapp
but not
/home/username/springsource/apache-tomcat/webapp
Do you know any fix?
My build.properties file
src.dir=src
web.dir=web
build.dir=${web.dir}/WEB-INF/classes
name=myproject
appserver.home=${env.CATALINA_HOME}
deploy.path=${appserver.home}/webapps
appserver.lib=${appserver.home}/lib
and build.xml file
<?xml version="1.0" encoding="UTF-8"?>
<project name="kervan" basedir="." default="usage">
<property environment="env"/>
<property file="build.properties"/>
<path id="cp">
<fileset dir="${web.dir}/WEB-INF/lib">
<include name="*.jar"/>
</fileset>
<fileset dir="${appserver.lib}">
<include name="servlet-api.jar"/>
</fileset>
<pathelement path="${build.dir}"/>
</path>
<target name="usage">
<echo message=""/>
<echo message="${name} build file"/>
<echo message="-----------------------------------"/>
<echo message=""/>
<echo message="Available targets are:"/>
<echo message=""/>
<echo message="build --> Build the application"/>
<echo message="deploy --> Deploy application as a WAR file"/>
<echo message=""/>
</target>
<target name="build" description="Compile main source tree java files">
<mkdir dir="${build.dir}"/>
<javac destdir="${build.dir}" source="1.6" target="1.6"
debug="true" deprecation="false" optimize="false"
failonerror="true">
<src path="${src.dir}"/>
<classpath refid="cp"/>
</javac>
</target>
<target name="deploy" depends="build" description="Deploy application as a WAR file">
<war destfile="${name}.war"
webxml="${web.dir}/WEB-INF/web.xml">
<fileset dir="${web.dir}">
<include name="**/*.*"/>
</fileset>
</war>
<copy todir="${deploy.path}" overwrite="true">
<fileset dir=".">
<include name="*.war"/>
</fileset>
</copy>
</target>
</project>
Try putting the following after the two <property> lines:
<echo message="CATALINA_HOME=${env.CATALINA_HOME}" />
and see what it outputs. If it in fact outputs the correct value, then something strange may be happening. If it outputs the literal string
CATALINA_HOME=${env.CATALINA_HOME}
then somehow your ant script hasn't picked up the environment variable.
Note that when you set an environment variable for your system, only applications launched AFTER the variable is set will recognize the new variable. And variables set from the command line will only be recognized if the application being launched is being launched from that same command line session.
If you're running from within Eclipse or an Eclipse-like environment, Eclipse can be kind of weird in that depending on how you launch it, it's startup scripts won't make your environment natively available to your in-IDE Ant build process.
With my Eclipse-based Ant build, I had to manually set the environment. So for me, I right click on my project & go to "Properties". Then I click on the "Builders" section. I select my "Ant Builder" and click "Edit...". Under this section there's an "Environment" tab where you can specify environment variables and their corresponding values.
Even if you're not using Eclipse exactly like I was, poke around in the build properties and you should be able to find a way to specify environment variables and make them available to the build process.
Is CATALINA_HOME set in your environment?
e.g. Windows
echo %CATALINA_HOME%
Linux
echo $CATALINA_HOME
You could always hardcode the value in your properties file if it's not getting resolved correctly but provided it's in your environment then it should work.
The forum here discusses the same problem:
http://www.nabble.com/%3Cproperty-environment%3D%E2%80%9Denv%E2%80%9D%3E-doesn%27t-pick-up-an-environment-variable-td21481164.html
When run from eclipse, I don't believe the environment is passed to ant. You will have to specify each of the environment variables (and the values) that you want passed to ant in the configuration of the build file within eclipse.
if you are set your environmental variable in global
/etc/environment
thats the problem in Ubuntu. Ant does not pick the environment variable from here.
But the echo $CATALINA_HOME works fine in terminal. I am facing the same problem.
set your environment in .bashrc may fix your problem.
I recently suffered a similar issue.
The problem was in the CATALINA_HOME environment variable: I needed to close the path with a backslash ("/"):
$ export CATALINA_HOME=/home/username/springsource/apache-tomcat/
After fixing that I could deploy the application with ant.
Please make sure you end your path with a / and it shall solve your problem.
example: export JAVA_HOME=/opt/java/
instead of: export JAVA_HOME=/opt/java

Categories

Resources