Ant jar file for swing program - ant

<?xml version="1.0" ?>
<project name="javaGui" default="execute">
<target name="init" depends="clean">
<mkdir dir="build/classes" />
<mkdir dir="dist" />
</target>
<target name="compile" depends="init">
<javac srcdir="src" destdir="build/classes" />
</target>
<target name="execute" depends="compile">
<java classname="Swing" classpath="build/classes" />
<jar destfile="dist/final.jar" basedir="build/classes" />
</target>
<target name="clean">
<delete dir="build" />
<delete dir="dist" />
</target>
This is ant script to generate jar file.problem is those code will generate jar but when i click on that jar it is not opening means it not showing any GUI.
im new to this please let me know what is going wrong.
javaGUI is project and Swing is class name

jar file will not open in GUI. You need to run jar file from console.
Go to dist directory -> run this command:
$ java -jar final.jar [optional parameters]
For more details : see this reference
UPDATE
Instead of giving ant from target execute, try this :
<target name="jar">
<mkdir dir="build/jar"/>
<jar destfile="build/jar/HelloWorld.jar" basedir="build/classes">
<manifest>
<attribute name="Main-Class" value="Swing"/>
</manifest>
</jar>
</target>
<target name="run">
<java jar="build/jar/HelloWorld.jar" fork="true"/>
</target>

Related

Ant build.xml not functioning properly

Below is a simplified versin of a build.xml for a Java project. It completes "build" correctly (creates the correct .class files) and prints out "Finishing build". It does not, however, print out "Starting jar". What am I not understanding? The target "jar" depends on "build", so it should be run next.
Running it with target release.
<?xml version="1.0"?>
<project name="Project" basedir="." default="release">
<!-- directories -->
<property name="src.dir" location="src/main/java"/>
<property name="cls.dir" location="private/classes"/>
<property name="lib.dir" location="lib"/>
<property name="jar.name" value="${ant.project.name}-${jar.ver}.jar"/>
<target name="clean" description="Delete all generated files">
<delete dir="${cls.dir}"/>
<delete dir="${lib.dir}"/>
</target>
<target name="build" depends="clean">
<mkdir dir="${cls.dir}"/>
<javac
destdir="${cls.dir}"
nowarn="off"
fork="yes"
debug="on">
<classpath>
<path path="${run.classpath}"/>
</classpath>
<src path="${src.dir}"/>
</javac>
<echo message="Finishing build"/>
</target>
<target name="jar" depends="build">
<echo message="Starting jar"/>
<mkdir dir="${lib.dir}"/>
<jar destfile="${lib.dir}/${jar.name}">
<fileset dir="${cls.dir}"/>
<fileset dir="${src.dir}" includes="**/*.properties"/>
<fileset dir="${src.dir}" includes="**/*.xml"/>
</jar>
</target>
<target name="release" depends="jar" description="Entry point">
</target>
</project>
Update the release target as follows to note that release depends on build then jar. i.e. depends="build,jar"
i.e.
<target name="release" depends="build,jar" description="Entry point">
<echo message="release ..."/>
</target>

when I run Ant in cmd it is showing me error of build.xml not exist while I have build.xml file in my project folder

I am a selenium user trying to generate xslt reports using Ant, but when I run Ant in cmd it is showing me error of build.xml not exist while I have build.xml file in my project folder.
I am using eclispe juno on windows 7 and and kept the build.xml file under the project.
I have java JDK1.7 on my machine and I have already set the environment variables(Java and ant both) as per instructions given on apache.org
Ant version is apache-ant-1.9.1
I have imported all necessary jar files (selenium + maven +saxon + all required for xslt report through ant) in my project in eclipse.
When I am trying to run ant through cmd it is showing me this error:-
BUILD FAILED
D:\Projects\Project\Selenium\Workspace\build.xml:70: Compile failed; see the compiler error output for details.
Below is my build.xml file:-
<project name="Plumslice" default="usage" basedir=".">
<property environment="env"/>
<property name="ws.home" value="${basedir}"/>
<property name="ws.jars" value="D:\All jars"/>
<property name="test.dest" value="${ws.home}/build"/>
<property name="test.src" value="${ws.home}/src"/>
<property name="ng.result" value="test-output"/>
<!--target name="start-selenium-server">
<java jar="${ws.home}/lib/selenium-server.jar"/>
</target-->
<target name="setClassPath" unless="test.classpath">
<path id="classpath_jars">
<fileset dir="${ws.jars}" includes="*.jar"/>
</path>
<pathconvert pathsep=":"
property="test.classpath"
refid="classpath_jars"/>
</target>
<target name="init" depends="setClassPath">
<tstamp>
<format property="start.time" pattern="MM/dd/yyyy hh:mm aa" />
</tstamp>
<condition property="ANT"
value="${env.ANT_HOME}/bin/ant.bat"
else="${env.ANT_HOME}/bin/ant">
<os family="windows" />
</condition>
<taskdef name="testng" classpath="${test.classpath}"
classname="org.testng.TestNGAntTask" />
</target>
<!-- all -->
<target name="all">
</target>
<!-- clean -->
<target name="clean">
<delete dir="${test.dest}"/>
</target>
<!-- compile -->
<target name="compile" depends="init, clean" >
<delete includeemptydirs="true" quiet="true">
<fileset dir="${test.dest}" includes="**/*"/>
</delete>
<echo message="making directory..."/>
<mkdir dir="${test.dest}"/>
<echo message="classpath------: ${test.classpath}"/>
<echo message="compiling..."/>
<javac
debug="true"
destdir="${test.dest}"
srcdir="${test.src}"
target="1.7"
classpath="${test.classpath}">
</javac>
<copy todir="${test.dest}">
<fileset dir="${test.src}" excludes="**/*.java"/>
</copy>
</target>
<!-- build -->
<target name="build" depends="init">
</target>
<!-- run -->
<target name="run" depends="compile">
<testng classpath = "${test.classpath}:${test.dest}" suitename = "suite1" >
<xmlfileset dir="${ws.home}" includes="testng.xml"/>
</testng>
<!--
<testng classpath="${test.classpath}:${test.dest}" groups="fast">
<classfileset dir="${test.dest}" includes="example1/*.class"/>
</testng>
-->
</target>
<target name="usage">
<echo>
ant run will execute the test
</echo>
</target>
<path id="test.c">
<fileset dir="${ws.jars}" includes="*.jar"/>
</path>
<target name="email" >
<java classname="com.qtpselenium.util.SendMail" classpath="${test.dest}" classpathref="test.c" />
</target>
<target name="makexsltreports">
<mkdir dir="${ws.home}/XSLT_Reports/output"/>
<xslt in="${ng.result}/testng-results.xml" style="src/com/testing/xslt/testng-results.xsl"
out="${ws.home}/XSLT_Reports/output/index.html" classpathref="test.c" processor="SaxonLiaison">
<param name="testNgXslt.outputDir" expression="${ws.home}/XSLT_Reports/output/"/>
<param name="testNgXslt.showRuntimeTotals" expression="true"/>
</xslt>
</target>
</project>
Thanks you all for your great help, I have resolved that error ..
It was related to the path of the jar files , I provided incorrect path to the jars.
This was line 70 : -
<javac debug="true" destdir="${test.dest}" srcdir="${test.src}"
target="1.7" classpath="${test.classpath}">
this is related to the path to the jars and in the top line of my file I did provided this path - D:\All jars , while all required jars was not in this folder, now I updated the jar folder and it is working fine now.

ant build generating same class files while packaging the war file

Below is the Ant script i am trying in GAE project but when i look into the war file i see two class for each java file.
Just a fyi, WAR file created i open in winrar but even when i extract it winrar keeps on asking me to either replace existing file message. Not sure how even same file name & extension is present in one folder. Also i checked the "dist" & "classes" folder i does not have duplicate file.
<?xml version="1.0" ?>
<project name="AntExample1" default="war">
<path id="compile.classpath">
<fileset dir="war/WEB-INF/lib">
<include name="*.jar"/>
</fileset>
</path>
<target name="init">
<mkdir dir="build/classes"/>
<mkdir dir="dist" />
</target>
<target name="compile" depends="init" >
<javac destdir="build/classes" debug="true" srcdir="src">
<classpath refid="compile.classpath"/>
</javac>
</target>
<target name="war" depends="compile">
<war destfile="dist/AntExample.war" webxml="war/WEB-INF/web.xml">
<fileset dir="war"/>
<lib dir="war/WEB-INF/lib"/>
<classes dir="build/classes"/>
</war>
</target>
<target name="clean">
<delete dir="dist" />
<delete dir="build" />
</target>
</project>

Ant With Internal Dependencies

I have a a jar right now that uses external dependencies. I'm trying to create a jar that packages all the external dependencies inside, and will just give me one jar. I saw this question asked multiple times, but I still can't figure it out. I'm using Ant, and copied some of the examples I saw on here. I'm using zipgroupfileset to reference the external(now internal) jars. As soon as I added the zipgroupfileset I got a runtime error that said my Runner class could not be found.
<?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="ExcelDemo">
<property environment="env"/>
<property name="ECLIPSE_HOME" value="../../../../Program Files (x86)/eclipse"/>
<property name="debuglevel" value="source,lines,vars"/>
<property name="target" value="1.6"/>
<property name="source" value="1.6"/>
<property name="external-lib-dir" value="lib\poi-3.9" />
<property name="external-lib-dir2" value="lib\poi-3.9\lib" />
<property name="external-lib-dir3" value="lib\poi-3.9\ooxml-lib" />
<path id="ExcelDemo.classpath">
<pathelement location="bin"/>
</path>
<target name="init">
<mkdir dir="bin"/>
<copy includeemptydirs="false" todir="bin">
<fileset dir="src" excludes="**/*.launch, **/*.java"/>
</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" source="${source}" target="${target}">
<src path="src"/>
<classpath refid="ExcelDemo.classpath"/>
</javac>
</target>
<target description="Build all projects which reference this project. Useful to propagate changes." name="build-refprojects">
<ant antfile="${ExcelSensitize.location}/build.xml" inheritAll="false" target="clean"/>
<ant antfile="${ExcelSensitize.location}/build.xml" inheritAll="false" target="build">
<propertyset>
<propertyref name="build.compiler"/>
</propertyset>
</ant>
</target>
<target description="copy Eclipse compiler jars to ant lib directory" name="init-eclipse-compiler">
<copy todir="${ant.library.dir}">
<fileset dir="${ECLIPSE_HOME}/plugins" includes="org.eclipse.jdt.core_*.jar"/>
</copy>
<unzip dest="${ant.library.dir}">
<patternset includes="jdtCompilerAdapter.jar"/>
<fileset dir="${ECLIPSE_HOME}/plugins" includes="org.eclipse.jdt.core_*.jar"/>
</unzip>
</target>
<target description="compile project with Eclipse compiler" name="build-eclipse-compiler">
<property name="build.compiler" value="org.eclipse.jdt.core.JDTCompilerAdapter"/>
<antcall target="build"/>
</target>
<target name="RunnerClass">
<java classname="runner.RunnerClass" failonerror="true" fork="yes">
<classpath refid="ExcelDemo.classpath"/>
</java>
</target>
<target name="jar" description="Create a jar for this project">
<manifestclasspath property="lib.list" jarfile="Test.jar">
<classpath refid="ExcelDemo.classpath" />
</manifestclasspath>
<jar jarfile="Test.jar" includes="*.class" basedir="bin">
<zipgroupfileset dir="${external-lib-dir}" includes="*.jar"/>
<zipgroupfileset dir="${external-lib-dir2}" includes="*.jar"/>
<zipgroupfileset dir="${external-lib-dir3}" includes="*.jar"/>
<manifest>
<attribute name="Class-Path" value="${lib.list}" />
<attribute name="Main-Class" value="runner.RunnerClass" />
</manifest>
</jar>
</target>
</project>
To make things simpler:
Create a separate sources jar for compilation. Then, have a separate compiled jar without the sources.
Don't include the third party jars. Instead, use Ivy with Ant. Ant will automatically download the required jars. In fact, I've see sources that just include the ivy.jar, so Ivy will automatically be configured when you unjar the sources. You type in ant, and everything just builds.
As an alternative, you can look at Maven which is how many projects are now packaged. In fact, if your jar is an open source project, you can probably host it on the OSS Maven repository. This way, no one even needs to manually download your compiled jar. If they want it, they configure their Maven project to do it for them.
i think the problem is that you use basedir="bin" in the your jar task. then path of your zipgroupfileset convert to bin/${external-lib-dir}

Javadoc errors when building Ant project

I am trying to write a build.xml file for my project. When I run build.xml as an Ant project, I get the following error:
D:\workspace\LogAlerter\src\com\j32bit\alerter\launcher\LogAlerter.java:9:
error: package org.apache.log4j does not exist
[javadoc] import org.apache.log4j.Logger;
I have imported log4j in LogAlerter.Java. Here is my build.xml file:
<?xml version="1.0"?>
<project name="LogAlerter" default="main" basedir=".">
<!-- Sets variables which can later be used. -->
<!-- The value of a property is accessed via ${} -->
<property name="src.dir" location="src" />
<property name="build.dir" location="build" />
<property name="dist.dir" location="dist" />
<property name="docs.dir" location="docs" />
<property name="libs.dir" location="lib" />
<!--
Create a classpath container which can be later used in the ant task
-->
<path id="build.classpath">
<fileset dir="${libs.dir}">
<include name="**/*.jar" />
</fileset>
</path>
<!-- Deletes the existing build, docs and dist directory-->
<target name="clean">
<delete dir="${build.dir}" />
<delete dir="${docs.dir}" />
<delete dir="${dist.dir}" />
</target>
<!-- Creates the build, docs and dist directory-->
<target name="makedir">
<mkdir dir="${build.dir}" />
<mkdir dir="${docs.dir}" />
<mkdir dir="${dist.dir}" />
</target>
<!-- Compiles the java code (including the usage of library for JUnit -->
<target name="compile" depends="clean, makedir" >
<javac srcdir="${src.dir}" destdir="${build.dir}" classpathref="build.classpath" includeantruntime="false">
</javac>
</target>
<!-- Creates Javadoc -->
<target name="docs" depends="compile">
<javadoc packagenames="src" sourcepath="${src.dir}" destdir="${docs.dir}">
<!-- Define which files / directory should get included, we include all -->
<packageset dir="${src.dir}" defaultexcludes="yes">
<include name="**" />
</packageset>
</javadoc>
</target>
<!--Creates the deployable jar file -->
<target name="jar" depends="compile">
<jar destfile="${dist.dir}\LogAlerter.jar" basedir="${build.dir}">
<manifest>
<attribute name="Main-Class" value="LogAlerter.Main" />
</manifest>
</jar>
</target>
<target name="main" depends="compile, jar, docs">
<description>Main target</description>
</target>
</project>
Try adding a classpath ref to your javadoc task:
<javadoc packagenames="src"
sourcepath="${src.dir}"
destdir="${docs.dir}"
classpathref="build.classpath">
What the warning is telling you is that you've not provided the full classpath to the javadoc task. Try adding a similar classpath ref to that in your compile task and see where that leads.
Importing is fine but make sure it is available at run time for the JavaDoc tool. log4j.jar should be present in your build.classpath.
Make use of the classpathref inside the docs target like so:
<javadoc packagenames="src" sourcepath="${src.dir}" destdir="${docs.dir}" classpathref="build.classpath">

Resources