How to add one folder in classpath for ant script? - java

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.

Related

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 java jar without main class

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.

Ant-TestNG classpath issue: getResource can't find the file

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.

Java: Ant script works like a charm, but Jar throws exceptions

I created my first Ant script and it's working pretty well. It compiles all my java sources, creates a .jar file and runs the program without even any warning.
But when i try to run my .jar from command line i get NoClassDefFoundError exceptions.
So, how to translate this Ant snippet to work from command line?
<property name="main.class" value="de.bfs.radon.omsimulation.OMMainFrame"/>
<path id="classpath">
<fileset dir="${lib.dir}" includes="**/*.jar"/>
</path>
<target name="run" depends="jar">
<java fork="true" classname="${main.class}">
<classpath>
<path refid="classpath"/>
<path location="${bin.dir}/omsimulation-${version}.jar"/>
</classpath>
</java>
</target>
This is the command line:
# java -classpath lib/ -jar bin/omsimulation-0.4.45-beta3.jar
Throws:
Exception in thread "AWT-EventQueue-0" java.lang.NoClassDefFoundError: com/toedter/calendar/JDateChooser
at de.bfs.radon.omsimulation.OMMainFrame.(OMMainFrame.java:133)
at de.bfs.radon.omsimulation.OMMainFrame$1.run(OMMainFrame.java:106)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:251)
Why does my .jar file not work with the defined classpath? Further down the Ant script:
<target name="jar" depends="manifest">
<mkdir dir="${bin.dir}" />
<jar jarfile="${bin.dir}/omsimulation-${version}.jar" manifest="${src.dir}/omsimulation.manifest" compress="no" basedir="${build.dir}" includes="de/**" />
</target>
<target name="manifest" depends="compile">
<manifestclasspath property="manifest.cp" jarfile="${bin.dir}/omsimulation-${version}.jar">
<classpath refid="classpath" />
</manifestclasspath>
<manifest file="${src.dir}/omsimulation.manifest">
<attribute name="Built-By" value="${author}"/>
<attribute name="Main-Class" value="${main.class}"/>
</manifest>
</target>
Again, running the Ant script works fine. I even tried adding the said libraries to my .jar but that only blows up the file size, the problem still persists.
<jar jarfile="${bin.dir}/omsimulation-${version}.jar" manifest="${src.dir}/omsimulation.manifest" compress="no" basedir="${build.dir}"> <!-- includes="de/**" /-->
<fileset dir="${lib.dir}">
<include name="**/*.jar" />
</fileset>
</jar>
Any ideas on this?
Thanks a lot,
donc_oe
SOLVED: Thanks to perception, the unix command line i was looking for is:
# java -cp lib/*:bin/myjarfile.jar my.package.MyMainClass
And for Windows (note the ; semicolon):
# javaw -cp lib/*;bin/myjarfile.jar my.package.MyMainClass
The relevant thing to note from your build script is this:
<path id="classpath">
<fileset dir="${lib.dir}" includes="**/*.jar"/>
</path>
With that little snippet you have defined a path construct in Ant, which you then refer to in your run task:
<target name="run" depends="jar">
<java fork="true" classname="${main.class}">
<classpath>
<path refid="classpath"/>
<path location="${bin.dir}/omsimulation-${version}.jar"/>
</classpath>
</java>
</target>
This is in effect executing:
java -cp ${lib.dir}/*.jar:${bin.dir}/omsimulation-${version}.jar ${main.class}
Of course, it does so without the squigly lines and the path(s) fully substituted. The main point being that the command you are trying to run yourself is not equivalent at all. When attempting to invoke from the command line you will need to include in the classpath all the necessar JAR's containing your code and all third party libraries. Assuming everything is still bundled in the Ant created folders, something like:
java -cp <full-path-to-lib>/* -jar <full-path-to-bin>/omsimulation-0.4.45-beta3.jar
Or:
java -cp <full-path-to-lib?/*:<full-path-to-bin>/omsimulation-0.4.45-beta3.jar <MainClass>
ClassDefNotFoundException most likely occur when class is found in classpath, but it is loaded in different classloader, or in a wrong path etc.
From the build file, you appears to create jar that include other jars. This may not give a result you want.
Most likely you want a solution described in Easiest way to merge a release into one JAR file. My personal favorite is one-jar.

Ant task for compiling GUI forms (Intellij IDEA)

How I can create a Ant task to compile GUI forms (XML) in Intellij IDEA? I use Scala and Java in my project. Java only for GUI class, and I create it with Intellij IDEA UI Designer.
Please don't beat me, but after setting the 'Generate Ant Build'-dialog like:
the errors are gone:
IDEA provides a Ant task, javac2, that does this. It's a drop-in replacement for the standard javac Ant task.
First, you'll need to include something like the following near the top of your Ant build file.
<path id="javac2.class.path">
<pathelement location="${idea.dir}/redist/forms_rt.jar"/>
<pathelement location="${idea.dir}/redist/javac2.jar"/>
<pathelement location="${idea.dir}/redist/annotations.jar"/>
</path>
<taskdef name="javac2" classname="com.intellij.ant.Javac2" classpathref="javac2.class.path"/>
Here "${idea.dir}" refers to the directory of your IDEA installation. Those jars are redistributable, so you can copy them into your project if you wish, and refer to them there. Once you've done that, just replace any calls to "javac" tasks with "javac2", and everything should just work.
To compile scala, of course, you'll need calls to either scalac or fsc, but those are unaffected by all of this.
Same problem here.
Solved this way:
<property name="idea.lib" value="C:\\Program Files (x86)\\JetBrains\\IntelliJ IDEA Community Edition 9.0.3\\lib"/>
<path id="javac2.classpath">
<pathelement location="${idea.lib}/javac2.jar"/>
<pathelement location="${idea.lib}/jdom.jar"/>
<pathelement location="${idea.lib}/asm.jar"/>
<pathelement location="${idea.lib}/asm-commons.jar"/>
<pathelement location="${idea.lib}/jgoodies-forms.jar"/>
</path>
<taskdef name="javac2" classname="com.intellij.ant.Javac2" classpathref="javac2.classpath"/>
Since this comes up on google, here is what is needed:
<property name="javac2.home" value="${idea.home}/lib"/>
<path id="javac2.classpath">
<pathelement location="${javac2.home}/asm.jar"/>
<pathelement location="${javac2.home}/asm-all.jar"/>
<pathelement location="${javac2.home}/javac2.jar"/>
<pathelement location="${javac2.home}/jdom.jar"/>
<pathelement location="${javac2.home}/asm-commons.jar"/>
<pathelement location="${javac2.home}/jgoodies-forms.jar"/>
</path>
The key is asm and asm-all which solves ClassReader and ClassWriter errors. I had to look in the jars to find that out. "javac2.home" will be OS dependent. This is on Intellij Ultimate.
Here's the correct way:
<property name="javac2.home" value="C:\\Program Files (x86)\\JetBrains\\\IntelliJ IDEA 14.1.4\\lib"/>
<path id="javac2.classpath">
<pathelement location="${javac2.home}/asm.jar"/>
<pathelement location="${javac2.home}/asm-all.jar"/>
<pathelement location="${javac2.home}/javac2.jar"/>
<pathelement location="${javac2.home}/jdom.jar"/>
<pathelement location="${javac2.home}/asm-commons.jar"/>
<pathelement location="${javac2.home}/jgoodies-forms.jar"/>
</path>
Works for both Intellij Community and Ultimate. Tested in both. Just change it to your Intellij Community path, so "IntelliJ IDEA Community Edition 14.1.4", for example.

Categories