Output of java task in Ant - java

I have a .java file and am compiling it using javac in ant. The .class file goes to output directory. A.class when ran, produces a.txt.
How to run the ant ´java´ task and where will the a.txt go, when ran? I mean which directory? Can I specify the direc. where the output files of java task should go?

Take a look at this for reference:
http://ant.apache.org/manual/Tasks/java.html
It contains an example of using the Java task to run a specific class, e.g:
<target name="run">
<java classname="A">
<classpath>
<pathelement location="output"/>
<pathelement path="${java.class.path}"/>
</classpath>
</java>
</target>
It really depends on where you are writing the file out to from A.java. If it is in the current directory, e.g:
File f = new File("./test.txt");
f.createNewFile();
then it will output the file relative to where you ran the build file from.
Hope that helps.

Related

Unknown level in directory, fetch the name of directory with .java file in it

How to access the directory name where I only know it contain .java files and level of directory keep on changing. so is their any way in ANT Script to access those directories with particular .java files in it.
I am using below Script:
<target name="check">
<for param="check1">
<path>
<dirset dir="${project_path}/adapters" includes="*/src/*.java"/>
</path>
<sequential>
<echo>#{check1}</echo>
</sequential>
</for>
</target>
Result:
It is giving nothing as output because it is searching .java file inside src folder but .java files are present under multilevel directories of src. I am not getting how to do it.
As the ant fileset doc states (https://ant.apache.org/manual/Types/fileset.html) you can use ** to search recursively into directories.
<dirset dir="${project_path}/adapters" includes="*/src/**/*.java"/>

how to remove java System.out.println from ant script

how to remove java System.out.println from ant script?
(when we compile the code from ant we should remove the existing java class system.out.println & compiled classes should not have the Sys.out.println)
In build.xml file, preparation phase, before the mkdir commands,
provide:
<javac srcdir="exe" includes="SysOutRemove.java"/>
<java fork="true" classname="SysOutRemove" dir="exe" failonerror="true"/>
where SysOutRemove.java is in the exe package of your project.
SysOutRemove.java should iterate through the list of directories and files in them, store the contents of each file to a reader or something, find sysout statements and replace.
We can use this in the ant file.
<replaceregexp match="System.out.println(.*);" replace="" flags="g" byline="true">
<fileset dir="${src.dir}" includes="**/*.java"/>
</replaceregexp>

Difference between setting classpath in build.xml using fileset and pathelement

I have a build file that declares the classpath as shown
<path id="compile.classpath">
<fileset dir="${basedir}/lib" includes="**"/>
<fileset dir="${jboss.home}/lib" includes="**"/>
<pathelement path ="${build.classes.dir}"/>
</path>
I tried looking at the documentation but I am not able to understand the use of
pathelement.
I know that the ID is used to refer to this class path while performing a task and fileset includes the jarfiles.
edit 1:
My doubt is Why can't we use fileset to include the class files in place of pathelement?
Latest edit:
My doubt is Why can't we use fileset to include the class files in place of pathelement?
If you use a fileset then you'd be adding a set of class files to the path as follows:
CLASSPATH=classes/MyClass1.class:classes/MyClass2.class:classes/MyClass3.class:....
When what Java expects to see is simply:
CLASSPATH=classes
Only jar (and WAR,EAR,etc) files are explicitly listed on the classpath (Java will open them up and load their class files), hence the need for a fileset in ANT.
Update
Here's the Oracle documentation:
http://docs.oracle.com/javase/7/docs/technotes/tools/windows/classpath.html
Class paths to the .jar, .zip or .class files. Each classpath should end with a filename or directory depending on what you are setting the class path to:
For a .jar or .zip file that contains .class files, the class path ends with the name of the .zip or .jar file.
For .class files in an unnamed package, the class path ends with the directory that contains the .class files.
For .class files in a named package, the class path ends with the directory that contains the "root" package (the first package in the full package name).
There was already similar question about 'pathelements' here. From the provided documentation:
"If it's path structure like in your example: "A path-like structure can include a reference to another path-like structure (a path being itself a resource collection) via nested elements"
<path id="base.path">
<pathelement path="${classpath}"/>
<fileset dir="lib">
<include name="**/*.jar"/>
</fileset>
<pathelement location="classes"/>
</path>
If it's classpath structure:
"The path attribute is intended to be used with predefined paths"
<classpath>
<pathelement path="${classpath}"/>
<pathelement location="lib/helper.jar"/>
</classpath>

Run a Java Program from an Ant Build File

So I wrote an ant build.xml file where I take the class files from two Java programs, one that extends the other, package them up into two separate jar files, and then, launches them.
<java classname="Main">
<classpath>
<pathelement location="${mainDir}"/>
<pathelement path="${Main-Class}"/>
</classpath>
</java>
Whenever I invoke ant, it says that "Main" can not be found. I can post the rest of the build.xml file if needed but it's really just this part that I'm confused about. I'm pretty sure that I have the classname right but my biggest problem is figuring out what goes in for location and path. Right now I just have dummy variables.
EDIT: Here's the whole file.
<?xml version="1.0"?>
<project default="dist" name="webscarab">
<description>Class</description>
<property name="ClassFiles" location="..\Simple\trunk\dev\Class\bin\" />
<property name="mainClassFiles" location="..\Simple\trunk\dev\main\build\" />
<property name="buildDir" location=".\build" />
<property name="distDir" location=".\dist" />
<property name="mainDir" location="..\Simple\trunk\dev\webscarab\src\" />
<target name="init">
<tstamp/>
<mkdir dir="${buildDir}"/>
<mkdir dir="${distDir}"/>
</target>
<target name="dist" depends="init">
<jar destfile="${distDir}/package${DSTAMP}.jar" basedir="${ ClassFiles}"/>
<jar destfile="${distDir}/package-web-${DSTAMP}.jar" basedir="${mainClassFiles}"/>
<java classname="Main">
<classpath>
<pathelement location="${mainDir}"/>
<pathelement path="${Main-Class}"/>
</classpath>
</java>
</target>
</project>
EDIT 2: I should mention everything is already compiled and I have all the .class files. It's not something I need to do in the Ant file.
The error message about “class cannot be found” is likely due to an incorrect classpath.
The classpath should grant access to all the class and resource files your application requires. It is composed of a set of directories and jar files. location will denote a single directory or path, whereas path will denote multiple, separated by ; or : depending on your platform.
In your case, it seems to me that you want your classpath to either consist of ${pegaFuzzClassFiles} and ${webScarabClassFiles} as directories, or of ${distDir}/package-pega-${DSTAMP}.jar and ${distDir}/package-web-${DSTAMP}.jar as jar files. You could do this using a single <pathelement path="…"/> element, but I'd suggest multiple <pathelement location="…"/> as I consider this to be clearer.
You should also make sure that any third-party libraries used by your application are available on that path as well. You could nest one or more <fileset> into that <classpath>, each of them describing all the jar files in a given directory.
You probably need to change the elements to point to the Jars that are being created, right now the classpath would appear to point to the source, which is not going to work as the classpath needs the compiled .class files or the jars that contain them.
I would also, just as a matter of style, move the task into a separate 'run' or 'run-app' target that depends on the dist target.
You need to Javac task to compile your code and after that you can run your compiled class. For more information about Javac task visit http://ant.apache.org/manual/Tasks/javac.html
The second advice:
The use of the unix slash '/' is strongly recommended, whether you're on
windows or not. Change your mainDir,distDir and buildDir property locations.
I don't have a ton of ant experience, but you might need to explicitly tell java which jar file to run.
<java jar="path-to-jar-file" classname="org.owasp.webscarab.Main">

Executing a Java running application from a Jar or Batch file

I have a Java application and a build file which, among its tasks, has one task to create a jar file from the application and one to run the application
<!-- ===== Create An Executable Jar target ======-->
<target name="jar-task" depends="compile-task">
<mkdir dir="${jar.dir}"/>
<jar destfile="jar/Guix.jar" basedir="${gui_bin.dir}">
<fileset dir="${basedir}">
<include name="img/**/" />
</fileset>
<manifest>
<attribute name="Main-Class" value="sys.deep.cepu.Start"/>
</manifest>
<filelist dir="${basedir}" files="user.properties"/>
</jar>
</target>
<!--
============ Run target ===================
-->
<target name="run-task" depends="jar-task">
<java classpath="${basedir};jar/Guix.jar;CK-DASP-2.0.0.jar;library/*;user.properties;img/* " classname="sys.deep.cepu.Start" fork="true">
</java>
</target>
The build file runs perfectly, the jar is created and the application runs. I would like to allow the user to start the application by clicking on a single file (jar or batch file). I tried to click on the generated executable jar file but nothing happens. Is it normal? Can someone give me an help on how to execute the program from this jar or from a batch file? Thanks!
Yes, this is normal. You have to start it from command line or via batch script. Try using as a batch script (if you have a MANIFEST.MF) added to your jar.
java -ea -jar Application.jar <Parameters>
or otherwise:
java -cp jar-file packageOfMainClass.MainClass
When building you jar file you already specify the Main-Class. But you did not specify the required libraries in the manifest file, only in the ant file when running the application.
Write another manifest attribute into the ant-file. The name should be Class-Path, the value a space separated list of libraries. Look here (Wikipedia) for an example.
After that your application should start when entering
java -jar Guix.jar
Then follow the step described in this question to make it startable with a double click.
Alternate ways to run a jar file
java -jar jar-file
java -cp jar-file MainClassNameFullyQualified

Categories