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

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.

Related

bad name in value for --add-modules when trying to compile through ant

I am trying to include the javafx.controls module in my project and compile it through ant, rather then use javaFX javac --module-path "..\lib\javafx-sdk-17.0.2\lib" --add-modules javafx.controls App.java
java --module-path "..\lib\javafx-sdk-17.0.2\lib" --add-modules javafx.controls App commands to compile the file(s).
This is how my ant build.xml looks like right now:
<project name="projectName" default="dist">
<property file="build.properties"/>
<property file="${user.home}/build.properties"/>
<path id="run.classpath">
<fileset dir="${dist.dir}">
<include name="${project.name}.jar"/>
</fileset>
</path>
<target name="compile">
<mkdir dir="${build.dir}"/>
<javac srcdir="${src.dir}"
destdir="${build.dir}"
debug="${compile.debug}"
deprecation="${compile.deprecation}"
optimize="${compile.optimize}"
includeantruntime="false">
<classpath>
<fileset dir="${lib.dir}/javafx-sdk-17.0.2/lib">
<include name="**/*.jar" />
</fileset>
</classpath>
<compilerarg line="--add-modules ${lib.dir}/javafx-sdk-17.0.2/lib/javafx.controls"/>
</javac>
</target>
<target name="dist" depends="compile">
<mkdir dir="${dist.dir}"/>
<jar jarfile="${dist.dir}/${project.name}.jar"
basedir="${build.dir}"
manifest="${src.dir}/Manifest.mf">
<zipgroupfileset dir="${lib.dir}/javafx-sdk-17.0.2/lib" includes="**/*.jar"/>
</jar>
</target>
<target name="build" depends="dist">
<java jar="${dist.dir}/${project.name}.jar" fork="true">
</java>
</target>
<target name="clean">
<delete dir="${build.dir}"/>
<delete dir="${dist.dir}"/>
</target>
</project>
The error I keep on getting is: error: bad name in value for --add-modules option: 'C:\xampp\htdocs\projectName/lib/javafx-sdk-17.0.2/lib/javafx.controls'. I have looked up the error, but the answers are very vague in my opinion and have not helped me get more knowledge in regards to the error.
What you are doing wrong
You are providing an incorrect value to --add-modules.
You are not providing a string to ant that matches the example command in your question.
In your question, for the example javac command, you have:
--module-path "..\lib\javafx-sdk-17.0.2\lib" --add-modules javafx.controls
but in your ant script you have
--add-modules ${lib.dir}/javafx-sdk-17.0.2/lib/javafx.controls
You have tried to compress the --module-path argument with the --add-modules argument. Those arguments must remain separate and have different values.
ant javac support for the Java Platform Module System
The ant javac task has a few parameters for working with the Java module system, you should use appropriate ones:
modulepath
Use this to reference the directory set containing your JavaFX modules (e.g. the SDK lib directory).
Do not put the JavaFX libraries on the class path, they are modules and belong on the module path.
Specific changes required for your ant script
I am not familiar enough with the ant argument syntax to provide the exact ant XML elements for the correct definitions of the arguments for your case.
If somebody knows the exact values required in ant element syntax, they can edit this answer and put the values here.
Background info
Read the javac man page if you want to understand what the --module-path and --add-modules arguments are, what data should be supplied to them, and the format required for that data.
Also review understanding java 9 modules.
Alternate approach: defining module info
As an alternative to adding modules via the --add-modules VM argument, you can instead define a module-info.java file in your application to make it modular and require the modules there.
This will work fine if you aren't depending on non-modular code.
Even if you define a module-info.java file, you still need to set the --module-path for the compiler and runtime so that it can find the module implementations. Build tools such as Maven will do this automatically for defined dependencies.
However, ant isn't aware of build dependencies and needs manual help with parameters to support the module system. You will need to set the module path manually via modulepath arguments to the compiler. Similarly, you will need to set VM arguments for runtime module support if you have a java execution task in ant.
solution
<target name="compile">
<mkdir dir="${build.dir}"/>
<javac srcdir="${src.dir}"
destdir="${build.dir}"
debug="${compile.debug}"
deprecation="${compile.deprecation}"
optimize="${compile.optimize}"
includeantruntime="false">
<classpath>
<fileset dir="${lib.dir}/javafx-sdk-17.0.2/lib">
<include name="**/*.jar" />
</fileset>
</classpath>
<compilerarg line="--module-path ${lib.dir}/javafx-sdk-17.0.2/lib/"/>
<compilerarg line="--add-modules javafx.controls"/>
</javac>
</target>
<target name="build" depends="dist">
<java jar="${dist.dir}/${project.name}.jar" fork="true">
<jvmarg line="--module-path ${lib.dir}/javafx-sdk-17.0.2/lib/"/>
<jvmarg line="--add-modules javafx.controls"/>
</java>
</target>
First use the compilerarg with the line, to indicate what you want to compile during the javac process.
<compilerarg line="--module-path ${lib.dir}/javafx-sdk-17.0.2/lib/"/>
<compilerarg line="--add-modules javafx.controls"/>
You then provide these lines within the java process as well.
<jvmarg line="--module-path ${lib.dir}/javafx-sdk-17.0.2/lib/"/>
<jvmarg line="--add-modules javafx.controls"/>

Ant not able to locate external jar files. Showing java.lang.NoClassDefFound Error exception

I am creating a jar file using following target -
<target name="jar" depends="compile">
<jar destfile="${jar.dir}/TargetClass.jar"
basedir="${build.dir}">
<manifest>
<attribute name="Main-Class" value="${main.class}"/>
</manifest>
</jar>
</target>
and I'm trying to execute the jar file using following target -
<target name="runjar">
<java jar="${jar.dir}/TargetClass.jar" fork="true"/>
</target>
I've used apache poi jar in my TragetClass which is located under D:/Jar directory. While executing it's not able to locate the poi jars and showing java.lang.NoClassDefFoundError exception.
You need to use a classpath. Something like:
<property name="poi.jars" value="path to poi jars"/>
<path id="run.classpath">
<pathelement location="${poi.jars}"/>
</path>
<target name="runjar">
<java
jar="${jar.dir}/TargetClass.jar"
fork="true"
<classpath refid="run.classpath"/>
/>
</target>
See Ant Java Task for more information.

java.lang.NoClassDefFoundError + ant - running a jar

Tagging- Selenium as well just in case someone faced similar issue while creating selenium tests using Ant.
I have seen lot of questions/answers on this topic, tried all the options suggested on various forums but still my issue is not getting resolved. Basically i compile code(includes the test scripts), create JAR and run the same JAR. For some reason it does not seem to identify the libraries during run time. Same code(With tests) works fine when main() method is run from Eclipse. Here is the build.xml,
<project default="run">
<target name="clean">
<delete dir="build" />
</target>
<target name="init-classpath">
<path id="lib.classpath">
<fileset dir="./lib/">
<include name="**.jar" />
</fileset>
</path>
<pathconvert property="mf.classpath" pathsep=" ">
<path refid="lib.classpath" />
<flattenmapper />
</pathconvert>
</target>
<target name="jar" depends="clean, init-classpath">
<javac classpathref="lib.classpath" destdir="./compiled" failonerror="true" srcdir="./src" />
<mkdir dir="build/jar" />
<jar destfile="build/jar/BANC.jar" basedir="compiled">
<manifest>
<attribute name="Main-Class" value="com.portico.driver.TestDriver" />
<attribute name="Class-Path" value="${mf.classpath}" />
</manifest>
</jar>
</target>
<target name="run" depends="jar">
<java jar="build/jar/BANC.jar" fork="true">
</java>
</target>
</project>
Error:-Exception in thread "main" java.lang.NoClassDefFoundError: jxl/Workbook
Manifest content
Manifest-Version: 1.0
Ant-Version: Apache Ant 1.8.3
Created-By: 1.6.0_41-b02 (Sun Microsystems Inc.)
Main-Class: com.portico.driver.TestDriver
Class-Path: activation.jar commons-lang-2.4.jar jna-3.4.0.jar jxl.jar
logging-selenium-1.2.jar mail.jar ojdbc14.jar poi-3.0.2-FINAL.jar rep
ortng-1.1.1.jar saxon-8.7.jar selenium-grid-demo-1.0.7.jar selenium-g
rid-demo-standalone-1.0.7.jar selenium-grid-hub-1.0.7.jar selenium-gr
id-hub-standalone-1.0.7.jar selenium-grid-remote-control-1.0.7.jar se
lenium-grid-remote-control-standalone-1.0.7.jar selenium-grid-tools-1
.0.7.jar selenium-grid-tools-standalone-1.0.7.jar selenium-server-1.0
.3-standalone.jar selenium-server-standalone-2.33.0.jar sikuli-script
.jar testng-6.8.jar velocity-1.7.jar
The first thing to check is, whether the problem is connected with the manifest or something else. If you can run your application with java -cp <allthejarfiles> <main-class> the problem is connected with the manifest. Keep in mind that the jar files specified in the manifest are relative to the jar file’s location. Trying to run the application with the -verbose:class option gives hint about which jar are really loaded.
Your manifest assumes the jars in the current working directory. So it would require dir attribute set to the folder where the jar exists.
Java task supports providing classpath in the arguments. Try giving the classpath in arguments.
<target name="run" depends="jar">
<java jar="build/jar/BANC.jar" fork="true" dir="build/jar" >
<classpath>
<pathelement path="${lib.classpath}"/>
</classpath>
</java>
</target>
I have faced the same issue in my project. I suggest that you should create a separate directory specially for the jar files, put all of your jars in that directory, and then point the lib address to that directory.
for example in your case say I have created a directory D:/jar_collection, where I have put all my jars physically.
<property name="lib.dir" value="D:/jar_collection"/>
<target name="setClassPath">
<path id="classpath_jars">
<fileset dir="${lib.dir}" includes="*.jar" />
</path>
<pathconvert pathsep=":" property="test.classpath" refid="classpath_jars" />
</target>
and it works fine. Please try it once.

Ant include external .jar

I want to include external jar to my java project. I'm using ant. External .jar is in folder lib. My build.xml looks something like that:
<?xml version="1.0" encoding="UTF-8"?>
<project>
<path id="classpath">
<fileset dir="lib" includes="**/*.jar"/>
</path>
<target name="clean">
<delete dir="build"/>
</target>
<target name="compile">
<mkdir dir="build"/>
<javac srcdir="src" destdir="build" classpathref="classpath" />
</target>
<target name="jar">
<mkdir dir="trash"/>
<jar destfile="trash/test.jar" basedir="build">
<zipgroupfileset dir="lib" includes="**/*.jar"/>
<manifest>
<attribute name="Main-Class" value="com.Test"/>
</manifest>
</jar>
</target>
<target name="run">
<java jar="trash/test.jar" fork="true"/>
</target>
</project>
But it doesn't work. When I want to import something from the external .jar, there is an error after command ant compile: package com.something does not exist.. What should I edit to get it working?
Exact error:
Compiling 23 source files to xy/build
xy/src/com/Test.java:5: package com.thoughtworks.xstream does not exist
import com.thoughtworks.xstream.*;
^
1 error
You should try without the includes attribute:
<fileset dir="lib" />
And in the jar part you include the classes like this:
<zipgroupfileset includes="*.jar" dir="lib"/>
You can't put external libraries into a jar and expect the classloader to use those jars. Unfortunately this is not supported.
There are ant tasks like one jar that help you, to create a jar file, that contains everything you need.
This bit is from the background information of one jar:
Unfortunately this is does not work. The Java Launcher$AppClassLoader
does not know how to load classes from a Jar inside a Jar with this
kind of Class-Path. Trying to use
jar:file:jarname.jar!/commons-logging.jar also leads down a dead-end.
This approach will only work if you install (i.e. scatter) the
supporting Jar files into the directory where the jarname.jar file is
installed.
Another approach is to unpack all dependent Jar files and repack them
inside the jarname.jar file. This approach tends to be fragile and
slow, and can suffer from duplicate resource issues.
Other Alternative:
jarjar: Jar Jar Links is a utility that makes it easy to repackage Java libraries and embed them into your own distribution
I also use ant to include a number of dependency JARs in my JAR. My compile task looks like this. Perhaps something similar will work for you.
<target name="compile" depends="init">
<javac srcdir="${src}" destdir="${build}" includeantruntime="false">
<classpath>
<pathelement path="${classpath}" />
<fileset dir="${deps}">
<include name="**/*.jar"/>
</fileset>
</classpath>
</javac>
<copy todir="${build}">
<fileset dir="${src}" excludes="**/*.java"/>
</copy>
</target>
sometimes u can use jar contents directly, just unzip
<unzip src="/Developer-Java/mysql-connector-java/mysql-connector-java-5.1.22-bin.jar" dest="${build_dir}" />

Package Problem with Ant

I'm having a problem getting the javac used by Ant to find and use certain packages. When I invoke javac directly from the command line the packages are found and used.
The .jar files are located in my home directory under lib/java. This is my classpath:
/home/bliskovs/lib/java/*:/home/bliskovs/vendor/cytoscape-v2.7.0/cytoscape.jar
This is the relevant section in my build.xml:
<target name="compile">
<javac srcdir="." debug="true"/>
<javac srcdir="tools/" debug="true"/>
<javac srcdir="core/" debug="true"/>
</target>
How can I get Ant to recognize these packages?
Check out this.
<property name="build.classes.dir" location="build/classes"/>
<path id="compile.classpath">
<fileset dir="lib"/>
<pathelement location="/home/bliskovs/vendor/cytoscape-v2.7.0"/>
</path>
<target name="compile" description="Compile src dir">
<javac destdir="${build.classes.dir}" debug="true" includeantruntime="true">
<src location="src"/>
<classpath refid="compile.classpath"/>
</javac>
</target>
Define a classpath for the javac task. Relying on the CLASSPATH environment variable is a bad practice. It's even more true for the build process of a project, which should work without having to setup a whole lot of environment variables. If you start developing three or four projects at once, you'll understand why using a single CLASSPATH env variable is a bad idea.
See http://ant.apache.org/manual/Tasks/javac.html to know how to define a classpath inside the build.xml and use it in the javac task.

Categories