"Error running javac compiler" in Ant - java

I was trying to run an Ant script in Eclipse, but I am getting the below error:
BUILD FAILED
C:\Users\name\workspace\Client\build\Build_Local.xml:111: Error running C:\Program Files\Java\jdk1.6.0_45\jre\bin\javac compiler.
My java home is pointed to jdk and I edited the java path in windows>preferences>installed jre as well as in run>external tool>config>jre. Both locations are pointed to jdk. Also I am posting my build file(This is not the exact file, I have edited a lot before posting)
<?xml version="1.0"?>
<project name="projectName" default="main" basedir="C:\path\build">
<property name="LocalHome" value="C:\proName" />
<property name="home" value="${LocalHome}"/>
<property name="java.home" value="C:\Program Files \Java\jdk1.6.0_45"/>
<property name="CLIENT_JAR" value="client.jar"/>
<target name="main" description=": This is the default target.">
<antcall target="name"/>
</target>
<target name="name">
<javac failonerror="true" srcdir="${home}/source" destdir="path to class file"
executable="${java.home}\bin\javac" fork="true" debug="on" encoding="UTF-8"
source="1.6" target="1.6" bootclasspath="${java.home}/jre/lib/rt.jar"
classpath=""/>
</project>
See here I mentioned the javac path to "C:\Program Files \Java\jdk1.6.0_45\bin\javac". Then why does Ant choose javac from C:\Program Files\Java\jdk1.6.0_45\jre\bin\javac (see the build error)

It worked when I directly used java home path(C:\Program Files\Java\jdk1.6.0_45) in place of java.home

${java.home} points to C:\Program Files\Java\jdk1.6.0_45\jre. So you should be able to just remove that from bootclasspath:
bootclasspath="${java.home}/lib/rt.jar"
If you put the following line below a target, you'll see it print out the full path with jre appended:
<echo level="info" message="java.home = ${java.home}"/>

Related

Ant javac task not compiling. Its 'srcdir' parameter not behaving as documented. Is this a bug?

I have written the Ant script pasted below. It uses the javac task. The Apache documentation states that scrdir parameter is used to point to the source (.java) files. But it also explains that the javac task can use patternsets.
I believe there may be a bug in the javac task or at least, perhaps a poor implementation of the javac Ant task.
In my code, the compile target fails because the source files cannot be found. Can anyone please explain why?
Background:
Directory structure of my project is as follows:
My ant scripts are in the ant directory.
Source files are inside wm-adapter/src
<?xml version="1.0" encoding="UTF-8"?>
<project name="wm-adapter" default="dev" basedir=".">
<property file="Build.properties"/>
<property name="build.compiler" value="javac1.5"/>
<target name="compile" depends="prepare">
<!-- Define required FileSets and PatternSets... -->
<patternset id="patternset.wm.adapter.sources">
<include name="${source.dir}/*.java"/>
<exclude name=""/>
</patternset>
<fileset id="fileset.wm.adapter.sources" dir="${proj.dir}">
<patternset refid="patternset.wm.adapter.sources"/>
</fileset>
<!-- Compile... -->
<javac srcdir="${basedir}"
destdir="${classes.dir}"
classpath="${compile.classpath}"
debug="${javac.debug}"
verbose="true"
fork="true"
source="1.5"
target="1.5">
<patternset refid="patternset.wm.adapter.sources"/>
</javac>
<echo message=" Compilation completed successfully!"/>
</target>
Here is a snippet from the properties file:
# DEFINE DIRECTORY STRUCTURE...
proj.dir=../wm-adapter
source.dir=${proj.dir}/src
build.dir=${proj.dir}/build
classes.dir=${build.dir}/classes
dist.dir=${proj.dir}/dist
lib.dir=../lib
docs.dir=${proj.dir}/documentation
resources.dir=${proj.dir}/www
config.dir=${proj.dir}/config
# DECLARE ANY OTHER WORKING DIRECTORIES...
tmp.jar.dir=C:\\Temp
tmp.app.dir=${tmp.jar.dir}/FATCA_WM_Adapter
# Name the jar file...
jar.file=FATCA_WM_Adapter.jar
# IMPORT EXTERNAL JARS INTO OUR CLASSPATH...
compile.classpath=${g11nutils.jar};${javax.ejb_3.0.jar};${middleware_UTILS.jar};${wmbrokerclient.jar};${classes.dir}
compile.classpath=${classes.dir};${lib.dir}
basedir has been defined as '.'
I know how to make it work: The target will compile if:
I make changes to the patternset as shown below:
<patternset id="patternset.wm.adapter.sources">
<include name="src/*.java"/>
<exclude name=""/>
</patternset>
Within the javac task I ammend srcdir to:
srcdir="${proj.dir}"
But this is a fix! Why do my original settings not work?
basedir here is equal to your 'ant' subdir, which is probably why it doesn't work. Perhaps you want <project basedir=".."> at the top.

Eclipse/Ant Making a jar in version 1.9, even though everything seems to be set for 1.8

I have a project I build using ant and eclipse which I'd like to have be compatible with Java 8.
When I try running the jar using Java 8, I get the following error:
$ /usr/lib/jvm/java-8-openjdk-amd64/bin/java -jar hypnos.jar
Error: A JNI error has occurred, please check your installation and try again
Exception in thread "main" java.lang.UnsupportedClassVersionError:
org/joshuad/musicplayer/MusicPlayerUI has been compiled by a more recent version of
the Java Runtime (class file version 53.0), this version of the Java Runtime only
recognizes class file versions up to 52.0
So it appears that it's compiling using Oracle's Java 9 (which is installed on my system) but I can't find the place in eclipse where this is designated. Everything I see seems to suggest that we're using java 8 (screenshots below).
I feel like I must be missing something, but I can't figure out where. What do I have to do to get this jar to be compatible with Java 8 / class file version 52.0?
Project > Properties > Java Build Path
Project > Properties > Java Compiler
Window > Preferences > Compiler
Right Click Ant Build File > Run As > External Tools Configuration
Ant Build File
<project name="Hypnos Music Player" default="compile" basedir=".">
<property name="src" location="src"/>
<property name="build" location="build"/>
<property name="jarFile" location="hypnos.jar"/>
<path id="class.path">
<fileset dir="lib">
<include name="**/*.jar" />
</fileset>
<pathelement location="${jarFile}" />
</path>
<target name="init">
<tstamp/>
<mkdir dir="${build}"/>
</target>
<target name="compile" depends="init" description="compile the source">
<javac fork="yes" includeantruntime="false" srcdir="${src}" destdir="${build}">
<classpath refid="class.path" />
</javac>
</target>
<target name="jar" depends="compile" description="Create a jar.">
<jar destfile="${jarFile}" basedir="${build}">
<manifest>
<attribute name="Main-Class" value="org.joshuad.musicplayer.MusicPlayerUI" />
<attribute name="Class-Path" value="lib/commons-cli-1.4.jar lib/jaad-0.8.4-nospi.jar
lib/jaudiotagger-2.2.6-SNAPSHOT.jar lib/jflac-1.2.jar
lib/jl1.0.1.jar lib/jogg-0.0.7.jar lib/jorbis-0.0.15.jar
lib/vorbisspi1.0.3.jar" />
</manifest>
</jar>
</target>
</project>
What is your $JAVA_HOME environment variable?
I recently had a similar problem and it turns out that Ant seems to ignore many settings provided by Eclipse and only uses $JAVA_HOME. I discovered this by placing the following in build.xml:
<target name="jdk_version_validation">
<echo message="Java Version: ${java.version}" />
<echo message="Java home: ${java.home}" />
<echo message="JAVA_HOME: ${env.JAVA_HOME}" />
<echo message="JAVACMD: ${env.JAVACMD}" />
</target>
Which got me this:
jdk_version_validation:
[echo] Java Version: 1.7.0_80
[echo] Java home: C:\Program Files\Java\jre7
[echo] JAVA_HOME: C:\Program Files\Java\jdk1.8.0_131\
[echo] JAVACMD: C:\Program Files\Java\jdk1.7.0_80\bin
I found that the only sure way to predict which version Ant will produce is by brute-forcing the following in build.xml:
<property name="ant.build.javac.source" value="1.7"/>
<property name="ant.build.javac.target" value="1.7"/>
So, my recommendation to you: Try placing in your build.xml the following:
<property name="ant.build.javac.source" value="1.8"/>
<property name="ant.build.javac.target" value="1.8"/>
Update, for the benefit of those with a variation of the problem described in the OP, to eliminate any possibility of "Eclipse fooling Ant":
Go to Window > Preferences > Java > Installed JREs and try leaving only the target JRE, making sure it is the one inside the JDK folder (not the sibling one directly under the Java folder).
Go to Project > Properties > Java Compiler and check Enable project specific settings, making sure that the Compiler compliance level is set to the target JRE java version.
In addition to what datv said above, I'll add from experience:
Ant's javac, java, and junit tasks have a jvm attribute that determines how to run the JDK/JRE, if fork="true". Unfortunately, it only sets the raw launcher, and doesn't change all the other settings. When I set the jvm attribute to the full path of a Java 8 JRE, turning on ant's -d switch shows that it is being used as the executable, but followed by
'-Djava.specification.version=9'
'-Dsun.boot.library.path=C:\Java\x64\jdk-9.0.1\bin'
'-Djava.home=C:\Java\x64\jdk-9.0.1'
'-Djava.runtime.version=9.0.1+11'
'-Djava.version=9.0.1'
'-Djava.library.path=...;C:\Java\x64\jdk-9.0.1\bin;.....
'-Djava.vm.version=9.0.1+11'
'-Djava.class.version=53.0'
all of which take precedence.
It turns out that Ant cares more about the JAVACMD environment variable when it starts up. In the situation above, our build framework had set JAVACMD to C:\Java\x64\jdk-9.0.1\bin\java, and the individual jvm= and executable= attributes on specific tasks were... not "ignored" really, merely pointless and misleading.

Could not find or load main class- I cannot run my .jar file created from Ant

I have been struggling with this for two days now. I created a very simple HelloWorld class to test if I can get this working but I was not able to.
I get Error- Could not find or load main class...
It works from Eclipse or run task from the script. But double-clicking .jar or running it from CMD gives me the error. What are some possible reasons for this error? Class-path? environmental variables? directory structure? Please help!
package com.hellojava;
public class HelloWorld {
public static void main(String[] args)
{
System.out.println("Hello World!");
}
}
My build.xml
<project name="TestProject" basedir="." default="main">
<property name="src.dir" value="src"/>
<property name="build.dir" value="build"/>
<property name="classes.dir" value="${build.dir}/classes"/>
<property name="jar.dir" value="${build.dir}/jar"/>
<property name="main-class" value="com.hellojava.HelloWorld"/>
<target name="clean">
<delete dir="${build.dir}"/>
</target>
<target name="compile">
<mkdir dir="${classes.dir}"/>
<javac srcdir="${src.dir}" destdir="${classes.dir}"/>
</target>
<target name="jar" depends="compile">
<mkdir dir="${jar.dir}"/>
<jar destfile="${jar.dir}/${ant.project.name}.jar" basedir="${classes.dir}">
<manifest>
<attribute name="Main-Class" value="${main-class}"/>
</manifest>
</jar>
</target>
<target name="run" depends="jar">
<java jar="${jar.dir}/${ant.project.name}.jar" fork="true"/>
</target>
<target name="clean-build" depends="clean,jar"/>
<target name="main" depends="clean,run"/>
run:
[java] Hello World!
main:
BUILD SUCCESSFUL
Total time: 5 seconds
Manifest-Version: 1.0
Ant-Version: Apache Ant 1.9.2
Created-By: 1.7.0_11-b21 (Oracle Corporation)
Main-Class: com.hellojava.HelloWorld
**Edited:
java -jar TestProject.jar works but javaw -jar TestProject.jar does not. However, I solved the problem- see answer I posted.
The usual syntax to run a jar file is java -jar TestPractice.jar.
If this fails, some analysis is required.
Open your jar file with WinZip, and extract to a temporary directory.
Check that HelloWorld.class file is present, and in the correct directory.
Check that there is a META-INF/MANIFEST.MF file, and it contains a line reading
Main-Class: com.hellojava.HelloWorld I don't see anything in your Ant script that would generate this file.
If all that is OK, your program will run.
For more information, take a look at how to create a bundled runnable jar using Ant
Problems Solved!
I had to change the registry to a correct Java version that I am using. It was set to the previous version of Java that was in my comp.
I can run .jar files through cmd, but I cannot double click them
Also, I noticed that javaw -jar file.jar does not work for me while java -jar file.jar works.
I changed the program that opens .jar files by "open with" and select java instead of javaw. This now solves the problem- I do have one more problem though. I wonder why javaw does not work but for now I'm happy this is working :)
I had the same problem. Application worked fine on my PC but on some PCs I get the same error. Try to upadte Java runtime on machines where your app donesn't work. For example I worked with JRE7 and on PC where app was not working there was Java 1.6 installed.

unable to read system properties inside jar which is in classpath of another java project

My project has a jar in classpath with some utilities in it. When I run the program using ant on Red Hat linux, it's unable to read the system properties using System.getProperty(key), but it works perfectly fine when ran on Ubuntu.
Any suggestion to why this might be happening is appreciated.
ant target
<target name="test">
<property environment="env"/>
<echo message="PATH_ROOT set => ${env.PATH_ROOT}" />
<echo message="CUSTOM_REPORT_PATH set => ${env.CUSTOM_REPORT_PATH}" />
<testng classpathref="compile.classpath" haltOnfailure="false" outputDir="${testng.output.dir}/xxx">
<sysproperty key="PATH_ROOT" value="${env.PATH_ROOT}"/>
<sysproperty key="CUSTOM_REPORT_PATH" value="${env.CUSTOM_REPORT_PATH}"/>
<xmlfileset dir="src/com/xxx" includes="suite.xml" />
</testng>
</target>
================
Guys it was a silly mistake. The framework was not executing the file which had the statements to fetch system variables as the listener was missing in testng suite which is a must for testng to invoke that

Building a Java project which does not have javadoc.exe

I need to use some java library in MATLAB. I did use it in MATLAB (Unix version), but now I have to do it on MATLAB (Win64), as well. As far as I know, this is a project developed in unix. I did simply compile it using
ant
command in Ubuntu.
Since I'm a beginner to java compiling, I thought of installing ant on Windows and running
c:\java\ant\bin\ant
command in the path. However when I do this, it says that:
build.xml:22: Javadoc failed: java.io.IOException:
Cannot run program "javadoc.exe": CreateProcess error=2
Here is the build.xml file:
<project default="all">
<target name="all" depends="doc,jar" />
<target name="compile">
<mkdir dir="build/classes" />
<mkdir dir="build/examples" />
<javac includeantruntime="false" debug="on" srcdir="src/main/java"
destdir="build/classes" target="1.5" />
<javac includeantruntime="false" srcdir="src/main/example/"
classpath="build/classes" destdir="build/examples" />
</target>
<target name="jar" depends="compile">
<jar destfile="dist/java_websocket.jar">
<fileset dir="build/classes" includes="**/*.class" />
</jar>
</target>
<target name="doc">
<delete dir="doc" />
<javadoc sourcepath="src/main/java" destdir="doc" />
</target>
<target name="clean">
<delete dir="build" />
</target>
</project>
When I try to search about this issue, I've come up with so many answers that are about Java Eclipse compilations, which I'm a total stranger to.
If you use maven to compile these projects, thats fine. I just need some guidance.
Here is the github link of the java project that I'm trying to compile (on Win64), if you would like to see any further information: https://github.com/TooTallNate/Java-WebSocket
And here is the build instructions (I'm guessing for Unix): https://github.com/TooTallNate/Java-WebSocket/blob/master/README.markdown
Note that I sadly use Windows 8 (x64) and I currently have jdk1.7.0_21 installed on the path "C:\Program Files\Java\jdk1.7.0_21" which is also the system variable JAVA_HOME. ANT_HOME variable is "c:\java\ant"
Please take a moment from your precious time and help me to find a solution, if you have any knowledge about this issue. Any help will be greatly appreciated.
Best regards.
If it compiles on ubuntu with just ant, there is no need for either maven or eclipse. The problem is likely that the PATH variable does not include the jdk. Look into c:\Programs\Java and find the javadoc.exe. Its directory must go into PATH. To set the dir in PATH, go to (roughly, have no windows around) computer / properties / advanced / environment variables / system and find the PATH variable. Append the directory you found by using semicolon as the separator.

Categories