Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to deal with NoClassDefFoundError 2

Status
Not open for further replies.

aas1611

Programmer
Dec 14, 2001
184
DE
Hello all,

I'm doing this exercise about jsp and beans. Let's get to the point.

Here is part of my java bean code:

Code:
package beans;

import java.util.Calendar;
import java.util.Date;
import DateFormater;

public class DateAnzeige extends Object {
...// rest of the code here
}

I compiled this and everything was fine.

Then, here is my jsp code:

Code:
<html><head><title>beansTest</title></head>
<body>
<%@ page info="JSP bezieht Anrede aus Bean, Version 1.0, &copy; Michael Siebert" %>
<jsp:useBean id="anrede" scope="page" class="beans.DateAnzeige" />
<jsp:setProperty name="anrede" property="anrede" value="Dies ist eine Anrede,<br><blockquote> die der Bean aufgezwungen wurde." />
</blockquote>
<jsp:getProperty name="anrede" property="anrede" />
<%@ include file="/templates/copyright.html" %>
</body></html>

Then I run this bean code in browser, which I found the following error message:
java.lang.NoClassDefFoundError: DateFormater

The DateFormater.java is saved under the same directory as DateAnzeige.java. The beansTest.jsp is in another directory. The beansTest_3.java (the Jasper Engine) only has:
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.jsp.*;

There is no import DateFormater;

Can anybody help me? Any hint will help!
Thanks soo much!
Andre
 
1.- You don't need to import classes that are in the same package/directory

2.- You need to include the jar/directort at runtime.

Cheers,
Dian
 
Hello,

1) On the exercise, I was actually supposed to put the DateFormater.class under another directory called utils, so the import under my java bean is supposed to look like this:

Code:
package beans;

import java.util.Calendar;
import java.util.Date;
import utils.DateFormater;

public class DateAnzeige extends Object {
...// rest of the code here
}

But when I compiled this, error message came up saying that the class DateFormater can not be found, even though I have already put DateFormater.class under utils (That is why I took off the utils, which did not help, either).

Does it have anything to do with classpath in the environment variables?

FYI, my java bean is supposed to be in directory
<TOMCAT_HOME>/webapps/learning/beans

and DateFormater.class is under
<TOMCAT_HOME>/webapps/learning/beans/utils

and my jsp code is under
<TOMCAT_HOME>/webapps/learning/jsp

2) Which jar do you mean? How to do that?


Thanks!
Andre
 
Should it not be :

import beans.utils.DateFormater;

then ?

--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
 
I think imports can be relative to actual package directory. Otherwise, the compiler will barf with a no package found.

But follow sedj's advice, that will keep things clearer.

Cheers,
Dian
 
Oh,

I was giving you the wrong directories.

The directory for DateFormater.class is:
<TOMCAT_HOME>/webapps/learning/WEB-INF/classes/utils

and, the java bean (DateAnzeige.java) and DateFormater.java are:
<TOMCAT_HOME>/webapps/learning/WEB-INF/classes/beans

So I think this import under DateAnzeige.java
Code:
import utils.DateFormater;
stays, right?

Here is my DateFormater.java code:
Code:
package utils;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

public class DateFormater {
...
}

Here is how I compilde DateFormater:
javac -d C:\jakarta\jakarta-tomcat-3.3\webapps\learning\WEB-INF\classes DateFormater.java

Any idea why it still doesn't work?

Andre
 
You need to compile the code from the correct directory for a kick off.

EG :

cd C:\jakarta\jakarta-tomcat-3.3\webapps\learning\WEB-INF\classes
javac utils\*.java
javac beans\*.java

--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
 
That was it. It worked now. Thanks!

So, the conclusion is, we have to put the code and compile it from the directory where the package is, which means the .java and its .class have to be in the same directory, is that it?

In this case here, the package code in my DateFormater.java is
package utils;

that means I have to compile it from utils directory, in order for DateAnzeige.java (which is in beans dir) to recognize
import utils.DateFormater;

Is that how it works (both with or without package)?

Andre
 
You should compile from the root dir of your packages.

Cheers,
Dian
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top