ANT eclipse headless build - java.lang.NoClassDefFoundError - java

I am trying to make a headless build that requires eclipse specific tasks.
For launching the ant buildfile, I use the following command. I do it this way because I believe it allows me to run eclipse tasks that previously complained that they needed a workspace to run in. If this is incorrect/if there is a better way, please inform me.
My batch script:
java -jar %EQUINOX_LAUNCHER_JAR% -application org.eclipse.ant.core.antRunner -buildfile %ANT_SCRIPT_JAR% -data %WORKSPACE_PATH%
Inside my ant buildfile, I need to define a task:
<taskdef name="myTask" classname="path.to.class.with.execute"><classpath><pathelement location="path\to\dependency.jar"/></classpath></taskdef>
When running
<myTask/>
I get
java.lang.NoClassDefFoundError: path/to/class/that/I/tried/to/import

Classes which your task’s code uses must be in the classpath. One option is to add them explicitly to the classpath when defining the task:
<taskdef name="myTask" classname="path.to.class.with.execute">
<classpath>
<pathelement location="path/to/dependency.jar"/>
<pathelement location="path/to/transitive-dependency.jar"/>
<pathelement location="path/to/other-transitive-dependency.jar"/>
</classpath>
</taskdef>
If all the .jar files are in the same directory tree, you can shorten it to:
<taskdef name="myTask" classname="path.to.class.with.execute">
<classpath>
<fileset dir="path/to/dir" includes="**/*.jar"/>
</classpath>
</taskdef>
One other possibility is to add a Class-Path attribute to the manifest of the .jar which contains the task class. The attribute’s value is a space separated list of relative URLs, with their implied base being the .jar file where the manifest resides. For example:
Class-Path: transitive-dependency.jar utils/other-transitive-dependency.jar
If you’re building the task .jar itself in Ant, you can specify the Class-Path attribute in Ant’s jar task:
<jar destfile="task.jar">
<fileset dir="classes"/>
<manifest>
<attribute name="Class-Path"
value="transitive-dependency.jar utils/other-transitive-dependency.jar"/>
</manifest>
</jar>

Related

Taskdef class com.temp.install.common.action.UserInstallDirRule cannot be found using the classloader AntClassLoader

I have created a jar with the .class files and the dependency libraries which are required for executing the class files using the below jar code
<target name="jar" depends="clean">
<jar destfile="${basedir}/lib/HelloWorld.jar">
<zipgroupfileset dir="${basedir}/lib" includes="*.jar" />
<fileset dir="${basedir}" includes="/com/temp/**" />
<fileset dir="${basedir}" includes="build.properties"/>
<manifest>
<attribute name="Class-Path" value="./HelloWorld.jar"/>
</manifest>
</jar>
</target>
Now i have written other build.xml to run taskdef actions using this jar but the following error occurs when i try to invoke the class files using the taskdef actions even though the class files and their dependencies are present in the same jar.
BUILD FAILED
C:\Users\kh2139\Desktop\New folder\build.xml:4: taskdef class com.temp.install.common.action.UserInstallDirRule cannot be found
using the classloader AntClassLoader[C:\Users\kh2139\Desktop\New folder\HelloWorld.jar]
Attaching my build.xml code below which is used to run taskdef actions on the HelloWorld.jar
<?xml version="1.0" encoding="ISO-8859-1"?>
<project name="MyTask" basedir="." default="use">
<target name="use" description="Use the Task" >
<taskdef name="helloworld1" classname="com.temp.install.common.action.UserInstallDirRule" classpath="HelloWorld.jar"/>
<helloworld1/>
<taskdef name="helloworld" classname="com.temp.install.common.action.EncryptionGUID" classpath="HelloWorld.jar"/>
<helloworld/>
</target>
</project>
PS: I could able to run the build.xml file successfully without errors when i specify the lib folder in the location where i place HelloWorld.jar and give the classpath to the lib folder in the taskdef actions.
But my issue is i want to use the same jar to contain the dependencies that are used while executing the classes.
The error indicates that Java cannot find UserInstallDirRule.class in HelloWorld.jar. To determine if HelloWorld.jar contains the class, try running the jar.exe program included with the JDK.
Here's an example of running jar.exe in a Windows Command Prompt:
C:\>jar.exe tf "C:\Users\kh2139\Desktop\New folder\HelloWorld.jar"
The output will show whether UserInstallDirRule.class is in the JAR file.

Why combine all jars together?

I have create RESTful web service based on the JAX-RS and used Jersey embedded web server. My ant script compiles code successfully while it gives me error ClassNotFoundException when I run my main class. So after doing research I came up with solution & here it goes java build ant file with external jar files . What I did was created a bundled jar file try to execute that & it works perfectly fine. I want to know the reason behind :
why this solution works ?
Why I should combine all jar file ?
Is it similar to war file which we create following J2EE architecture otherwise war will not be extracted by server ( say TOMCAT ) & in my case jar file for Jersey embedded HTTP server?
EDIT:
Here is my ant build.xml file
<property name="lib.dir" value="${user.dir}/lib"/>
<property name="build.dir" value="${user.dir}/build"/>
<property name="build.lib.dir" value="${build.dir}/lib"/>
<property name="build.classes.dir" value="${build.dir}/classes"/>
<property name="src.dir" value="${user.dir}/src/main/java"/>
<property name="main.class" value="com.assignment.ConsoleServer"/>
<path id="classpath">
<fileset dir="${lib.dir}" includes="**/*.jar"/>
</path>
<target name="clean">
<delete dir="${build.dir}"/>
</target>
<target name="init" depends="clean">
<!-- Create the build directory structure used by compile -->
<mkdir dir="${build.dir}"/>
<mkdir dir="${build.classes.dir}"/>
</target>
<target name="copy_jars" depends="init" >
<copy todir="${build.lib.dir}" >
<fileset dir="${lib.dir}">
<include name="**/*.jar" />
</fileset>
</copy>
</target>
<target name="compile" depends="copy_jars">
<javac srcdir="${src.dir}" destdir="${build.classes.dir}" classpathref="classpath" includeantruntime="false"/>
</target>
<target name="jar" depends="compile">
<jar destfile="${build.dir}/${ant.project.name}.jar" basedir="${build.classes.dir}">
<manifest>
<attribute name="Main-Class" value="${main.class}"/>
</manifest>
<zipgroupfileset dir="${lib.dir}" includes="*.jar"/>
</jar>
</target>
<target name="run" depends="jar">
<java fork="true" classname="${main.class}">
<classpath>
<path refid="classpath"/>
<path location="${build.dir}/${ant.project.name}.jar"/>
</classpath>
</java>
</target>
Here is my folder structure
P.S. I am not java expert so pardon me if this question is stupid.
Why this solution works?
In your particular case, you probably didn't include all of the necessary dependencies in your deployment in your previous. (It is not clear from your question how you were originally doing the deployment.)
Now you have put all of the application and dependent class files, etc into one JAR file, and presumably you are deploying / running that file. It works because now it has everything that it needs to run ... which it didn't before.
Why I should combine all jar file?
In your case I suspect that it was not strictly necessary. There was probably a way to "deploy" all of the dependencies without combining them into a single JAR file.
However, there is one case where a "uber-jar" has advantages. That is when the JAR is intended to be an "executable" JAR, and you want to be able to distribute / install it as a single file. (And executable JAR
file can refer to external JARs, etc, but the way that you have to do
it is "fragile".)
Is it similar to war file ... ?
Sort of, though a WAR file contains JAR files ... and typically other kinds of resources that the web-container understands.
The solution works because you packed all you service classes and depending libraries in one jar. That jar and everything inside will be in the class path and visible to your execution virtual machines class loader.
If you leave your depending libraries out your Jersey Web server needs to have them on it's class path, then you wouldn't get ClassNotFoundExcpetion
You shouldn't pack web application in single jar. You should crate war file where you dependencies will be placed inside WEB-INF/lib. You would easily then deploy that war on any application server. Switching to Maven instead of Ant can help a lot.
EDIT: After you added more details to description and ant
If you don't want to use fat-jar you can either
modify your antjava task to specify classpath that will reference
all external libraries (basically telling ant how to build
-classpath parameter for java -jar command
even better, modify your javac ant task by making complete Manifest file that specifies Class-Path correctly, take a better
look at the solution (at the bottom) of the answer you linked (java build ant file with external jar files)
For completness reference on Manifest here

java.io.IOException: CreateProcess error=206, The filename or extension is too long [duplicate]

I'm trying to call Findbugs via Ant, but receiving this error:
Cannot run program "C:\Program Files (x86)\Java\jre6\bin\javaw.exe" (in
directory "H:\Users\MyName\workspace\MyProject"):
CreateProcess error=206, The filename or extension is too long
How can I fix this? o.O
I had the same problem.
I used
<fileset dir="${basedir}/build">
<include name="**/*.class"/>
</fileset>
inside findbugs target and it seems that there is too much .class files to be passed to findbug (?via command line?) because when I used
<fileset dir="${basedir}/build/com/domain/package">
<include name="**/*.class"/>
</fileset>
that had low number of classes, the error was gone.
So, I solved the problem by making one jar file and feeding it to findbugs target with
<findbugs home="${findbugs.home}">
...
<class location="${basedir}/targets/classes-to-analyze.jar"/>
</findbugs>
I think one of the effective file paths are really long when java tries to compile clases.
One worth try is to put codebase in a directory such as C:\MyProject instead of something like C:\Users\MyName\workspace\MyProject
To solve this issue you need to generate a manifestclasspath and a pathing jar.
First Generate your classpath.
<path id="javac.path">
<fileset dir="lib/" includes="**/*.jar"/>
</path>
Next Generate your manifestclasspath
<target name="generate-manifest-classpath">
<manifestclasspath property="manifest.classpath" jarfile="pathing.jar">
<classpath refid="javac.path"/>
</manifestclasspath>
<jar destfile="pathing.jar" basedir="${the location of your build classes}">
<manifest>
<attribute name="Class-Path" value="${manifest.classpath}"/>
</manifest>
</jar>
<path id="javac.classpath">
<pathelement path="pathing.jar"/>
</path>
</target>
Next Implement your Manifestclasspath
<javac srcdir="${foo.dir}" destdir="${bar.dir}"
<classpath refid="javac.classpath"/>
</javac>
This will solve the 206 error message if implemented correctly.
I had the same error on IntelliJ while starting debug mode only. To fix is I've changed:
Run > Edit Configurations > "Configuration" tab > Shorten command line
to "JAR-manifest"

Log4j not being added to classpath in Ant

I'm attempting to add Log4j to my project's classpath in Ant which creates an executable JAR, but it appears that it's not being added properly.
Here is the path component of my Ant build script:
<path id="classpath.compile">
<fileset dir="${dir.myLibs}">
<include name="**/*.jar"/>
</fileset>
<pathelement location="${dir.webContent}/WEB-INF/lib/log4j.jar" />
</path>
The compile target looks like this:
<target name="-compile">
<javac destdir="${dir.binaries}" source="1.6" target="1.6" debug="true" includeantruntime="false">
<src path="${dir.source}"/>
<classpath refid="classpath.compile"/>
</javac>
</target>
Tthe target that creates the JAR:
<target name="-createJar" >
<jar jarfile="${path.jarFile}"
manifest="${dir.source}\META-INF\MANIFEST.MF">
<fileset dir="${dir.binaries}" casesensitive="yes">
<exclude name="**/*.java"/>
</fileset>
</jar>
</target>
Lastly, the MANIFEST.MF:
Manifest-Version: 1.0
Class-Path: ../../../WebContent/WEB-INF/lib/log4j.jar (what is this pathing relative to?)
Main-Class: foo.Bar
The JAR is created, but when I execute it, I get:
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/log4j/Logger...
Any thoughts as to what I'm doing wrong?
It looks from the classpath in your MANIFEST that you are trying to reference a jar inside your jar. The only two ways to make that work AFAIK are 1) a special classloader, like #infosec812 mentions, or 2) by exploding the jar dependencies directly into the root of your jar. Either is workable, but I don't see either of them happening in your ant script.
If you're trying to reference a jar outside of your jar, your relative classpath is relative to the location of the jar you are executing. Make sure the referenced jar exists in that location.
I'm guessing that you're running the Java program as follows
java -jar myapp.jar
In this case you'll need to specify the Class-Path attribute in the manifest. I suggest you also check out the manifestclasspath task
Creating the jar does not include the linked libraries in the jar. You would have to have the required jars in your execution classpath in order to run it that way. Or, you could use the solution I use, which is to create a one-jar archive. It adds a specialized class loader for your application into the resulting jar and also packages your required jars in to the final executable jar. It works really well for deploying neat, simple to use packages.

How can include multiple jars in the classpath using ant?

I have a bunch of .java files in a "src" folder that depend on three jars in a "lib" folder. I have the following build.xml file:
<?xml version="1.0"?>
<project name="MyProj" basedir=".">
<property name="src" value="src"/>
<property name="build" value="build"/>
<property name="lib" value="lib"/>
<path id="master-classpath">
<fileset dir="${lib}">
<include name="activemq-all-5.1-SNAPSHOT.jar"/>
<include name="geronimo-jms_1.1_spec-1.1.1.jar"/>
<include name="activemq-core-5.3.0.jar"/>
</fileset>
</path>
<javac destdir="${build}">
<src path="${src}"/>
<classpath refid="master-classpath"/>
</javac>
</project>
This compiles fine, but when I try and run I get
"java.lang.NoClassDefFoundError:
javax/jms/Destination"
This program runs and compiles fine when I include the jars in the buildpath using Eclipse, though.
EDIT: So I copied the jars into the folder that has the compiled classes. The class with the main method is NDriver.class. When I try:
java -classpath
./geronimo-jms_1.1_spec-1.1.1.jar:./activemq-core-5.3.0.jar:./activemq-all-5.1-SNAPSHOT.jar
NDriver
This gives:
Exception in thread "main"
java.lang.NoClassDefFoundError:
NDriver
I'd appreciate any help.
You need to put the jars used at compile time on the classpath when running the application. Sadly, you didn't provide any detail on how you are actually running it so its hard to provide more guidance.
UPDATE: The directory containing the compiled classes needs to be added to the classpath too. If you launch java from the directory containing the compiled classes, then you can use . to designate the current directory. Add it to the classpath as shown below to tell java to look for classes there too (I've added . right after activemq-all-5.1-SNAPSHOT.jar):
java -classpath ./geronimo-jms_1.1_spec-1.1.1.jar:./activemq-core-5.3.0.jar:./activemq-all-5.1-SNAPSHOT.jar:. NDriver
One way (slightly different variables than yours)
<path id="classpath">
<fileset dir="${lib.dir}" includes="**/*.jar"/>
</path>
<manifestclasspath property="manifest.classpath" jarfile="${jarfile}">
<classpath refid="classpath"/>
</manifestclasspath>
<target name="jar" depends="compile" description="create the jar">
<jar destfile="${jarfile}" basedir="${build.dir}">
<manifest>
<attribute name="Manifest-Version" value="${manifest-version}"/>
<attribute name="Created-By" value="${ant.java.version}"/>
<attribute name="Main-Class" value="${main-class}"/>
<attribute name="Class-Path" value="${manifest.classpath}"/>
</manifest>
</jar>
</target>
Of course here I'm assuming that you are creating a jar and running it (including the classpath there). Another option would be to have a run target which use the <java> tag and explicitly use the classpath there.
Are the library jars included in the classpath when you run the program? Eclipse automatically add these, but you need to specifying them when you run the program from the command line.
From my experience it seems Eclipse will often include classes and jars in the classpath without explicitly using the classpath declaration. Indeed it can sometimes be quite hard to remove classes from Eclipse's build (they have to be deleted or clean'ed).

Categories

Resources