-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: How to include jar files from lib folder?
PostPosted: Tue Aug 31, 2004 2:51 pm 
Beginner
Beginner

Joined: Thu Jul 29, 2004 7:14 pm
Posts: 41
I am trying to create a simple Hibernate application (the Message application from Hibernate in Action Book) using NotePad. My environment is Hibernate 2.1.4, MS SQL Server 2000 under Windows XP. The Hibernate is installed in C:\MyPrograms\hibernate folder and my CLASSPATH is set to ".;%JAVA_HOME%\lib\tools.jar;%HIBERNATE_HOME%\hibernate2.jar". The source code of the files are at the end.

I created a folder c:\Examples\HIA. There I created Message.java and Message.hbm.xml files. In MS SQLServer, I created a database and create a table MESSAGES in it. I then created an ODBC connection for that database. I then configure the hibernate.properties to connect to my database and put this file in C:\Examples\HIS folder. Now I compile the Message.java file at C:\Examples\HIS folder by typing "javac -d . Message.java" at command prompt. It creates the hello folder and put the Message.class file in it. So far good. Now I created another file TestMessage.java which saves a message to the database. I was able to compile it successfully by typing "javac TestMessage.java". When I try to run it by typing "java TestMessage.java", I got this error message :
Exception in thread "main" java.lang.NoClassDefFoundError: org/dom4j/Attribute at TestMessage.main(TestMessage.java:10)

I can see that this error message is happening because the %HIBERNATE_HOME%\lib has some jar files which needs to be included in the classpath. The problem is there are 33 of them and i dont want to include them manually in my CLASSPATH. Is there any way to solve this problem?

Thanks



Message.java
=========

package hello;

import java.io.Serializable;

public class Message implements Serializable {
private Long id;
private String text;
private Message nextMessage;

public Message() {}
public Message(String text) {
this.text = text;
}

public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
public Message getNextMessage() {
return nextMessage;
}
public void setNextMessage(Message nextMessage) {
this.nextMessage = nextMessage;
}
}


Message.hbm.xml
============

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping>
<class name="hello.Message" table="MESSAGES">
<id
name="id"
column="MESSAGE_ID">
<generator class="increment"/>
</id>
<property
name="text"
column="MESSAGE_TEXT"/>
<many-to-one
name="nextMessage"
cascade="all"
column="NEXT_MESSAGE_ID"/>
</class>
</hibernate-mapping>


TestMessage.java
============
import hello.Message;
import net.sf.hibernate.cfg.Configuration;
import net.sf.hibernate.SessionFactory;
import net.sf.hibernate.Session;
import net.sf.hibernate.Transaction;

public class TestMessage {
public static void main(String[] args) throws Exception
{
Configuration cfg = new Configuration()
.addClass(hello.Message.class);

SessionFactory factory = cfg.buildSessionFactory();

Session session = factory.openSession();

Transaction tx = session.beginTransaction();

Message message = new Message("Hello World!");

session.save(message);

tx.commit();

session.close();
}
}


Top
 Profile  
 
 Post subject: Re: How to include jar files from lib folder?
PostPosted: Tue Aug 31, 2004 4:38 pm 
Proxool Developer
Proxool Developer

Joined: Tue Aug 26, 2003 10:42 am
Posts: 373
Location: Belgium
hussains wrote:
I can see that this error message is happening because the %HIBERNATE_HOME%\lib has some jar files which needs to be included in the classpath. The problem is there are 33 of them and i dont want to include them manually in my CLASSPATH. Is there any way to solve this problem?


Read the docs that comes with the Java JDK... that's where you should look at - not at Hibernate.

Hint: java -h


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 31, 2004 6:02 pm 
Beginner
Beginner

Joined: Thu Jul 29, 2004 7:14 pm
Posts: 41
thanks. I already solve it by building an ANT script. I will look at the JDK help too.

<?xml version="1.0" encoding="GB2312" ?>
<project name="bookexample" default="compilejavafiles" basedir=".">
<target name="init">
<property name="bookexamplehome" value="C:\Examples\HIA"/>
<path id="compile.classpath">
<fileset dir="${bookexamplehome}/lib">
<include name="*.jar"/>
</fileset>
</path>
</target>
<target name="compilejavafiles" description="Compile Java files" depends="init">
<javac srcdir="."
destdir=".">
<classpath refid="compile.classpath"/>
<include name="**/*.java"/>
</javac>
</target>
</project>


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.