The app runs perfectly fine in Eclipse and IntelliJ, and also in 'ant run'. Only when I run as Windows cmd to get the following errors:
java -jar TheApp.jar
Exception in thread "JavaFX Application Thread" Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/log4j/Logger
at com.th.app.ui.Login.<clinit>(Unknown Source)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplicationWithArgs$2(LauncherImpl.java:352)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$7(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null$5(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$6(PlatformImpl.java:294)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$4(WinApplication.java:185)
at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.ClassNotFoundException: org.apache.log4j.Logger
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 12 more
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at sun.launcher.LauncherHelper$FXHelper.main(Unknown Source)
Caused by: java.lang.NullPointerException
at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:383)
at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)
... 5 more
This appears to be a -classpath problem, but I have spent more than one day but still getting the same error.
I'm attaching the build.xml, which works perfectly also:
<?xml version="1.0"?>
<project name="App" default="all" basedir=".">
<property name="src" value="./src"/>
<property name="build" value="./build"/>
<property name="lib" value="./lib"/>
<property name="dest" value="./dest"/>
<property name="main-class" value="com.th.app.ui.Login"/>
<path id="classpath">
<fileset dir="${lib}" includes="**/*.jar"/>
</path>
<target name="all" depends="clean, compile, jar, copy-file" description="Builds the whole project">
<echo>Doing all</echo>
</target>
<target name="clean" description="Removes previous build">
<delete verbose="true">
<fileset dir="${build}"/>
</delete>
</target>
<target name="compile" depends="clean" description="compile whole project">
<echo>compile ${ant.project.name} </echo>
<mkdir dir="${build}/classes"/>
<copy file="./config.properties" tofile="${build}/classes/config.properties"/>
<copy file="./src/log4j.properties" tofile="${build}/classes/log4j.properties"/>
<copy todir="${build}/classes/com/th/app/ui">
<fileset dir="${src}/com/th/app/ui">
<include name="**/*.fxml"/>
<include name="**/*.css"/>
</fileset>
</copy>
<javac srcdir="${src}" destdir="${build}/classes" classpathref="classpath" includeantruntime="false" />
</target>
<target name="jar" depends="compile">
<mkdir dir="${build}/jar"/>
<jar destfile="${build}/jar/${ant.project.name}.jar" basedir="${build}/classes">
<manifest>
<attribute name="Main-Class" value="${main-class}"/>
</manifest>
</jar>
</target>
<property name="args" value="READWRITE"/>
<target name="run" depends="copy-file, input-runargs">
<java fork="true" classname="${main-class}">
<classpath>
<path refid="classpath"/>
<path location="${dest}/jar/${ant.project.name}.jar"/>
</classpath>
<arg line="${args}"/>
</java>
</target>
<target name="input-runargs" unless="args" description="prompts for command line arguments if necessary">
<input addProperty="args" message="Type the desired command line arguments:"/>
</target>
<target name="copy-file">
<copy todir="${dest}"><fileset dir="${build}"/></copy>
</target>
</project>
This is my first JavaFx project and I'm trying hard to get it done.
Please help and any insight is greatly appreciated.
You are using at least one 3rd party library (log4j), you may also be using others. You need to also have those libraries on the classpath. Currently, you only have the application jar on the classpath and not the dependent libraries.
You could modify the manifest of your jar to provide the reference to the dependent jars.
Add the following line to the manifest of your jar file.
Class-Path: log4j-core.jar log4j-api.jar
This can probably be done using the manifest element in the ant script (though I have not tried that).
<manifest>
<attribute name="Main-Class" value="${main-class}"/>
<attribute name="Class-Path" value="log4j-core.jar log4j-api.jar"/>
</manifest>
Then ensure that the libraries are in the same directory as your jar file with the matching names you specified.
Then you can likely run the app as you were trying to do before (again I didn't try it).
java -jar TheApp.jar
Or, you could run using the -cp option and place all jars (including your jar) on the path, and run the app as:
java -cp TheApp.jar:log4j-core.jar:log4j-api.jar com.th.app.ui.Login
If you have other dependent libraries than just log4j, you will need to ensure they are on the classpath similarly.
There is a process called shading which unpacks all of the jar files used by your application and then repacks them into a single jar which you can execute. I do not recommend shading, but that is an alternate option which I won't detail here.
Such execution will likely only be possible on systems running obsolete versions of the JDK such as Oracle JDK 8. Recent versions of JDKs usually do not ship with the JavaFX framework, which is instead provided through a set of separate modules. The packaging and execution of applications built using recent JavaFX versions are quite different than what you are doing for a JDK 8-based JavaFX application.
Advice for future applications
Use up-to-date libraries, frameworks, and development tools. Follow the official documentation for those software versions. For instance, JDK 18+, JavaFX 18+, information at openjfx.io, Maven or Gradle instead of Ant and jlink or jpackage (usually via a Maven or Gradle plugin) for application packaging.
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.
The Ant build below compiles successfully.
Unfortunately, it doesn't execute the run step despite the depends attribute specifying execute the run target after compile step completed successfully.
It also doesn't create a jar file in the specified classpath.
build.xml
<?xml version="1.0" encoding="UTF-8"?>
<project default="run" name="My Project">
<target name="run" depends="compile">
<java classname="com.company.program.project">
<classpath path="staging\" location="C:\my_work\Eclipse\3.6-64\plugins\"/>
</java>
</target>
<target name="compile">
<javac includeantruntime="false" srcdir="./src" destdir="staging">
<classpath>
<fileset dir="C:\my_work\Eclipse\3.6-64\plugins">
<!-- <include name="**/*.jar" /> -->
<include name="org.eclipse.swt.*.jar" />
</fileset>
</classpath>
</javac>
</target>
<jar destfile="./build/jars/swtgui.jar" filesetmanifest="mergewithoutmain">
<manifest>
<attribute name="Main-Class" value="org.swtgui.MainGui" />
<attribute name="Class-Path" value="." />
</manifest>
<fileset dir="./bin/com/company/program/project" includes="**/*.class" />
<fileset dir="C:\my_work\Eclipse\3.6-64\plugins\" includes="org.eclipse.swt.win32.win32.x86_64_3.6.0.v3650b.jar" />
</jar>
<record name="./project.log" loglevel="verbose" action="start"/>
EDIT: Got it to run. Did not edit the build.xml, but went to Project Explorer -> build.xml -> External Tool Configurations -> Build -> Targets and for some reason the run target was unchecked.
I checked it and now the run target runs, but produces the error log below.
run:
[java] running com.company.project.program with default permissions (exit forbidden)
[java] Running in same VM Executing 'com.company.project.program'
The ' characters around the executable and arguments are
not part of the command.
[java] Could not find com.company.project.program. Make sure you have it in your classpath
at org.apache.tools.ant.taskdefs.ExecuteJava.execute(ExecuteJava.java:140)
at org.apache.tools.ant.taskdefs.Java.run(Java.java:834)
at org.apache.tools.ant.taskdefs.Java.executeJava(Java.java:228)
at org.apache.tools.ant.taskdefs.Java.executeJava(Java.java:137)
at org.apache.tools.ant.taskdefs.Java.execute(Java.java:110)
at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:293)
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.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:1405)
at org.apache.tools.ant.Project.executeTarget(Project.java:1376)
at org.apache.tools.ant.helper.DefaultExecutor.executeTargets(DefaultExecutor.java:41)
at org.eclipse.ant.internal.launching.remote.EclipseDefaultExecutor.executeTargets(EclipseDefaultExecutor.java:36)
at org.apache.tools.ant.Project.executeTargets(Project.java:1260)
at org.eclipse.ant.internal.launching.remote.InternalAntRunner.run(InternalAntRunner.java:460)
at org.eclipse.ant.internal.launching.remote.InternalAntRunner.main(InternalAntRunner.java:142)
[java] Java Result: -1
run: duration 0 seconds
BUILD SUCCESSFUL
BUILD SUCCESSFUL
Do I need to do anymore editing to the xml file to get this to run and create a jar?
Your jar step is not inside a "target". Create a target named "jar" that depends on "compile", that creates a jar (your "jar" block should work here), and have target "run" depend on it.
It looks like you will need to add the jar you create to your "run" "java" classpath as well.
I am new for apache ant. Now, I am trying to run a single ant for two project. Lets see below....
I have a project named 'Multiply'. In that project, I write a java class named 'Multiply' and a function named 'multiply' that multiply two input integer and return result.
And then I created another project named 'Multiply-Test'. In build path configuration, I add 'Multiply' project into it to test. And then I write a Test class named 'MultiplyTest' and a test case that test return value of multiply function of Multiply class of Multiply Project.
And then I write a ant script (build.xml) file for 'Multiply-Test' project. My xml file is....
<!-- Sets variables which can later be used. -->
<property name="src.dir" location="src" />
<property name="build.dir" location="bin" />
<property name="test.report.dir" location="test-result" />
<!-- Define the classpath which includes the junit.jar and the classes after compiling-->
<path id="junit.class.path">
<pathelement location="lib/junit.jar" />
<pathelement location="${build.dir}" />
</path>
<!-- Deletes the existing build, docs and dist directory-->
<target name="clean">
<delete dir="${build.dir}" />
<delete dir="${test.report.dir}" />
</target>
<!-- Creates the build, docs and dist directory-->
<target name="makedir">
<mkdir dir="${build.dir}" />
<mkdir dir="${test.report.dir}" />
</target>
<!-- Compiles the java code (including the usage of library for JUnit -->
<target name="compile" depends="clean, makedir">
<javac srcdir="${src.dir}/com/dat/multiply" destdir="${build.dir}">
<classpath refid="junit.class.path" />
</javac>
</target>
<!-- Run the JUnit Tests -->
<target name="junit" depends="compile">
<junit printsummary="on" fork="true" haltonfailure="yes">
<classpath refid="junit.class.path" />
<formatter type="xml" />
<batchtest todir="${test.report.dir}">
<fileset dir="${src.dir}">
<include name="**/*Test.java" />
</fileset>
</batchtest>
</junit>
</target>
<target name="main" depends="compile, junit">
<description>Main target</description>
</target>
And then I run ant script. I found the following error in console....
Buildfile: C:\Eclipse Kepler\workspace\Multiply-Test\build.xml
clean:
[delete] Deleting directory C:\Eclipse Kepler\workspace\Multiply-Test\bin
[delete] Deleting directory C:\Eclipse Kepler\workspace\Multiply-Test\test-tesult
makedir:
[mkdir] Created dir: C:\Eclipse Kepler\workspace\Multiply-Test\bin
[mkdir] Created dir: C:\Eclipse Kepler\workspace\Multiply-Test\test-result
compile:
[javac] C:\Eclipse Kepler\workspace\Multiply-Test\build.xml:29:
warning: 'includeantruntime' was not set, defaulting to build.sysclasspath=last; set to false for repeatable builds
junit:
[junit] Running com.dat.test.MultiplyTest
[junit] Tests run: 1, Failures: 0, Errors: 1, Time elapsed: 0 sec
BUILD FAILED
C:\Eclipse Kepler\workspace\Multiply-Test\build.xml:36: Test com.dat.test.MultiplyTest failed
Total time: 1 second
And found the following junit output error......
java.lang.ClassNotFoundException: com.dat.test.MultiplyTest
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
But I test by another way. That, and then, I clean and build both project. And then run 'MultiplyTest' class by Right click on class -> Run As. -> JUnit Test. Oh, It is work really. And Junit result is true that i wish.
Now, I don't know how to do for work that test class from ant scripts. I need to solve, but I don't know.
Anybody help me. Thanks..!
This doesn't look right:
<javac srcdir="${src.dir}/com/dat/multiply" destdir="${build.dir}">
<classpath refid="junit.class.path" />
</javac>
The srcdir of <javac> shouldn't include the /com/dat/multiply part. The <javac> documentation explains:
Do not include part of your package structure in the srcdir attribute
Instead, it should be:
<javac srcdir="${src.dir}" destdir="${build.dir}">
<classpath refid="junit.class.path" />
</javac>
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>