I have a simple class, FabricClient, in the package client, with a public static void main(). This package is in the 'bin' directory of my project. From the bin directory, on the command line, I can execute the main in this class as follows:
user#sys /c/projects/myproject/bin
$ java -classpath .;c:\dir\my.jar client.FabricClient
I want to execute my ant build.xml file from the parent directory (/c/projects/myproject) from where I run the java command by hand. I've tried using the dir attribute as follows:
<target name="run-client">
<java classname="client.FabricClient"
classpath="${classpath}"
fork="true"
failonerror="true"
dir="./bin">
</java>
</target>
My classpath variable does have the current directory as well as the directory of my.jar as used on the command line (my ant command to compile the class works fine with the same variable)
Here are the settings used to generate the classpath
<property name="my.jar" location="c:\dir\my.jar" />
<property name="classpath" location=".:${my.jar}:." />
I'm getting the typical java.lang.NoClassDefFound Error: client/FabricClient
I'm using Ant version 1.7.0
If I move the build.xml file into bin and remove the dir attribute, it works fine. Can you run java from a directory other than the local directory? I thought this was what the dir attribute did.
Paths in ant are usually referred like this:
<java classname="client.FabricClient"
fork="true"
failonerror="true"
dir="./bin">
<classpath>
<pathelement location="C:/dir/my.jar"/>
</classpath>
it is more readable and reliable
Your classpath should contain the file my.jar, not the directory where my.jar is.
Related
I have some code that I revised.
When I try to just run Ant in the directory it fails with missing classes. I can specify the location to the existing classes by using the -lib option to ant. The compile then works fine, however dist ZIP file that is created appears to have missing libraries, as when I try to run it, I see errors relating to missing classes which are the classes that I specified with the -lib option, so this is probably due to the way I have used the -lib option.
How can I force the regular Ant command to include the additional classes specified with the -lib command?
You can write a target that will copy your lib directory/files in your zip file.
Let's say create a temp dir then copy your files then execute target for copying lib directory and then zip temp dir.
<target name="copyLib">
<copy todir="${temp.dir}">
<fileset dir="${lib.dir}">
<include name="*.jar" />
</fileset>
</copy>
</target>
Update paths and call this target into your create zip target.
Compiler task could look like this:
<javac srcdir="${base}/src"
destdir="${base}/classes"
classpath="${base}/lib">
</javac>
And the zip task could look like this:
<zip
destfile="${base}/dist.jar"
basedir="${base}/classes"
includes="..."
excludes="...">
</zip>
So sources are compiled in classes and zipped in a jar, but libraries used for compile are not included in the jar, they are runtime dependencies.
I would suggest that you declare your paths at the top of your ANT scripts as follows, using a fileset.
<path id="build.path">
<fileset dir="lib" includes="*.jar"/>
</path>
Less error prone compared to forcing users to specify the correct "-lib" parameter.
Finally the same fileset can then be used to include the same jars inside the zip file you're creating:
<zip destfile="${dist.dir}/mycode.zip">
..
..
<fileset dir="lib" includes="*.jar"/>
</zip>
This question already has answers here:
javac option to compile all java files under a given directory recursively
(10 answers)
Closed 8 years ago.
I'm using Eclipse & Tomcat7 in window7 platform, I have configured project in eclipse also. Usually we run the single java file using CMD.
But I want to compile and run the entire java code through command prompt.
I've a many structures in single src folder like E:\proj\src\com\mycode.Inside mycode folder there are 7 sub-folder are available & each sub-folder have many .java files & inner-sub-folders.
For Example:
E:\proj\src\com\mycode\dto\mail.java,E:\proj\src\com\mycode\dto\sms.java
E:\proj\src\com\mycode\dto\security\securityFile.java
The above same pattern other folders have java files.so I need to compile & run entire java files including sub-folders & inner-sub-folders USING COMMAND PROMPT.
thanks in advance,
I'll make some (hopefully reasonably safe) assumptions about how your code is structured:
you have a main program (I call it com.mycode.dto.Main below),
it has compile-time dependencies on the other files (you're not using reflection or whatever),
your source files match the package structure (com.foo.Bar is in E:\proj\src\com\foo\Bar.java).
In that case you can do:
javac -d <destination> -sourcepath E:\proj\src E:\proj\src\com\mycode\dto\Main.java
and then the compiler will traverse the file dependencies automatically and output the class files to destination.
If you have sources in multiple hierarchical directory, you may use ant.
create a build.xml file in the root of your project directory.
<project name="MyProject" default="dist" basedir=".">
<description>
simple example build file
</description>
<!-- set global properties for this build -->
<property name="src" location="src"/>
<property name="build" location="build"/>
<property name="dist" location="dist"/>
<target name="init">
<!-- Create the time stamp -->
<tstamp/>
<!-- Create the build directory structure used by compile -->
<mkdir dir="${build}"/>
</target>
<target name="compile" depends="init"
description="compile the source " >
<!-- Compile the java code from ${src} into ${build} -->
<javac srcdir="${src}" destdir="${build}"/>
</target>
<target name="dist" depends="compile"
description="generate the distribution" >
<!-- Create the distribution directory -->
<mkdir dir="${dist}/lib"/>
<!-- Put everything in ${build} into the MyProject-${DSTAMP}.jar file -->
<jar jarfile="${dist}/lib/MyProject-${DSTAMP}.jar" basedir="${build}"/>
</target>
<target name="clean"
description="clean up" >
<!-- Delete the ${build} and ${dist} directory trees -->
<delete dir="${build}"/>
<delete dir="${dist}"/>
</target>
</project>
Install ant on your machine, be sure to have its bin directory in your path, then you can just run
ant -f build.xml
Of course this is just a starting point (ant offers several interesting options and let you finetune all the aspects of your build/packaging).
the sample build.xml file was taken from here
Try using wildcard:
javac *.java
You could use various parameters as per need basis that comes in handy with javac:
Usage: javac <options> <source files>
where possible options include:
-g Generate all debugging info
-g:none Generate no debugging info
-g:{lines,vars,source} Generate only some debugging info
-nowarn Generate no warnings
-verbose Output messages about what the compiler is doing
-deprecation Output source locations where deprecated APIs are used
-classpath <path> Specify where to find user class files and annotation processors
-cp <path> Specify where to find user class files and annotation processors
-sourcepath <path> Specify where to find input source files
-bootclasspath <path> Override location of bootstrap class files
-extdirs <dirs> Override location of installed extensions
-endorseddirs <dirs> Override location of endorsed standards path
-proc:{none,only} Control whether annotation processing and/or compilation is done.
-processor <class1>[,<class2>,<class3>...]Names of the annotation processors to run; bypasses default discovery process
-processorpath <path> Specify where to find annotation processors
-d <directory> Specify where to place generated class files
-s <directory> Specify where to place generated source files
-implicit:{none,class} Specify whether or not to generate class files for implicitly referenced files
-encoding <encoding> Specify character encoding used by source files
-source <release> Provide source compatibility with specified release
-target <release> Generate class files for specific VM version
-version Version information
-help Print a synopsis of standard options
-Akey[=value] Options to pass to annotation processors
-X Print a synopsis of nonstandard options
-J<flag> Pass <flag> directly to the runtime system
I am new to apache ant and I am currently working on an apache Ant project. I Just started out, imported the project into workspace and tried to run the build.xml. I added all the libraries that come with the original project to the build path. I am having the following problem. Please someone else wrote the code and I am supposed to improve it. The directories this is all about exist in the project directory.
BUILD FAILED
C:\workspace\MyApp\build.xml:83: srcdir "C:\workspace\MyApp\${compile.javac.srcdir}" does not exist!
The error code is referencing the following part of the build.xml file
<target name="compile.default" depends="init">
<javac fork="yes" srcdir="${compile.javac.srcdir}" destdir="${compile.javac.destdir}" includes="${compile.javac.include}" excludes="${compile.javac.exclude}" classpath="${compile.javac.classpath}" debug="${compile.javac.debug}" optimize="${compile.javac.optimize}" deprecation="${compile.javac.deprecation}" verbose="${compile.javac.verbose}">
</javac>
<copy todir="${compile.javac.destdir}">
<fileset dir="${compile.javac.srcdir}" includes="${compile.copy.include}" excludes="${compile.copy.exclude}"/>
</copy>
</target>
<target name="compile" depends="init,compile.default" description="Compile all java source">
</target>
<!--+++++++++++++++-->
<!-- lib target(s) -->
<!--+++++++++++++++-->
<target name="lib.default" depends="init,compile">
<xmlbean schema="config/schemas/validate/1.0/validate.xsd" destfile="lib/glx-beans.jar" classpath="lib/xbean.jar:lib/jsr173_1.0_api.jar" />
<jar jarfile="${lib.filename}">
<fileset dir="${lib.srcdir}" excludes="${lib.exclude}" />
</jar>
</target>
<target name="lib" depends="init,compile,lib.default" description="Create all Project Libraries">
</target>
Would you please tell me what I am missing?
The ${compile.javac.srcdir} isn't defined. There are a few possibilities:
This is defined not in the build.xml, but in some sort of properties file. See if you have something like <property file="..."/> in your build script. My recommendation is to have all properties defined in the build.xml file, and use a properties file to override those settings. This way, the only build file that a developer needs in the build.xml file and doesn't have to worry about setting up a separate build.porperties file.
This is defined in the build.xml file under a particular task, but you forgot to say that your target where you use thisis dependent upon this task.
One of the things you can do is use the -d parameter when running Ant. I run the following command when running Ant with the -d parameter:
$ and -d 2>&1 | tee ant.out
I can then look at ant.out and see if somehow I didn't define that particular property. Maybe I had the wrong capitalization or misspelled the property name. For example, it's very likely I'll define the property as copmile.javac.srcdir because I don't know how to spell. Looking at the -d output can quickly point these types of errors out.
By the way, you shouldn't have all of your tasks dependent upon init since they're dependent upon compile.default anyway:
<target name="compile.default" depends="init">
....
</target>
<target name="compile" depends="compile.default">
....
</target>
<target name="lib" depends="compile,lib.default">
....
</target>
If I run the target lib, it will see compile is dependent upon compile.default which is dependent upon init. Thus, your build will run init, then compile.default, then compile, then 'lib.defaultand finallylib`.
If the init task is just setting up properties, you can do that outside of any task. Then, these properties will be setup before any task is executed. This way, they're not forgotten. If your init is also creating directories, you may want to move those <mkdir/> tasks in front of the task where that directory is used. For example, you may want to make the destdir uses in javac before the <javac/> task.
I find assigning default properties outside of any task, and creating directories before they are needed to simplify the build.xml. Plus, you're not creating a whole flock of unused directories if the user is merely compiling and not packaging the jar/war/etc.
I am trying to implement my build.xml file so I can compile and create a Jar using ANT. In order to run some JUnit tests I need to first manually start up a custom Server on port 7777. However, I am having trouble starting the server up using XML. This is my code so far:
<property name="server" location="cs.hw4.Server"/>
<target name="run">
<java fork="true" failonerror="yes" classname="${server}">
<classpath refid="cs.hw4.classpath"/>
<arg line="7777"/>
</java>
</target>
I run the build.xml and I get the: Error: Could not find or load main class F:...
I am finding that the error is trying to find class in the directory:
...\cs.hw4\cs.hw4.Server
When it is actually in:
...\cs.hw4\ bin \cs.hw4.Server
The funny thing is that the classpath refid= "cs.hw4.classpath" includes the "bin" folder.
Any ideas?
You can try this way. Right click on the ant build file and run as you should see a window that pops out with the Classpath and Jar file path. Remove that and add it manually. This time you should see no conflicts.
I have got involved in a project. This project uses ant which is not something I am comfortable with. I have checked out the source code and tried running ant on the most outer directory.
Running 'ant' in commando prompt takes 1 sec and I get a BUILD SUCCESFULL message. If I run 'ant all' I get a
BUILD FAILED. Java.io.IOExceptio: Cannot run program "ant": CreateProcess=2, the system cannot find the file specified and then a long stacktrace.
Most of the people on the project runs OS-X while I use Windows XP.
Any help or information is appreciated :)
EDIT:
<target name="-all-submodules">
<subantlight target="all">
<filelist refid="ordered_build_files"/>
</subantlight>
</target>
In another xml file
<macrodef name="subantlight">
<attribute name="target"/>
<element name="files" optional="no" implicit="true" description="Filessets/lists of build files"/>
<sequential>
<apply executable="ant" failonerror="true">
<arg value="-f"/>
<srcfile/>
<arg value="#{target}"/>
<files/>
</apply>
</sequential>
</macrodef>
This is what throws IOException when it hits the line with "apply executeable..".
UPDATED EDIT:
If i set the absolute path like this
<macrodef name="subantlight">
<attribute name="target"/>
<element name="files" optional="no" implicit="true" description="Filessets/lists of build files"/>
<sequential>
<apply executable="MyAbsolutePathHereToAnt.bat" failonerror="true">
<arg value="-f"/>
<srcfile/>
<arg value="#{target}"/>
<files/>
</apply>
</sequential>
</macrodef>
Everything works.
I have set ANT_HOME to my ant directory. I have set my JAVA_HOME to Java JDK directory. In my PATH I have set %ANT_HOME%\bin;%JAVA_HOME%\bin
Calling echo %ANT_HOME% produces the right path.
I can't see what I am during wrong here.
ant with no attributes calls the default target on the build.xml file on the curent path. 'ant all' will call the 'all' target on the same build file.
First - double check the default ant target - is it 'all' or something different? I guess, the default target is not 'all' in your case and the 'all' build includes a build target, that itself calls ant. And this causes the problem.
Hard to tell from here, but scan the build file for an <ant> task inside some <target>. The IO error smells a bit like a violation of user access rights or missing files near/within this <ant> task.
EDIT
the build.xml starts with something like
<project name="Name" default="compile" basedir="/src">
The 'default' attribute names the default target. If the attribute is missing, all top level targets are executed (since ant 1.6) which should include all in your case.
If it works 'for everyone else' then 'everyone else' might have a different environment. Have a look at the environment variable ant references in the script (like 'env.JAVA_HOME' and so on) and compare with the actual environment. Maybe you find a broken path.
Do you have the bin directory of your Ant installation in your PATH? If not, then add it.
It looks like the all target tries to execute Ant (recursively) but can't find it.