ClassDefNotFound Exception

S

Steve Burrus

I am wondering just why i seem to get the old Java "ClassDefNotFound
Exception" so much when I either try to compile or execute a Java source
code file when I KNOW that I have everything about the Java installation
properly installed and configured!!!
 
G

Gordon Beaton

I am wondering just why i seem to get the old Java "ClassDefNotFound
Exception" so much when I either try to compile or execute a Java
source code file when I KNOW that I have everything about the Java
installation properly installed and configured!!!

Here's a good rule of thumb: When you KNOW that you're doing
everything correctly but it still doesn't work, then something you
know might very well be WRONG.

Of course there are extremly few clues in your post to give the
slightest indication of what you might be doing wrong. Are you asking
us to guess why it doesn't work?

Have a look here for some clues:

http://www.catb.org/~esr/faqs/smart-questions.html
http://www.physci.org/codes/sscce.jsp
http://www.yoda.arachsys.com/java/packages.html

Here are two issues with your problem description that may be related
to the problem itself:

- are you really trying to execute a *source* code file?
- do you really get ClassDefNotFoundException when you *compile*?

/gordon
 
S

Steve Burrus

Gordon Beaton wrote:

Of course there are extremly few clues in your post to give the
slightest indication of what you might be doing wrong. Are you asking
us to guess why it doesn't work?

Okay Gordon, to both respond back to your apparent need to know some
details about this exception, AND to maybe "re-activate" this particular
thread which, I noticed, hasn't seen much activity on since early this
AM, here is a "PortScanner.java" file which has given me the exception
when i try to execute it, i.e., use the java interpreter on it, not
necessarily when I try to compile it, which it does okay.

"import java.io.*;
import java.net.*;

/**
* Port Scanner
*
* Scans for servers listening on a range of ports.
*/
public class PortScanner
{
final static int LOW_RANGE = 0; //scan reserved ports
final static int HIGH_RANGE = 1023;

public static void main(String[] args)
{
Socket connect=null;
String host = "localhost"; //defaults to local host

if (args.length > 0)
{
host = args[0]; //sets host to command-line parameter
}
for (int iCount = LOW_RANGE; iCount < HIGH_RANGE; iCount++)
{
System.out.print("Checking port " + iCount + "...");
try
{
connect = new Socket(host, iCount);
System.out.println("Server responding on port " +
iCount + " of " + host);
}
catch (UnknownHostException e)
{
System.err.println("Host is invalid.");
break;
}
catch (IOException e)
{
System.out.println("No server found");
}
finally
{
try
{
connect.close(); //close socket connection
}
catch (Exception e) {}
}
}
}
}"
 
G

Gordon Beaton

Okay Gordon, to both respond back to your apparent need to know some
details about this exception, AND to maybe "re-activate" this
particular thread which, I noticed, hasn't seen much activity on
since early this AM, here is a "PortScanner.java" file which has
given me the exception when i try to execute it, i.e., use the java
interpreter on it, not necessarily when I try to compile it, which
it does okay.

Well, your code works fine for me.

Probably you are doing something wrong. Show us the command line you
use run the program, and exactly what the error message looks like.

Try this from the directory containing PortScanner.class:

java -cp . PortScanner

/gordon
 
S

Steve Burrus

Gordon Beaton wrote:
Well, your code works fine for me.
Probably you are doing something wrong. Show us the command line you
use run the program, and exactly what the error message looks like.

Try this from the directory containing PortScanner.class:

java -cp . PortScanner

"Success" Gordon is what I can say I achieved with your solution which u
offered to me, so thanx for that!! BTW : what is that -cp interpreter
switch that got it going for me in DOS??? And also, just generally, what
should I do the next time that I might encounter that ClassDefNotFound
exception either trying to compile or run a java file?
 
G

Gordon Beaton

"Success" Gordon is what I can say I achieved with your solution
which u offered to me, so thanx for that!! BTW : what is that -cp
interpreter switch that got it going for me in DOS???

It adds the current directory to the classpath so the JVM can find
your class. It's in the documentation, which I suggest you have a look
at:

http://java.sun.com/j2se/1.4.2/docs/tooldocs/windows/java.html
And also, just generally, what should I do the next time that I
might encounter that ClassDefNotFound exception either trying to
compile or run a java file?

Add the relevant directories to your classpath.

Also have a look at the following link, which I posted in my first
reply. It will come in handy when you start to use package
declarations in your code and start getting that exception again:

http://www.yoda.arachsys.com/java/packages.html

/gordon
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top