I have an ANT build that compiles my Java project and builds a jar. An ANT target creates a manifest classpath for the jar. Everything works great.
Now, I would like to add a period '.' to the beginning of the manifest classpath so that the runtime working directory is on the classpath. How do I do that using manifestclasspath?
My manifestclasspath example:
<manifestclasspath property="foobar.manifest.classpath" jarfile="whocares.jar">
<classpath >
<pathelement path="."/>
<!-- :-( adds the working directory of the build. -->
<pathelement location="lib/some.jar"/>
<pathelement location="lib/someother.jar"/>
<fileset dir="${foobar.lib.dir}">
<include name="**/*.jar" />
</fileset>
</classpath>
</manifestclasspath>
<pathelement path="."/>adds the working directory of the build to my manifest classpath which is not what I want. I just want a period.
This is what I want the jar classpath to look like:
Class-Path: . lib/some.jar lib/someother.jar lib/blah.blah.blah.blah.b
lah.blah.jar /lib/and-on-and-on.jar
The key thing being the '.' as the first item in the classpath.
How to I make ANT add the literal '.' to the manifest class path?
Couldn't you just prepend manually :
<property name="theRealManifestClasspath" value=". ${foobar.manifest.classpath}"/>
Related
My project is as given below.
properties
lib
a.jar
b.jar
c.jar
d.jar
log4j.jar
build.xml
I have a java task which references a jar in the lib folder. It writes the output as per the config in log4j properties.
Now where do i place the log4j.properties & mention it in ant java task.
This is how i run the java task & include jars for my project.
<path id="proj.classpath">
<fileset dir="${basedir}/lib">
<include name="*.jar"/>
</fileset>
</path>
<target name="some-task">
<java classname="my.class.main"
classpathref="proj.classpath">
<arg value="some args"/>
</java>
</target>
In your java class, include this in the main method. Pass the file path of the log4j.properties
PropertyConfigurator.configure(args[0]);
Something like this
<java classname="my.class.main"
classpathref="proj.classpath">
<arg value="log4j.properties path"/>
<arg value="some args"/>
</java>
I want to add a file to a classpath from a fileset. How can I do this with Ant?
For example, let's say the fileset with id "my.fileset" contains:
dir1/subdir1/file1.jar, dir2/subdir2/file2.jar, dir3/subdir3/file3.jar
I know the name of the jar: "file2.jar". Now, I want to find the full filename and add it to a classpath (I assume this will require to use a regex like ".*file2.*)
I'm not sure I understand the question. Are you trying to dynamically add the file to the classpath, or are you trying to set a classpath in ant? In ant, once you set a property (like a classpath), you cannot change it.
To create a classpath in a normal build.xml do something like this:
<path id="compile.classpath">
<pathelement location="${foo.jar}"/>
<fileset dir="./bin">
<include name="*.jar"/>
</fileset>
<pathelement location="./lib"/>
<fileset dir="./lib">
<include name="**/*.jar"/>
</fileset>
</path>
This will add directories, or every jar in a bin folder, or even every jar in the ./lib sub-tree.
I have my jar files in Assignment2\lib folder and my build file is in Assignment2. The name of the jar file is Assignment1.jar The following is how I tried to compile my Assignment2 from build file through ant.
<project name="Assignment1" default="run" basedir=".">
<property name="classes" value="classes" />
<path id="project.class.path">
<pathelement location="src"/>
<pathelement path="${java.class.path}/"/>
<pathelement path="${additional.path}"/>
</path>
<path id="lib.jars">
<fileset dir="lib" includes="**/*.jar" />
</path>
......
<target name="compile" description="compaling java files with Assignment2">
<mkdir dir="build"/>
<javac srcdir="src" destdir="build" debug="on" failonerror="true">
<classpath refid="project.class.path"/>
<classpath refid="lib.jars"/>
</javac>
</target>
<Project/>
I am getting compile errors. I'm using windows. Is that the problem? Is there any way to compile?
Well,Everything looks good to me but, you can try the following to validate the same,
Check if the lib folder has all the necessary library jars required to compile you application
You can try assigning the relative path of the lib folder to a property and refer it in the classpath eg: Property lib value=".\Lib"
Try the following before you call the compile target to see the list of jars being included in the classpath
<pathconvert property="libjars" refid="lib.jars"/>
<echo>libjars is ${classpathProp}</echo>
hey guys thanx for the answers actually i found probelm. i was trying to use a jar file created by netbeans files. Apparently netbeans jar files can be used only by netbeans. at the end i made a new jar file of Assingment1 and used it. now everything is fine. Thanx again for the answers.
I need to ant java jar that is execute a jar file that has been created without Main-Class attribute.
The Documentation of ant Java task says that the jar file must have a Main-Class manifest. Is there any way?
Sorry for Typos earlier
Part of build.xml to create JAR - without Main-Class
<target name = "createJar" depends = "javac">
<jar destfile = "./sampleJar.jar"
basedir = "build/classes"
/>
</target>
Now i want to
<java jar />
this jar file by passing classname as some kind of arguments. Possible?
Try this:
<java classname="my.package.ClassWithMain">
<classpath>
<pathelement location="sampleJar.jar"/>
<pathelement path="${java.class.path}"/>
</classpath>
</java>
my.package.ClassWithMain is the fully qualified name of the class with a main method, the classpath should point to the jar containing the jar.
Instead of running the jar file you would instruct Ant's java task to use the jar file as classpath and run the main class directly. E. g. using a task like the following, taken from the documentation of the java task:
<java classname="test.Main">
<classpath>
<pathelement location="./sampleJar.jar"/>
<pathelement path="${java.class.path}"/>
</classpath>
</java>
Of course, you will have to adjust the main class'es name test.Main for your use case.
I got a TestNG test, that works perfectly fine on it's own. Somewhere in it I got:
URL fileName = this.getClass().getClassLoader().getResource("config.properties");
I'm trying to run the same test using Ant:
<testng outputDir="${resultsFolder}" haltOnFailure="false">
<classpath>
<fileset dir="./src/">
<include name="**/*.properties"/>
</fileset>
-----------------------------...--------------------
</classpath>
<classfileset dir="correct path here" includes="**/*.class" />
</testng>
I can see in debug mode that the config.properties is in the classpath. But the line at the top can not find it, it is null.
EDIT: I solved it.The critical line actually does not search for the file in the classpath directly, it searches IN the files/folders. So, this solved my problem:
<pathelement location="src/"/>
Thanks for the help.
try to replace your <classpath>...</classpath> with this:
<classpath>
<pathelement path="./src"/>
</classpath>
In order for JVM to find the config.properties the parent directory of config.properties should be in classpath.