ant java jar without main class - java

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.

Related

Ant Javac compile subpackage class files to the parent src directory

I would like Ant 1.9 to be able to compile servlet classes to the parent src directory, but the file system has the files in packages. No, the packages are not declared in the servlet files.
The code is deployed with all the servlets in the same directory. I supposed I can create an ant copy command to do this, but I would prefer there be an ant javac solution.
Manually this would be 'javac -d .. *.java' after changing to each subdirectory, not including the files already in the default package. This is highly irregular, but I cannot change the how the packages are defined nor can I change the urls from which the code is executed from.
<target name="compileServlet" description="Compiles Java source files.">
<javac srcdir="${servlets.dir}" destdir="$(servlets.dir}" debug="true" encoding="ISO-8859-1" source="1.7" target="1.7" failonerror="true">
<classpath path="${env.CLASSPATH}" />
<include name="**/*.java"/>
</javac>
</target>
Right now when I run this ant build.xml, no class files are generated. Any thoughts how I can solve this issue?
If you have source files in multiple root directories and want to compile them all into a single root directory, use the <src> element instead of the srcdir attribute.
Here is the example shown in the documentation:
<javac destdir="${build}"
classpath="xyz.jar"
debug="on">
<src path="${src}"/>
<src path="${src2}"/>
<include name="mypackage/p1/**"/>
<include name="mypackage/p2/**"/>
<exclude name="mypackage/p1/testpackage/**"/>
</javac>

Include log4j in Ant Java task

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>

ant cannot identify my jar files at all

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.

Apache ANT manifest classpath literal

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}"/>

How to add one folder in classpath for ant script?

I need to add a folder in current classpath for ant script that I have written for running java files. How can it be done?
You could add it as an attribute
<java classpath="${extraDir}"
classname="pkg.Class">
...
</java>
Or using the nested <classpath> tag:
<java classname="pkg.Class">
<classpath>
<pathelement path="${extraDir}"/>
</classpath>
</java>
See the documentation for the Java task.
i added the following line in the tag of the task and it ran successfully.
<pathelement path="C:\JunitTest\folderIsHere"/>
and after this the script ran successfully.

Categories