Ant won't find classes in libs folder - java

Since last update of ADT & build tools, I cannot build my application via the ant release request.
The ant output shows multiple "cannot find symbol" errors when referencing a class file located in a subfolder of libs/.
I tried modifying ant's build.xml as following :
<path id="java.compiler.classpath">
<fileset dir="${jar.libs.dir}">
<include name="**/*.jar **/*.class" />
</fileset>
</path>
But it doesn't work.
Compiling via Eclipse is fine.
I am truly not an ant expert, but is there something obvious I am missing ?
EDIT :
I tried updating ant and it changes nothing.
I also tried replacing the code above by the following, without success :
<path id="java.compiler.classpath">
<fileset dir="${jar.libs.dir}">
<include name="**/*.jar" />
</fileset>
<pathelement location="${jar.libs.dir}" />
</path>
Any ideas ?

http://ant.apache.org/manual/Tasks/ant.html
Try this, you might be able to find the solution due to the update.
If not that..maybe this one..
http://ant.apache.org/manual/Tasks/java.html

I don't think your include is correct. I think you need to do it like this:
<include name="**/*.jar" />
<include name="**/*.class" />
Here is a way I've done it previously:
<fileset dir = "${lib}" includes = "**/*.jar"/>
<fileset dir = "${webLib}" includes = "**/*.jar"/>

Related

Splash-Screen works in IDE but not as .jar - issue with ANT?

I'm attempting to add a splash-screen to a large Java project. The application is compiled into an executable .jar file using ANT.
I am able to get the splash screen working easily from NetBeans by simply adding -splash:src/com/.../.../image.PNG to my main project's VM options. However, adding SplashScreen-Image: com/.../.../image.PNG to my manifest file fails with "SplashScreen.getSplashScreen() returned null"
I have already opened up my .jar archive to confirm that things were set up correctly: my META-INF\MANIFEST.MF file includes the SplashScreen-Image line. I have tried moving it before or after Main-Class. My actual image.PNG is also in the archive, in the correct path location.
I compile this java project with ANT, which I can guess is the source of my problems (I was able to make a simple, "jar cmf ..." example work just fine).
I use the following to get the project elements ready for achiving:
<target name="compile" depends="init"
description="Compile the source">
<!-- Compile the java code from ${src} into ${build} -->
<path id="lib.path.ref">
<fileset dir="${path_to_jre}" includes="*.jar"/>
</path>
<javac srcdir="${src}" destdir="${build}" source="${java_ver}" target="${java_ver}"
includeantruntime="false">
<!--compilerarg value="-Xbootclasspath/p:${toString:lib.path.ref}" compiler="javac1.7"/-->
<compilerarg value="-Xlint:unchecked"/>
<classpath>
<fileset dir="${LibDir}">
<include name="*.jar"/>
</fileset>
<pathelement path="${dependant_jar}"/>
<pathelement path="${another_dependant_jar}"/>
</classpath>
</javac>
<!-- Copy the .png files -->
<copy todir="${build}">
<fileset dir="${src}" casesensitive="false">
<include name="**/*.PNG"/>
</fileset>
</copy>
</target>
Notice that I use a copy to move .PNG files in with .class files. My image.PNG for the Splash Screen is just in with the others. I added it to my netbeans project by simply copying it there - nothing fancy.
My achieve target is as follows:
<target name="archive" depends="compile"
description="Generate the .jar file">
<!-- Put everything in ${build} into the .jar file -->
<jar jarfile="${jarfile}" basedir="${build}">
<!-- Merge in contents of dependency .jars -->
<zipfileset src="${LibDir}/snakeyaml.jar"/>
<zipfileset src="${dependant_jar}"/>
<zipfileset src="${another_dependant_jar}"/>
<!-- Specify main class in manifest -->
<manifest>
<attribute name="SplashScreen-Image" value="com/../../image.PNG"/>
<attribute name="Main-Class" value="${mainclass}"/>
</manifest>
</jar>
</target>
So my manifest in the eventual .jar is created here, which is also where I eventually realized to add the splashscreen tag.
I am somewhat new to working with ANT, so any advice regarding how to handle this or what to look for is appreciated.
It is not an issue with Ant. The problem is that the -splash option takes a file name, but the SplashScreen-Image manifest attribute must refer to a jar entry. If com/example/brian/image.PNG isn’t in the .jar file, it won’t be usable by SplashScreen-Image. The purpose of a .jar file is to be act as a self-contained module or application, so it should include all of the resources it needs.
The solution is to include the image in your .jar file:
<jar jarfile="${jarfile}" basedir="${build}">
<fileset dir="${src}" includes="**/*.PNG"/>
Update: Your build file was already adding the images to the .jar with a <copy> task, and I failed to notice it.
I have a solution, although I'm sure someone else will understand this better than I do.
I was able to run, with a splash-screen, using the Java binary in ../jre/lib/Java.exe, however I had initially been working from ../lib/Java.exe.
Looks like this is a known bug? Link here: https://bugs.java.com/bugdatabase/view_bug.do?bug_id=7129420. I guess the path to the SplashScreen dll is hardcoded.
Note: Thanks to user VGR for the exhaustive help.

How can I extract a file from a classpath?

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.

How to include external class files during build in ant

I have a package(with some outdated Java files) which i need to build using ant tool. I have some updated classes for which source is not available. So, I need to add the updated class to the jar during build and skip the outdated Java files from the build. Please let me know how to achieve this.
For example - I dont have the updated source java file for com.org.auth.ABC.Java, but i have the updated class file which i got from the another source ie com.org.auth.ABC.class. During build i need to point to the updated class(com.org.auth.ABC.class) for this class alone and create a new jar.
Please find how i am currently pointing to the class files below.
<target name="xxx.jar" depends="xjc,compile">
<jar destfile="${dist.dir}/xxx.jar">
<fileset dir="${classes.dir}" includes="**/*.class"/>
<fileset dir="${jaxb-classes.dir}" includes="**/*.class"/>
<fileset dir="${jaxb-source.dir}" includes="**/bgm.ser,**/jaxb.properties"/>
</jar>
</target>
If you want to leave some package from compilation you can use excludes attribute of fileset ant tag.
for example:
<fileset dir="src/">
<exclude name="**/dir_name_to_exclude/**" />
</fileset>
In order to include the specified class in compilation you can put the containing folder in your class-path using ant.
<path id="project.class.path">
<pathelement location="lib/"/>
<pathelement path="${java.class.path}/"/>
<pathelement path="path to your class file's base folder"/>
</path>
<javac srcdir="${src.dir}"
destdir="${classes.dir}"
classpathref="project.class.path">
... put source files here..
</javac>
And if you want to include that class-file in your jar then add it to fileset using include tag:
<fileset dir="classfiles">
<include name="your class file name"/>
</fileset>
Hope this helps

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.

Why do Jars get Excluded from Enunciate's Deployment?

I'm using Enunciate to build a prototype REST api and need to include a jar containing custom code as a library.
My Ant Script looks like this:
<!--include all jars-->
<path id="en.classpath">
<fileset dir="${lib}">
<include name="**/*.jar" />
</fileset>
</path>
<!--define the task-->
<taskdef name="enunciate" classname="org.codehaus.enunciate.main.EnunciateTask">
<classpath refid="en.classpath" />
</taskdef>
<mkdir dir="${dist}" />
<enunciate dir="${src}" configFile="${basedir}/enunciate.xml">
<include name="**/*.java" />
<classpath refid="en.classpath"/>
<export artifactId="spring.war.file" destination="${dist}/${war.name}" />
</enunciate>
The problem is that my custom jar is being excluded from the WAR file. It is necessary to compile the enunciate annotated classes so the jar is obviously on the classpath at compile time but enunciate is failing to include it in the distribution. I have also noticed that several of the jars needed by enunciate are not being included in the WAR file.
Why are they being excluded and how do I fix it?
I never used enunciate, but as a quick hack you can add the jars to the war:
<jar jarfile="${dist}/${war.name}" update="true">
<fileset dir="${lib}">
<include name="**/*.jar" />
</fileset>
</jar>
Note: you probably want to add the jars to the WEB-INF/lib directory, instead of the root directory.
I'm guessing that enunciate does the mininum to interfere with your own build process, since you know best what to put within your jar file.
As it turns out one of the jars we're attempting to include has a dependency listed in it's Manifest file of a jar that Enunciate depends on (freemarker). Enunciate automatically excludes freemarker and at first glance it seems as though it automatically excludes anything that depends on freemarker as well. If we remove freemarker from the list of dependent jars in our code's manifest file it works just fine.
However; I've spoken with the main developer of Enunciate (Ryan Heaten) and he assures me this isn't what's happening. Including his response below:
Really?!
Wow. Interesting. I can't explain
it; Enunciate doesn't look at what's
in the Manifest in order to determine
what to include in the war, so I'm
kind of stumped here. It could also
be some weird Ant behavior (not
including that jar in the
"en.classpath" reference for some
reason).
~Ryan
In enunciate.xml I tell it not to copy any libs itself:
<webapp doLibCopy="false">
Then in the ant build file at the end of the enunciate task I update the war (you can do this to update the included/excluded jars whether or not you have Enunciate copy the jars for you in the step above):
<war destfile="build-output/{mywar}" update="true">
<lib dir="WebContent/WEB-INF/lib">
<include name="**/*.jar" />
</lib>
<lib dir="build-output">
<include name="some_other.jar" />
</lib>
</war>

Categories