My Folder sructure looks like this
Hibernate
|
ant |
build.xml
|
bin
|
aniStart
|
contact.class
FirstExample.class
Instrument.class
|
jar
|
HibernateAnt.jar
|
contact.hbm.xml
instrument.hbm.xml
hibernate.cfg.xml
|
lib
|
All the jar files
|
src
|
aniStart
|
Contact.java
Instrument.java
FirstExample.java
Before I say something,I must say that my project runs smoothly(entries are made into the Database table) when I run it via ECLIPSE Run,when I use ANT script,all hell
breaks loose.
My build.xml
-------------
<?xml version="1.0" encoding="UTF-8"?>
<project name="Hibernate" default="deploy" basedir=".">
<target name="init">
<property name="sourceDir" value="../src/aniStart" />
<property name="outputDir" value="../bin" />
</target>
<target name="clean" depends="init">
<delete dir="${outputDir}/aniStart" />
<delete dir="${outputDir}/jar" />
</target>
<!-- Libraries to use for build (relative to ${lib.dir} -->
<path id="lib">
<fileset dir="../lib" includes="**/*.jar" />
<fileset dir="../lib" includes="**/*.zip" />
</path>
<!-- *** Classpath definitions ***************************************** -->
<path id="build.classpath">
<path refid="lib"/>
<pathelement location="${outputDir}" />
</path>
<target name="compile" depends="clean">
<javac executable="${javaHome}/javac" srcdir="${sourceDir}" destdir="${outputDir}"
classpathref="build.classpath"/>
</target>
<target name="makeJar" depends="compile">
<mkdir dir="${outputDir}/jar"/>
<jar destfile="${outputDir}/jar/HibernateAnt.jar" basedir="${outputDir}">
<fileset dir="${outputDir}">
<include name="${outputDir}/antTry/*.class"/>
</fileset>
<manifest>
<attribute name="Main-Class" value="aniStart.FirstExample"/>
</manifest>
</jar>
</target>
<target name="deploy" depends="makeJar">
<
java jar="${outputDir}/jar/HibernateAnt.jar" fork="true"/>
</target>
</project>
FirstExample.java
-----------------------
package aniStart;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
public class FirstExample {
public static void main(
String[] args) throws Exception {
System.out.println("ComeOn");
FirstExample frstEx = new FirstExample();
Session session = null;
try {
session = frstEx.currentSession();
Transaction tx = session.beginTransaction();
Contact contact = frstEx.insertContact();
Instrument instr = frstEx.insertInstru(contact);
frstEx.save(session, contact);
frstEx.save(session, instr);
tx.commit();
} catch (RuntimeException e) {
e.printStackTrace();
}
finally{
if (session != null){
session.close();
}
}
}
public Session currentSession() throws HibernateException{
SessionFactory sesFac = null;
Session ses = null;
try {
sesFac = new Configuration().configure().buildSessionFactory();
} catch (HibernateException e) {
throw new RuntimeException("In here",e);
}
ses = sesFac.openSession();
return ses;
}
public Contact insertContact(){
Contact contact = new Contact();
contact.setId(System.currentTimeMillis());
contact.setEmail("ani@yahoo.com");
contact.setFirstName("Ani");
contact.setLastName("Chowdhury");
return contact;
}
public void save(Session session,Object object){
//Contact contact = (Contact)object;
session.save(object);
}
public Instrument insertInstru(Contact contact){
Instrument instr = new Instrument();
instr.setId(System.currentTimeMillis());
instr.setName("Guitar");
instr.setExperience("2");
instr.addToContact(contact);
return instr;
}
}
This is the output I get
Buildfile: D:\ani_Work_Hibe\Hibernate\ant\build.xml
init:
clean:
[delete] Deleting directory D:\ani_Work_Hibe\Hibernate\bin\aniStart
[delete] Deleting directory D:\ani_Work_Hibe\Hibernate\bin\jar
compile:
[javac] Compiling 3 source files to D:\ani_Work_Hibe\Hibernate\bin
[javac] Note: D:\ani_Work_Hibe\Hibernate\src\aniStart\Instrument.java uses unchecked or unsafe operations.
[javac] Note: Recompile with -Xlint:unchecked for details.
makeJar:
[mkdir] Created dir: D:\ani_Work_Hibe\Hibernate\bin\jar
[jar] Building jar: D:\ani_Work_Hibe\Hibernate\bin\jar\HibernateAnt.jar
deploy:
[java] java.lang.NoClassDefFoundError: org/hibernate/HibernateException
[java] Caused by: java.lang.ClassNotFoundException: org.hibernate.HibernateException
[java] at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
[java] at java.security.AccessController.doPrivileged(Native Method)
[java] at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
[java] at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
[java] at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:276)
[java] at java.lang.ClassLoader.loadClass(ClassLoader.java:251)
[java] at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:319)
[java] Exception in
thread "main"
[java] Java Result: 1
BUILD SUCCESSFUL
Total time: 1 second
I am using Hibernate3.jar,commons-logging-1.0.4.jar.
Can someone help me please??I really want to know what is going wrong here?
Apart from my current problem,If anyone has any other suggestions,they will be appreciated.Thanks a lot in advance