Ant java task NoClassDefFoundError - ant

I want to make a simple ant build hibernate test project.
There is no error during the compilation and the build (jar).
But when I run it I get this:
java.lang.NoClassDefFoundError: org/hibernate/cfg/Configuration ...
I have found an advise, what said : jars should be added to classpath in the command line, (classpath is ignored when the jar run from ant ... ehh), ok I tried the following:
java -jar dist/student.jar -cp /home/myname/workspace/basic_ant1/lib/hibernate/hibernate-core-4.2.8.Final.jar
But still have the some error :NoClassDefFoundError ...
What did I wrong ?
Thanks for the replies in advance.
(org.hibernate.cfg is in hibernate-core-4.2.8.Final.jar)
Cs.

-jar and -cp are mutually exclusive.
If you want to use java -jar then your main JAR file needs a Class-Path entry in its manifest that points to all the other jars its Main-Class requires (the manifestclasspath task is a handy way to generate this value).
If you use java -cp then you have to give the main class name on the command line, the Main-Class from the manifest is ignored.

Related

Error while running TestNG using bat file

Why this error:
Error: Could not find or load main class org.testng.TestNG
Occur while running TestNG using bat file?
You must provide classpath to testng jar using -cp argument. also for other dependent jars like selenium and test classes (project)
Try the following command:
java -cp J:\temp\testng\*;J:\temp\workspace\TestNGExamples\bin;J:\automation\* org.testng.TestNG J:\temp\workspace\TestNGExamples\testng1.xml
Note: please update the paths as per your machine.
For -cp argument, given the directory paths to
testng jar
test classes (project bin directory)
selenium jar

executable jar from groovy sources

I could not find a proper answer to the following question : while creating an executable jar from groovy compiled sources, I cannot simply include groovy-all-version.jar in a lib directory (as I would do in java) but I need to extract all the classes from the jar and package them with my own classes as otherwise I get an exception. This actually is the case with all libraries I may want to use.
Not clear ? Let's see an example.
toto.groovy belonging to package titi is compiled in a build directory which looks like this :
build
lib
groovy-all-1.8.4.jar
titi
toto.class
and a MANIFEST.MF that will be included in the jar contains the lines:
Main-Class: titi.toto
Class-Path: lib/groovy-all-1.8.4.jar
Executable jar creation is done with the following ant target in build.xml:
<jar destfile="${build.exe.dir}/${exe.name}"
basedir="${build.dir}"
manifest="${src.conf.dir}/MANIFEST.MF"
/>
Now if I try to execute the jar, I get
$ java -jar target/toto.jar
Exception in thread "main" java.lang.NoClassDefFoundError: groovy/lang/Script
(I've checked that the groovy.lang.Script class exists.)
On the other hand, if I unjar groovy-all-version.jar in build by adding an unjar task before the jar, execution goes fine. OK, it works, but it slows down the compilation process and I'd like to understand...
Any feedback is highly appreciated.

how do i run command-line ant with stuff in the classpath?

In eclipse, I can tell my external ant tool to run with stuff in the classpath.
If i want to run ant from the command line, how would i do that?
For argument's sake, the classpath i want to add is c:\some\folder\here\hooray.jar
Use the -lib argument. From the Ant docs on this page:
Additional directories to be searched may be added by using the -lib option. The -lib option specifies a search path. Any jars or classes in the directories of the path will be added to Ant's classloader.

How can one request a debug build from javac without changing the ant scripts?

I have a complex build system involving many ant scripts, some targets of which invoke the javac task.
These ant scripts do not provide for a way to request a debug build from javac, i.e. neither debug nor debuglevel parameters of the javac task are specified.
Is it still possible to instruct javac to build with debugging support without changing the build scripts themselves?
The scripts are invoked from console.
The short answer is no unfortunately :(
Are the Ant scripts invoking javac by calling an external executable, i.e., is fork=true? If so, you could try putting a wrapper script named javac earlier in the $PATH. A tool like strace could show you exactly how javac is being invoked.
On Unix it could look something like this:
echo '#!/bin/bash
exec /usr/bin/javac -g "${#}"' > ./javac
chmod +x javac
PATH=".:${PATH}" ant build
If Ant is invoking the Java compiler in-process, you can make an edited version of Ant's javac task class that defaults to having debugging turned on, and put this earlier than the official Ant jar in the classpath when invoking Ant.

"JUnitTask was not found" error (Hudson, Ant, JUnit)

I tried several ways to install ant and junit on the same server as hudson. I eventually downloaded the Ant source, installed the optional dependencies by using the included fetch.xml file, built the source code, and dropped junit-4.8.1.jar into the lib directory where the compiled ant code [that I just built] is. Then I set ANT_HOME to the ant root directory by using the "export ANT_HOME=/usr/share/ant" command. I also set a Hudson configuration variable of "ANT_HOME" with the same value (/usr/share/ant) and I also put a new file into the /etc/profile.d directory with the export ANT_HOME command as well. At this point I'm not sure what else to try, but Hudson still gives me the error:
BUILD FAILED /var/lib/hudson/jobs/MyProject/build.xml:31: Problem:
failed to create task or type junit Cause: the class
org.apache.tools.ant.taskdefs.optional.junit.JUnitTask was not found.
This looks like one of Ant's optional components. Action: Check that the appropriate optional JAR exists in
-/usr/share/ant/lib
-/var/lib/hudson/.ant/lib
-a directory added on the command line with the -lib argument
Do not panic, this is a common problem. The commonest cause is a
missing JAR.
This is not a bug; it is a configuration problem
I did, in fact, check /usr/share/ant/lib and the class "org.apache.tools.ant.taskdefs.optional.junit.JUnitTask.class" is located in the ant-junit jar file as I confirmed by running the command: jar tfv ant-junit.jar | grep org.apache.tools.ant.taskdefs.optional.junit.JUnitTask
Can someone please tell me what to try, I am not sure where to go from here.
If you are on Ubuntu you can simply apt-get ant; I did that and linked to the junit jar within my project.
Cheers,
Neil
It turns out that hudson itself installs some version of Ant in one of its own libraries. I suppose that somehow caused a conflict because when I got rid of it, things worked. I think it is in /var/hudson or /var/hudson/lib. That path is added to the PATH environment variable during hudson installation. So your choice is either to add your own Ant install to the BEGINNING of the path, to delete hudson's version of Ant, or to delete that part of the PATH.

Resources