SPRING java.lang.NoClassDefFoundError: org/springframework/context/ApplicationContext - java

It seems people have had similar problems, but on IDE's.
I am not using an IDE.
I installed Spring using a Maven dependency as you will see in my build.xml file.
I get the following stacktrace that says org.springframework.context is not found:
run-decouple:
[java] java.lang.NoClassDefFoundError: org/springframework/context/ApplicationContext
[java] at java.lang.Class.getDeclaredMethods0(Native Method)
[java] at java.lang.Class.privateGetDeclaredMethods(Class.java:2442)
[java] at java.lang.Class.getMethod0(Class.java:2685)
[java] at java.lang.Class.getMethod(Class.java:1620)
[java] at sun.launcher.LauncherHelper.getMainMethod(LauncherHelper.java:488)
[java] at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:480)
[java] Caused by: java.lang.ClassNotFoundException: org.springframework.context.ApplicationContext
[java] at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
[java] at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
[java] at java.security.AccessController.doPrivileged(Native Method)
[java] at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
[java] at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
[java] at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
[java] at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
[java] ... 6 more
[java] Exception in thread "main"
[java] Java Result: 1
BUILD SUCCESSFUL
Total time: 4 seconds
Here is my client (java file: DecoupledDataReaderClient.java):
package com.example.decouple.client;
import java.io.*;
import java.util.*;
import com.example.filereader.IReader;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class DecoupledDataReaderClient {
private IReader reader = null;
private ApplicationContext ctx = null;
public DecoupledDataReaderClient() {
ctx = new ClassPathXmlApplicationContext("beans.xml");
}
private String fetchData() {
reader = (IReader) ctx.getBean("reader");
return reader.read();
}
public static void main(String[] args) {
DecoupledDataReaderClient client = new DecoupledDataReaderClient();
System.out.println("Example 1.3: Got data: " + client.fetchData());
}
}
Here is my build.xml file with the spring dependency & classpath and xmlns to use MAVEN
*EDIT: ADDED THE CLASSPATH TO RUN-DECOUPLE*
<project name="data-reader" default="compile" xmlns:artifact="antlib:org.apache.maven.artifact.ant">
<property name="src" location="."/>
<property name="obj" location="../classes"/>
<property name="jarfile" location="../data-reader.
jar"/>
<target name="init">
<mkdir dir="dist" />
<artifact:dependencies pathId="dependency.classpath">
<dependency groupId="org.springframework"
artifactId="spring-context"
version="3.2.4.RELEASE" />
</artifact:dependencies>
</target>
<target name="compile" depends="init">
<mkdir dir="${obj}"/>
<depend srcdir="${src}" destdir="${obj}" />
<javac includeantruntime="false"
srcdir="${src}"
destdir="${obj}">
<classpath refid="dependency.classpath" />
</javac>
</target>
<target name="jar" depends="compile">
<jar basedir="${obj}" destfile="${jarfile}">
<include name="*.class"/>
</jar>
</target>
<target name="run" depends="compile">
<java classpath="${obj}" classname="VanillaDataReaderClient"/>
</target>
<target name="decouple-jar" depends="compile">
<jar destfile="dist/decouple.jar" basedir="${obj}">
<manifest>
<attribute name="Main-Class" value="com.example.decouple.client.DecoupledDataReaderClient"/>
</manifest>
</jar>
</target>
<target name="run-decouple" depends="decouple-jar">
<java fork="true" jar="dist/decouple.jar" >
<classpath>
<path refid="dependency.classpath" />
</classpath>
</java>
</target>
</project>
And lastly, here is my beans.xml file
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean name="reader" class="com.example.filereader.VanillaFileReader">
<constructor-arg value="src/hello.txt" />
</bean>
</beans>
If someone could help me with why it is not finding the class even though the spring dependency is in my build.xml (which downloaded successfully according to the output of build.xml) and with it in the classpath, I would greatly appreciate it.
Thanks in advance.

artifact:dependencies is storing the classpath for the Spring dependencies in the path dependency.classpath, and you're using that when you compile:
<javac ...
...
<classpath refid="dependency.classpath" />
However, when you run, you're only including your compiled source in the classpath:
<target name="run" depends="compile">
<java classpath="${obj}" classname="VanillaDataReaderClient"/>
</target>
This means the Spring classes aren't included.
Try including dependency.classpath as well:
<target name="run" depends="compile">
<java classname="VanillaDataReaderClient">
<classpath>
<pathelement location="${obj}"/>
<path refid="dependency.classpath"/>
</classpath>
</java>
</target>
Edit:
Your run-decouple target is using jar: "When using the jar attribute, all classpath settings are ignored". Switch to specifying the main class and the classpath, so you can include the dependencies:
<target name="run-decouple" depends="decouple-jar">
<java fork="true" classname="com.example.decouple.client.DecoupledDataReaderClient">
<classpath>
<pathelement location="dist/decouple.jar" />
<path refid="dependency.classpath" />
</classpath>
</java>
</target>

Related

java.lang.AssertionError in JDK 8u202

I am currently updating one of my Java projects from JDK 1.5.0_14 to JDK 8u202.
Here is my problem:
I can build the project with JDK 1.5.0_14 and JDK 8u202.
But as soon as I change one single line of code (no matter what) I get the following java.lang.AssertionError (see at the end of this post).
When I downgrade to JDK 1.5.0_14, build the project once and upgrade to JDK 8u202, I can build again without any error.
But again, after changing any line of code (only one character), I get the error below.
Does anybody have an idea how to resolve this issue?
FYI: I am using NetBeans 8.0.2.
ant -f D:\\NetBeansProjects\\MyProject -Dnb.internal.action.name=build jar
init:
Deleting: D:\NetBeansProjects\MyProject\build\built-jar.properties
deps-jar:
Updating property file: D:\NetBeansProjects\MyProject\build\built-jar.properties
Compiling 1 source file to D:\NetBeansProjects\MyProject\build\classes
An exception has occurred in the compiler (1.8.0_202). Please file a bug against the Java compiler via the Java bug reporting page (http://bugreport.java.com) after checking the Bug Database (http://bugs.java.com) for duplicates. Include your program and the following diagnostic in your report. Thank you.
java.lang.AssertionError: the -J flag should be caught by the launcher.
at com.sun.tools.javac.main.Option$20.process(Option.java:331)
at com.sun.tools.javac.main.Main.processArgs(Main.java:260)
at com.sun.tools.javac.main.Main.compile(Main.java:414)
at com.sun.tools.javac.main.Main.compile(Main.java:381)
at com.sun.tools.javac.main.Main.compile(Main.java:370)
at com.sun.tools.javac.main.Main.compile(Main.java:361)
at com.sun.tools.javac.Main.compile(Main.java:56)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.apache.tools.ant.taskdefs.compilers.Javac13.execute(Javac13.java:56)
at org.apache.tools.ant.taskdefs.Javac.compile(Javac.java:1159)
at org.apache.tools.ant.taskdefs.Javac.execute(Javac.java:935)
at org.netbeans.modules.java.source.ant.JavacTask.execute(JavacTask.java:145)
at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:292)
at sun.reflect.GeneratedMethodAccessor67.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.apache.tools.ant.dispatch.DispatchUtils.execute(DispatchUtils.java:106)
at org.apache.tools.ant.Task.perform(Task.java:348)
at org.apache.tools.ant.taskdefs.Sequential.execute(Sequential.java:68)
at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:292)
at sun.reflect.GeneratedMethodAccessor67.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.apache.tools.ant.dispatch.DispatchUtils.execute(DispatchUtils.java:106)
at org.apache.tools.ant.Task.perform(Task.java:348)
at org.apache.tools.ant.taskdefs.MacroInstance.execute(MacroInstance.java:396)
at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:292)
at sun.reflect.GeneratedMethodAccessor67.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.apache.tools.ant.dispatch.DispatchUtils.execute(DispatchUtils.java:106)
at org.apache.tools.ant.Task.perform(Task.java:348)
at org.apache.tools.ant.Target.execute(Target.java:435)
at org.apache.tools.ant.Target.performTasks(Target.java:456)
at org.apache.tools.ant.Project.executeSortedTargets(Project.java:1393)
at org.apache.tools.ant.Project.executeTarget(Project.java:1364)
at org.apache.tools.ant.helper.DefaultExecutor.executeTargets(DefaultExecutor.java:41)
at org.apache.tools.ant.Project.executeTargets(Project.java:1248)
at org.apache.tools.ant.module.bridge.impl.BridgeImpl.run(BridgeImpl.java:286)
at org.apache.tools.ant.module.run.TargetExecutor.run(TargetExecutor.java:555)
at org.netbeans.core.execution.RunClassThread.run(RunClassThread.java:153)
D:\NetBeansProjects\MyProject\nbproject\build-impl.xml:910: The following error occurred while executing this line:
D:\NetBeansProjects\MyProject\nbproject\build-impl.xml:300: Compile failed; see the compiler error output for details.
BUILD FAILED (total time: 0 seconds)
Here is the compilation and JAR building section from my build-impl.xml
<!--
===================
COMPILATION SECTION
===================
-->
<target name="-deps-jar-init" unless="built-jar.properties">
<property location="${build.dir}/built-jar.properties" name="built-jar.properties"/>
<delete file="${built-jar.properties}" quiet="true"/>
</target>
<target if="already.built.jar.${basedir}" name="-warn-already-built-jar">
<echo level="warn" message="Cycle detected: MyProject was already built"/>
</target>
<target depends="init,-deps-jar-init" name="deps-jar" unless="no.deps">
<mkdir dir="${build.dir}"/>
<touch file="${built-jar.properties}" verbose="false"/>
<property file="${built-jar.properties}" prefix="already.built.jar."/>
<antcall target="-warn-already-built-jar"/>
<propertyfile file="${built-jar.properties}">
<entry key="${basedir}" value=""/>
</propertyfile>
</target>
<target depends="init,-check-automatic-build,-clean-after-automatic-build" name="-verify-automatic-build"/>
<target depends="init" name="-check-automatic-build">
<available file="${build.classes.dir}/.netbeans_automatic_build" property="netbeans.automatic.build"/>
</target>
<target depends="init" if="netbeans.automatic.build" name="-clean-after-automatic-build">
<antcall target="clean"/>
</target>
<target depends="init,deps-jar" name="-pre-pre-compile">
<mkdir dir="${build.classes.dir}"/>
</target>
<target name="-pre-compile">
<!-- Empty placeholder for easier customization. -->
<!-- You can override this target in the ../build.xml file. -->
</target>
<target if="do.depend.true" name="-compile-depend">
<pathconvert property="build.generated.subdirs">
<dirset dir="${build.generated.sources.dir}" erroronmissingdir="false">
<include name="*"/>
</dirset>
</pathconvert>
<j2seproject3:depend srcdir="${src.dir}:${build.generated.subdirs}"/>
</target>
<target depends="init,deps-jar,-pre-pre-compile,-pre-compile, -copy-persistence-xml,-compile-depend" if="have.sources" name="-do-compile">
<j2seproject3:javac gensrcdir="${build.generated.sources.dir}"/>
<copy todir="${build.classes.dir}">
<fileset dir="${src.dir}" excludes="${build.classes.excludes},${excludes}" includes="${includes}"/>
</copy>
</target>
<target if="has.persistence.xml" name="-copy-persistence-xml">
<mkdir dir="${build.classes.dir}/META-INF"/>
<copy todir="${build.classes.dir}/META-INF">
<fileset dir="${meta.inf.dir}" includes="persistence.xml orm.xml"/>
</copy>
</target>
<target name="-post-compile">
<!-- Empty placeholder for easier customization. -->
<!-- You can override this target in the ../build.xml file. -->
</target>
<target depends="init,deps-jar,-verify-automatic-build,-pre-pre-compile,-pre-compile,-do-compile,-post-compile" description="Compile project." name="compile"/>
<target name="-pre-compile-single">
<!-- Empty placeholder for easier customization. -->
<!-- You can override this target in the ../build.xml file. -->
</target>
<target depends="init,deps-jar,-pre-pre-compile" name="-do-compile-single">
<fail unless="javac.includes">Must select some files in the IDE or set javac.includes</fail>
<j2seproject3:force-recompile/>
<j2seproject3:javac excludes="" gensrcdir="${build.generated.sources.dir}" includes="${javac.includes}" sourcepath="${src.dir}"/>
</target>
<target name="-post-compile-single">
<!-- Empty placeholder for easier customization. -->
<!-- You can override this target in the ../build.xml file. -->
</target>
<target depends="init,deps-jar,-verify-automatic-build,-pre-pre-compile,-pre-compile-single,-do-compile-single,-post-compile-single" name="compile-single"/>
<!--
====================
JAR BUILDING SECTION
====================
-->
<target depends="init" name="-pre-pre-jar">
<dirname file="${dist.jar}" property="dist.jar.dir"/>
<mkdir dir="${dist.jar.dir}"/>
</target>
<target name="-pre-jar">
<!-- Empty placeholder for easier customization. -->
<!-- You can override this target in the ../build.xml file. -->
</target>
<target depends="init" if="do.archive" name="-do-jar-create-manifest" unless="manifest.available">
<tempfile deleteonexit="true" destdir="${build.dir}" property="tmp.manifest.file"/>
<touch file="${tmp.manifest.file}" verbose="false"/>
</target>
<target depends="init" if="do.archive+manifest.available" name="-do-jar-copy-manifest">
<tempfile deleteonexit="true" destdir="${build.dir}" property="tmp.manifest.file"/>
<copy file="${manifest.file}" tofile="${tmp.manifest.file}"/>
</target>
<target depends="init,-do-jar-create-manifest,-do-jar-copy-manifest" if="do.archive+main.class.available" name="-do-jar-set-mainclass">
<manifest file="${tmp.manifest.file}" mode="update">
<attribute name="Main-Class" value="${main.class}"/>
</manifest>
</target>
<target depends="init,-do-jar-create-manifest,-do-jar-copy-manifest" if="do.archive+profile.available" name="-do-jar-set-profile">
<manifest file="${tmp.manifest.file}" mode="update">
<attribute name="Profile" value="${javac.profile}"/>
</manifest>
</target>
<target depends="init,-do-jar-create-manifest,-do-jar-copy-manifest" if="do.archive+splashscreen.available" name="-do-jar-set-splashscreen">
<basename file="${application.splash}" property="splashscreen.basename"/>
<mkdir dir="${build.classes.dir}/META-INF"/>
<copy failonerror="false" file="${application.splash}" todir="${build.classes.dir}/META-INF"/>
<manifest file="${tmp.manifest.file}" mode="update">
<attribute name="SplashScreen-Image" value="META-INF/${splashscreen.basename}"/>
</manifest>
</target>
<target depends="init,-init-macrodef-copylibs,compile,-pre-pre-jar,-pre-jar,-do-jar-create-manifest,-do-jar-copy-manifest,-do-jar-set-mainclass,-do-jar-set-profile,-do-jar-set-splashscreen" if="do.mkdist" name="-do-jar-copylibs">
<j2seproject3:copylibs manifest="${tmp.manifest.file}"/>
<echo level="info">To run this application from the command line without Ant, try:</echo>
<property location="${dist.jar}" name="dist.jar.resolved"/>
<echo level="info">${platform.java} -jar "${dist.jar.resolved}"</echo>
</target>
<target depends="init,compile,-pre-pre-jar,-pre-jar,-do-jar-create-manifest,-do-jar-copy-manifest,-do-jar-set-mainclass,-do-jar-set-profile,-do-jar-set-splashscreen" if="do.archive" name="-do-jar-jar" unless="do.mkdist">
<j2seproject1:jar manifest="${tmp.manifest.file}"/>
<property location="${build.classes.dir}" name="build.classes.dir.resolved"/>
<property location="${dist.jar}" name="dist.jar.resolved"/>
<pathconvert property="run.classpath.with.dist.jar">
<path path="${run.classpath}"/>
<map from="${build.classes.dir.resolved}" to="${dist.jar.resolved}"/>
</pathconvert>
<condition else="" property="jar.usage.message" value="To run this application from the command line without Ant, try:${line.separator}${platform.java} -cp ${run.classpath.with.dist.jar} ${main.class}">
<isset property="main.class.available"/>
</condition>
<condition else="debug" property="jar.usage.level" value="info">
<isset property="main.class.available"/>
</condition>
<echo level="${jar.usage.level}" message="${jar.usage.message}"/>
</target>
<target depends="-do-jar-copylibs" if="do.archive" name="-do-jar-delete-manifest">
<delete>
<fileset file="${tmp.manifest.file}"/>
</delete>
</target>
<target depends="init,compile,-pre-pre-jar,-pre-jar,-do-jar-create-manifest,-do-jar-copy-manifest,-do-jar-set-mainclass,-do-jar-set-profile,-do-jar-set-splashscreen,-do-jar-jar,-do-jar-delete-manifest" name="-do-jar-without-libraries"/>
<target depends="init,compile,-pre-pre-jar,-pre-jar,-do-jar-create-manifest,-do-jar-copy-manifest,-do-jar-set-mainclass,-do-jar-set-profile,-do-jar-set-splashscreen,-do-jar-copylibs,-do-jar-delete-manifest" name="-do-jar-with-libraries"/>
<target name="-post-jar">
<!-- Empty placeholder for easier customization. -->
<!-- You can override this target in the ../build.xml file. -->
</target>
<target depends="init,compile,-pre-jar,-do-jar-without-libraries,-do-jar-with-libraries,-post-jar" name="-do-jar"/>
<target depends="init,compile,-pre-jar,-do-jar,-post-jar" description="Build JAR." name="jar"/>
Hello Malawirel I don't know if it helps but if you have a ant project, the problem can be on the properties file of the ant project. If you have it please check it to see if the versions are correct and all is fine.
I finally managed to compile it. Here is what I did:
I changed the Source/Binary Format to JDK 8. See Using NetBeans 8 but getting below compilation error for Lambda expression on how to do that. I still could not compile it (in NetBeans 8.0.2).
I installed NetBeans IDE 10.0 and opened the project there. Then it compiled without any issues.
So seems like this was an issue in NetBeans.

ANT - Java ClassLoader cant find class

in my project there are - among others - two classes: TheProblem and Server.
public class TheProblem {
public static void main (String args []) throws ClassNotFoundException {
ClassLoader.getSystemClassLoader().loadClass("Server");
}
}
When I execute the code from the command line, everything works just fine.
But when I use ANT to execute the code, I get a ClassNotFoundException - although both Server.class and TheProblem.class are inside of the same directory.
The directory structure of my project is fairly simple - I will try to illustrate it here:
root_folder/
- build.xml
- src/
- TheProblem.java
- Server.java
- build/
- TheProblem.class
- Server.class
Here is an excerpt of my build.xml file:
<?xml version="1.0" encoding="UTF-8" ?>
<project name="JAXB" default="compile">
<path id="project.class.path">
<pathelement path="${java.class.path}" />
<pathelement location="build" />
</path>
<target name="init" >
<mkdir dir="build" />
</target>
<target name="compile" depends="init" >
<javac classpathref="project.class.path" srcdir="src" destdir="build"
includeAntRuntime="false" />
</target>
<target name="execute-problem" depends="compile">
<java classpathref="project.class.path" classname="TheProblem" />
</target>
<target name="clean" depends="init">
<delete dir="build" />
</target>
</project>
When I execute ant compile, everything compiles, but when I execute ant execute-problem, the ClassLoader cannot find the Server class and throws a ClassNotFoundException.
When I navigate into the build directory and call java TheProblem, it works just fine. I really have no clue, why it doesn't work using ANT.
Thank you very much for taking the time to read this post.
Instead of
<target name="execute-problem" depends="compile">
<java classpathref="project.class.path" classname="TheProblem" />
</target>
try to use this
<target name="execute-problem" depends="compile">
<java fork="true" dir="." classname="TheProblem">
<classpath>
<path refid="project.class.path" />
</classpath>
</java>
</target>

java.lang.NoClassDefFoundError ANT build

I am getting an error regarding the classpath in the run stage. The error is
run:
[java] java.lang.NoClassDefFoundError: org/openqa/selenium/WebDriver
[java] at java.lang.Class.getDeclaredMethods0(Native Method)
[java] at java.lang.Class.privateGetDeclaredMethods(Unknown Source)
[java] at java.lang.Class.privateGetMethodRecursive(Unknown Source)
[java] at java.lang.Class.getMethod0(Unknown Source)
[java] at java.lang.Class.getMethod(Unknown Source)
[java] at sun.launcher.LauncherHelper.validateMainClass(Unknown Source)
[java] at sun.launcher.LauncherHelper.checkAndLoadMain(Unknown Source)
[java] Caused by: java.lang.ClassNotFoundException: org.openqa.selenium.WebDriver
[java] at java.net.URLClassLoader.findClass(Unknown Source)
[java] at java.lang.ClassLoader.loadClass(Unknown Source)
[java] at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
[java] at java.lang.ClassLoader.loadClass(Unknown Source)
[java] ... 7 more
[java] Error: A JNI error has occurred, please check your installation and try again
[java] Exception in thread "main"
[java] Java Result: 1
BUILD SUCCESSFUL
Total time: 1 second
This is the xml build code I have written. I am referencing the RunningPower jars folder and I believe the correct folder is being referenced as i able am able to compile. My base directory also contains a .classpath and .project file but not sure if they are important.
<?xml version="1.0" ?>
<project name="SeleniumProjectDataDriven" basedir="." default="run">
<target name="init">
<property name="src.dir" value="src" />
<property name="build.dir" value="build" />
<property name="classes.dir" value="${build.dir}/class" />
<property name="lib.dir" value="RunningPowerJars" />
</target>
<target name="clean" depends="init">
<delete dir="build"/>
</target>
<target name="compile" description="Compiles the code" depends="clean" >
<mkdir dir="${classes.dir}" />
<javac srcdir="${src.dir}" destdir="${classes.dir}" includeantruntime="false">
<classpath>
<fileset dir="${lib.dir}">
<include name="**/*.jar" />
</fileset>
</classpath>
</javac>
</target>
<target name="jar" description="Packages the code into jar" depends="compile">
<mkdir dir="build/jar"/>
<jar destfile="build/jar/RunningPower.jar" basedir="build/class">
<manifest>
<attribute name="Main-Class" value="RunningPower"/>
</manifest>
</jar>
</target>
<target name="run" description="Run the jar file" depends="jar" >
<java jar="build/jar/RunningPower.jar" fork="true"/>
</target>
</project>
In my RunningPowerJars folder, they contain
junit-4.8.1.jar
ojdbc6.jar
poi-3.7-20101029.jar
selenium-java-2.46.0.jar
selenium-server-standalone-2.46.0.jar
testng-6.1.1.jar
Update (7:32 AM PST 8/21/2015)
<target name="run" description="Run the jar file" depends="jar" >
<java jar="build/jar/RunningPower.jar" fork="true">
<classpath>
<fileset dir="${lib.dir}">
<include name="**/*.jar" />
</fileset>
</classpath>
</java>
</target>
I modified the code but ran into another error.
BUILD FAILED
C:\Users\dt208672\Perforce\depot\ebill\Automation\Selenium_eBill\RunningPower\build.xml:37: Problem: failed to create task or type classpath
Cause: The name is undefined.
Action: Check the spelling.
Action: Check that any custom tasks/types have been declared.
Action: Check that any <presetdef>/<macrodef> declarations have taken place.
The classpath is needed also when running the Jar file so that the JVM could find library classes. However, when using the jar attribute in the java task, "all classpath settings are ignored" (see https://ant.apache.org/manual/Tasks/java.html). The simplest way is to specify the main class and add the compiled Jar along with the library Jars located in the RunningPowerJars directory:
<target name="run" description="Run the jar file" depends="jar" >
<java classname="RunningPower" fork="true">
<classpath>
<pathelement location="build/jar/RunningPower.jar"/>
<fileset dir="${lib.dir}">
<include name="**/*.jar" />
</fileset>
</classpath>
</java>
</target>
An unrelated note is that you could use the properties defined in init throughout the buildfile. For example, in the jar target you can use ${classes.dir} instead of repeating build/class.
It still doesn't make sense why the build would fail if the classpath element is added inside the java task when using the jar attribute. Although it is ignored, it is weird that it is failing with the "failed to create task or type" error.
Consider that when you use the -jar parameter using the java.exe in command line the classpath parameter is ignored, that also happens with the java task in ant.
So, what you must do is to provide the libraries you need to have available to the process launching that ant and that will suffice.
Once said that... where should you add those libraries then ? if you're launching from eclipse, you may add them on Window->preferences under Ant->Runtime->Global Entries.
If you're using some C.I. server like jenkins you could add them on numerous ways, I cannot give a direct answer because there could be many.
There is also an alternative. You may declare the dependency on the .jar MANIFEST.MF and put it under /lib or whatever inside that jar.

Compiling successful, NoClassDefFoundError at ant java task

I have googled for hours now and read a lot of similar questions like this and this .
Now I simplified my code to some few short lines, so the problem should be obvious - but I still fail to solve it.
Here are my two simple classes:
package ant;
import my.package.util.exception.UtilTest;
public class Debug {
public static void main(String[] args) {
System.out.println(UtilTest.myTest());
}
}
package my.package.util.exception;
public class UtilTest {
public static boolean myTest(){
return true;
}
}
I built my.package.util.jar and put it into directory lib in the project directory.
Now I run the ant built script (see below) to built the jar for Debug class. The built is sucessfull, the MANIFEST.MF is:
Manifest-Version: 1.0
Ant-Version: Apache Ant 1.9.4
Created-By: 1.8.0_20-b26 (Oracle Corporation)
Main-Class: ant.Debug
Class-Path: lib/my.package.util.jar
with empty line at the end.
Here is the build.xml:
<?xml version="1.0"?>
<project>
<path id="my.classpath">
<fileset dir="lib/" includes="**/*.jar"/>
<pathelement location="."/>
<pathelement location="bin"/>
</path>
<target name="compile">
<mkdir dir="build/classes"/>
<javac includeantruntime="false" srcdir="src" destdir="build/classes" encoding="UTF-8">
<classpath refid="my.classpath"/>
</javac>
</target>
<target name="jar">
<mkdir dir="build/jar"/>
<jar destfile="build/jar/Debug.jar" basedir="build/classes">
<manifest>
<attribute name="Main-Class" value="ant.Debug"/>
<attribute name="Class-Path" value="lib/my.package.util.jar"/>
<!-- EDIT: to solve the issue, use value="../../lib/my.package.util.jar"/-->
</manifest>
</jar>
</target>
<target name="debug">
<java classpath="${my.classpath}" jar="build/jar/Debug.jar" fork="true">
<arg value="${file}"/>
</java>
</target>
</project>
Running "ant debug" now, I get:
debug:
[java] Exception in thread "main" java.lang.NoClassDefFoundError: my/package/util/exception/UtilTest
[java] at ant.Debug.main(Unknown Source)
[java] Caused by: java.lang.ClassNotFoundException: my.package.util.exception.UtilTest
[java] at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
[java] at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
[java] at java.security.AccessController.doPrivileged(Native Method)
[java] at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
[java] at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
[java] at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
[java] at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
[java] ... 1 more
[java] Java Result: 1
BUILD SUCCESSFUL
Total time: 1 second
Who can help? :-/
Any hint is appreciated!
Edit: You can't do this that way, because if you use -jar parameter:
the JAR file is the source of all user classes, and other user class path settings are ignored (source).
Issue solved.
I finally understood that i have to set the class path relativ to destination jar file, not relative to build.xml.
So i have to add ../../ in front of the class path.
<manifest>
<attribute name="Main-Class" value="ant.Debug"/>
<attribute name="Class-Path" value="../..lib/my.package.util.jar"/>
</manifest>

adding jar library files to an ant build

I added commons-codec.1.2.jar to my Java Build Path
[javac] C:\Users\souzamor\workspace\tczip\src\tczip\Tczip.java:190: error: c
annot find symbol
[javac] mdEnc = new String( Hex.encodeHex( diges
t ));
[javac] ^
and here is my build.xml file:
<?xml version="1.0" encoding="UTF-8"?>
<project>
<target name="clean">
<delete dir="build" />
</target>
<target name="compile">
<mkdir dir="build/classes" />
<javac srcdir="src" destdir="build/classes" />
</target>
<target name="jar">
<mkdir dir="build/jar" />
<jar destfile="build/jar/Tczip.jar" basedir="build/classes">
<manifest>
<attribute name="Main-Class" value="tczip.ZipComparison" />
</manifest>
</jar>
</target>
<target name="run">
<java jar="build/jar/Tczip.jar" fork="true" />
</target>
</project>
How could I add the jar files into the build.xml file?? I also have another Java class called Tczip which processes MD5:
[java] Processing: bhmcommonclient.zip
[java] Exception in thread "main" java.lang.NoClassDefFoundError: org/apach
mmons/codec/binary/Hex
[java] at tczip.Tczip.digest(Unknown Source)
[java] at tczip.Tczip.execute(Unknown Source)
[java] at tczip.ZipComparison.showFiles(Unknown Source)
[java] at tczip.ZipComparison.showFiles(Unknown Source)
[java] at tczip.ZipComparison.showFiles(Unknown Source)
[java] at tczip.ZipComparison.showFiles(Unknown Source)
[java] at tczip.ZipComparison.showFiles(Unknown Source)
[java] at tczip.ZipComparison.matchMD5(Unknown Source)
[java] at tczip.ZipComparison.main(Unknown Source)
I don't think I'm adding that correctly to my jar file ... how could I do that? I'm totally new to Ant
First declare it like this:
<path id="external.classpath">
<pathelement location="${lib.dir}/commons-codec-1.2.jar"/>
</path>
Then, inside your javac element, include it in the classpath like this:
<classpath>
<path refid="external.classpath" />
</classpath>

Categories

Resources