I have the following file structure:
ServerCode <- src , libs, bin
I am trying to compile all the code in src. Src has a couple of .java files at the top level and sub-directories. libs contains all the .jar files which are required to build my project.
I wrote the following build.xml but when I try to compile it, the compiler throws errors cannot find symbol errors for the libraries I am including.
<project default="compile">
<target name="compile">
<mkdir dir="bin"/>
<javac srcdir="src" destdir="bin" classpath="libs/*.jar">
</target>
</project>
Define class path to include all jars like this
<target name="compile" depends="" description="compile the java source files">
<javac srcdir="." destdir="${build}">
<classpath>
<fileset dir="${lib}">
<include name="**/*.jar" />
</fileset>
<fileset dir="${test_lib}">
<include name="**/*.jar" />
</fileset>
</classpath>
</javac>
I don't think you can use a pattern in the classpath attribute. I could be wrong about this. You should run ant in verbose mode (the -v option) to see how it's using your classpath attribute. I suspect it's passing it to javac as a literal string.
Here's what I do:
<javac target="${javac.target}" source="${javac.source}" destdir="${validator.output.dir}" debug="on"
nowarn="off"
memorymaximumsize="128m" fork="true">
<classpath refid="validator.module.production.classpath"/>
<src>
<dirset dir="Validator">
<include name="src"/>
</dirset>
</src>
</javac>
...
<path id="validator.module.production.classpath">
<fileset dir="Validator/lib/-validator" includes="**/*.jar"/>
</path>
Some of this code is generated by my IDE so it's a little verbose, but you get the idea.
Try this as mentioned at http://ant.apache.org/manual/using.html instead of giving classPath attribute alongwith javac
<classpath>
<pathelement location="libs/*.jar"/>
</classpath>
There are other ways also which you can glance thru the link mentioned above
Related
I try to send email using JavaMail and activation jars and compiling my project with ant.
Ant classpath looks like this:
<path id="classpath.test">
<pathelement location="lib/javax.mail.jar"/>
<pathelement location="lib/activation.jar"/>
<pathelement location="build/classes/"/>
</path>
Ant compile looks like this:
<target name="compile">
<mkdir dir="build/classes/"/>
<javac srcdir="src" destdir="build/classes/">
<classpath refid="classpath.test"/>
</javac>
<copy todir="build/classes/">
<fileset dir="src">
<exclude name="**/*.java"/>
</fileset>
</copy>
</target>
But when I try to execute this (also using ant), it throws
Exception in thread "main" java.lang.NoClassDefFoundError:
javax/mail/MessagingException
How can I fix this?
If you need to add a jar to the classpath to compile the code, then you try the following <javac> and it will look like this:
<javac srcdir="./src" destdir="./build/classes">
<classpath>
<pathelement path="lib/javax.mail.jar"/>
<pathelement path="lib/activation.jar"/>
</classpath>
</javac>
The problem was that dependencies (javax.mail.jar and activation.jar) were not properly included in jar created by ant, so I created a fat jar, and after doing so everything worked.
Currently, we define a path with
<path id="sources.production">
<pathelement path="src/module1"/>
<pathelement path="src/module2"/>
...
</path>
and then compile using
<javac ...>
<src refid="sources.production"/>
<classpath refid="classpath.production">
</javac>
Now we create some modified .java files (original files in src/module2) before this <javac> call and have put them before the other sources:
<path id="sources.production">
<pathelement path="generated-sources"/>
<pathelement path="src/module1"/>
<pathelement path="src/module2"/>
...
</path>
Unfortunately, the compile fails now because the original and the modified .java files are both fed to the javac task. How to exclude the original source files easily from the javac's sources without large charges?
you need an intermediate step which you copy all your source files to a new location and override them with generated ones, then use this new location as source folder for javac.
it may look something like this:
<path id="sources.4compile" location="all-sources" />
<target name="prepare-4compile">
<!-- clean -->
<delete dir="all-sources"/>
<mkdir dir="all-sources"/>
<copy todir="all-sources">
<fileset dir="src/module1"/>
<fileset dir="src/module2"/>
<fileset dir="generated-sources"/>
</copy>
</target>
<target name="compile" depends="prepare-4compile">
<javac ...>
<src refid="sources.4compile" />
<classpath refid="classpath.production" />
</javac>
</target>
other way, you may specify a fileset to javac disabling javac's default searching mechanism as suggested here.
I have a simple game implemented in eclipse. It consists of about 8 classes.
It is for my school assignment.
In the turn in specification, there is written:
"Send me all source codes, documentation and ant build file, which allows the project to be compiled and generate javadoc documentation".
I really do not understand how ant works. I googled some tutorials, but I cannot understand them either. I tried to generate build.xml file in eclipse, but the teacher said that this doesnt work either.
Could someone give me some simple steps or give me link to some really basic tutorial? Thanks for help.
This is the eclipse generated ant (export project as antbuildfile):
And it is kind of weird, because the class BasicPaint I deleted a long time ago.
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!-- WARNING: Eclipse auto-generated file.
Any modifications will be overwritten.
To include a user specific buildfile here, simply create one in the same
directory with the processing instruction <?eclipse.ant.import?>
as the first entry and export the buildfile again. --><project basedir="." default="build" name="Snakes_and_Adders">
<property environment="env"/>
<property name="debuglevel" value="source,lines,vars"/>
<property name="target" value="1.8"/>
<property name="source" value="1.8"/>
<path id="Snakes_and_Adders.classpath">
<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="build-subprojects,build-project" name="build"/>
<target name="build-subprojects"/>
<target depends="init" name="build-project">
<echo message="${ant.project.name}: ${ant.file}"/>
<javac debug="true" debuglevel="${debuglevel}" destdir="bin" includeantruntime="false" source="${source}" target="${target}">
<src path="src"/>
<classpath refid="Snakes_and_Adders.classpath"/>
</javac>
</target>
<target description="Build all projects which reference this project. Useful to propagate changes." name="build-refprojects"/>
<target name="BasicPaint">
<java classname="snakes_and_adders.BasicPaint" failonerror="true" fork="yes">
<classpath refid="Snakes_and_Adders.classpath"/>
</java>
</target>
<target name="Game">
<java classname="snakes_and_adders.Game" failonerror="true" fork="yes">
<classpath refid="Snakes_and_Adders.classpath"/>
</java>
</target>
<target name="NewGame">
<java classname="snakes_and_adders.NewGame" failonerror="true" fork="yes">
<classpath refid="Snakes_and_Adders.classpath"/>
</java>
</target>
<target name="PaintingExample">
<java classname="snakes_and_adders.PaintingExample" failonerror="true" fork="yes">
<classpath refid="Snakes_and_Adders.classpath"/>
</java>
</target>
Ant is used to perform tasks that are useful to build applications. You have tasks like <javac> <jar> etc.. To compile your classes and put them in a jar file.
I don't see why the build.xml generated file wouldn't work.. But you can take it as an example to understand how ant works. You can also adapt that build.xml file to make it work anywhere.
This tutorial looks well explained at first sight: http://www.javaworld.com/article/2076208/java-app-dev/automate-your-build-process-using-java-and-ant.html
I find that ant can be pretty complex easily, it'll take you time to understand it well but it's really doable.
I want to include external jar to my java project. I'm using ant. External .jar is in folder lib. My build.xml looks something like that:
<?xml version="1.0" encoding="UTF-8"?>
<project>
<path id="classpath">
<fileset dir="lib" includes="**/*.jar"/>
</path>
<target name="clean">
<delete dir="build"/>
</target>
<target name="compile">
<mkdir dir="build"/>
<javac srcdir="src" destdir="build" classpathref="classpath" />
</target>
<target name="jar">
<mkdir dir="trash"/>
<jar destfile="trash/test.jar" basedir="build">
<zipgroupfileset dir="lib" includes="**/*.jar"/>
<manifest>
<attribute name="Main-Class" value="com.Test"/>
</manifest>
</jar>
</target>
<target name="run">
<java jar="trash/test.jar" fork="true"/>
</target>
</project>
But it doesn't work. When I want to import something from the external .jar, there is an error after command ant compile: package com.something does not exist.. What should I edit to get it working?
Exact error:
Compiling 23 source files to xy/build
xy/src/com/Test.java:5: package com.thoughtworks.xstream does not exist
import com.thoughtworks.xstream.*;
^
1 error
You should try without the includes attribute:
<fileset dir="lib" />
And in the jar part you include the classes like this:
<zipgroupfileset includes="*.jar" dir="lib"/>
You can't put external libraries into a jar and expect the classloader to use those jars. Unfortunately this is not supported.
There are ant tasks like one jar that help you, to create a jar file, that contains everything you need.
This bit is from the background information of one jar:
Unfortunately this is does not work. The Java Launcher$AppClassLoader
does not know how to load classes from a Jar inside a Jar with this
kind of Class-Path. Trying to use
jar:file:jarname.jar!/commons-logging.jar also leads down a dead-end.
This approach will only work if you install (i.e. scatter) the
supporting Jar files into the directory where the jarname.jar file is
installed.
Another approach is to unpack all dependent Jar files and repack them
inside the jarname.jar file. This approach tends to be fragile and
slow, and can suffer from duplicate resource issues.
Other Alternative:
jarjar: Jar Jar Links is a utility that makes it easy to repackage Java libraries and embed them into your own distribution
I also use ant to include a number of dependency JARs in my JAR. My compile task looks like this. Perhaps something similar will work for you.
<target name="compile" depends="init">
<javac srcdir="${src}" destdir="${build}" includeantruntime="false">
<classpath>
<pathelement path="${classpath}" />
<fileset dir="${deps}">
<include name="**/*.jar"/>
</fileset>
</classpath>
</javac>
<copy todir="${build}">
<fileset dir="${src}" excludes="**/*.java"/>
</copy>
</target>
sometimes u can use jar contents directly, just unzip
<unzip src="/Developer-Java/mysql-connector-java/mysql-connector-java-5.1.22-bin.jar" dest="${build_dir}" />
I'm using build.xml to build my src. However it failed to generate class files without any error message. The full script is
<?xml version="1.0"?>
<project name="auxiliary" basedir="." default="dist">
<property name="src.dir" value="../auxiliary-src/com/nextbio/drugbank"/>
<property name="dist.dir" value="dist"/>
<property name="lib.dir" value="../jboss_config/common_app_jars"/>
<property name="temp.dir" value="temp"/>
<property name="foo_dist.dir" value="../foo/dist"/>
<path id="libs-classpath">
<fileset dir="${foo_dist.dir}">
<include name="foo.jar"/>
</fileset>
</path>
<target name="dist" depends="auxiliary-dist" />
<target name="auxiliary-cleanup">
<delete dir="${temp.dir}"/>
<delete dir="${dist.dir}"/>
<echo message="cleaned up. ${temp.dir}, and ${dist.dir} have been deleted."/>
</target>
<target name ="auxiliary-dist">
<delete dir="${temp.dir}"/>
<echo message="delete ${temp.dir}" />
<mkdir dir="${temp.dir}"/>
<javac destdir="${temp.dir}" source="1.6" target="1.6" debug="on" fork="true" memorymaximumsize="1024m">
<src path="${src.dir}"/>
<classpath>
<path refid="libs-classpath"/>
</classpath>
<include name="com/car/**"/> <!-- troubled line -->
</javac>
<!--<copy overwrite="true" todir="${temp.dir}">
<fileset dir="${src.dir}">
<exclude name="**/*.java"/>
<exclude name="**/*.sql"/>
<exclude name="**/*.txt"/>
</fileset>
</copy>
<delete dir="${dist.dir}"/>
<mkdir dir="${dist.dir}"/>
<jar destfile="${dist.dir}/auxiliary.jar" basedir="${temp.dir}"/> -->
</target>
There is no class file in ${temp.dir} after this step, and no error message. I double checked it, and found it is because of the "troubled line". I tried to add some files to the classpath. I don't know why it is wrong.
The source path should point to the root of the package tree. You make it point to a specific package inside the sources : ../auxiliary-src/com/nextbio/drugbank.
And in the javac task, you ask it to compile all the files matching the pattern com/car/**. That means that it will compile the Java source files in ../auxiliary-src/com/nextbio/drugbank/com/car or in a subdirectory. If that's the case, you have very unconventional package names.
I had the same problem.
My project complilated well but the classes there weren't in nowhere and It didn't have any error message.
My problem was the classpath. The eclipse wizard added EclipseLink 2.5.1 jars.
I removed it and the problem is gone.
I suggest make a simple HelloWord and remove all jars
reference from the classpath and try again.
I encountered this "ant, javac, compile" problem related with the classpath to.
No debug or verbose message shown.
This behavior appear because in classpath exists not compatible (superior) version jar packages and that cause no output classes.