473,836 Members | 1,585 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

java.lang.NoCla ssDefFoundError

Lem
I get the error
Exception in thread "main" java.lang.NoCla ssDefFoundError

when I type java app2 in the command prompt. I've tried moving to the jre
directory and typed java c:\app2\app2, but it gives me the same exception.
What could be the problem?

Thanks in advance,
Lem
Jul 17 '05
11 167125
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

<posted & mailed>

Peter Bradley wrote:
Hi guys and gals,

I've never understood $CLASSPATH and packages and friends - but I
had the same problem, which is why I'm reading this thread.

After looking at the replies (and remembering some odd experiences
I'd had with a previous excursion into Javaland) I tried calling the
program from the directory above where it resides.

To illustrate. Here's the code:

<codeextract>

package ConnectorJTest;

import java.sql.Connec tion;
import java.sql.Driver Manager;
import java.sql.SQLExc eption;

public class LoadDriver {

public static void main(String[] args) {
try {
Class.forName(" com.mysql.jdbc. Driver").newIns tance();
System.out.prin tln("Success!") ;
} catch (Exception ex) {
// Handle the error
System.out.prin tln("Failure!") ;
}
}
}

</codeextract>

The program is in
/home/peter/connector_j_tes t/ConnectorJTest/LoadDriver.clas s

Now, if I call the program by going to the ConnectorJTest and doing:

java LoadDriver

I get:

<output>

peter@linux:~/connector_j_tes t/ConnectorJTest> java LoadDriver
Exception in thread "main" java.lang.NoCla ssDefFoundError :
LoadDriver (wrong name: ConnectorJTest/LoadDriver)
at java.lang.Class Loader.defineCl ass0(Native Method)
at java.lang.Class Loader.defineCl ass(ClassLoader .java:502)
at
java.security.S ecureClassLoade r.defineClass(S ecureClassLoade r.java:123) at
java.net.URLCla ssLoader.define Class(URLClassL oader.java:250)
at
java.net.URLCla ssLoader.access $100(URLClassLo ader.java:54)
at java.net.URLCla ssLoader$1.run( URLClassLoader. java:193) at
java.security.A ccessController .doPrivileged(N ative Method)
at
java.net.URLCla ssLoader.findCl ass(URLClassLoa der.java:186)
at java.lang.Class Loader.loadClas s(ClassLoader.j ava:299) at
sun.misc.Launch er$AppClassLoad er.loadClass(La uncher.java:265 ) at java.lang.Class Loader.loadClas s(ClassLoader.j ava:255) at
java.lang.Class Loader.loadClas sInternal(Class Loader.java:315 )
</output>

And if I try the fully qualified name from the same directory, I
get:

<output>

peter@linux:~/connector_j_tes t/ConnectorJTest> java
ConnectorJTest. LoadDriver
Exception in thread "main" java.lang.NoCla ssDefFoundError :
ConnectorJTest/LoadDriver

</output>

However if I move up a directory and give the fully qualified name,
I get:

<output>
peter@linux:~/connector_j_tes t> java ConnectorJTest. LoadDriver
Success!

</output>

Any explanation (preferably rational, though :) ) would be
gratefully received

Cheers

Peter


Hi,
After working with Java for a while, I seem to understand the
CLASSPATH/packages thing. Here goes:

Every class has a package. If one isn't specified when it's created,
it's in the root package, but it still "has" a package.

If you create the following file in the current directory:

package com.somecompany .test;

public class Test {
public static void main(String[] args) {
System.out.prin tln("Hello World!");
}
}

and run javac on it directly:
javac Test.java
It produces a file called Test.class in the current directory, which
fails utterly when you try to run it. See, each piece of the package
name is supposed to correspond to a physical directory name in the
filesystem. When you run javac as above, it outputs the class file in
the current directory. If, on the other hand, you were to put the
above source into a file com/somecompany/test/Test.java, staying in
the same directory (not descending in deeper), compiling it would
create com/somecompany/test/Test.class, which you could then run by
typing "java com.somecompany .test.Test". The idea is that by using
the filesystem like this, packages are useful. If two companies make
two classes with the same name, they put them in different packages.
Thus, inside the VM, everyone is sure which class is being referred
to. By using directories in the filesystem, both classes can be put
in "the same place" but they won't overwrite each other.

If you type in "java com.somecompany .test.Test", here's what happens:

The JVM needs to find the class. Ignoring the jre/lib/ext directory
for the time being, we'll assume a CLASSPATH variable set to
/home/me/java. The JVM does this internally:

REALPATH=$CLASS PATH/$PACKAGENAME/$CLASSNAME
Thus, the real path is
/home/me/java/com/somecompany/test/Test.class
which exists.

Why it doesn't work moving into that directory and just typing "java
Test", is because the class file has the package name that it was
meant to be inside of embedded when it's compiled. The JVM detects
that although it seems to be able to find the class, it must be the
wrong file because by typing the above command line, you're asking
for a class in the root package, and the file it found isn't in the
root package. It assumes something is wrong.

Now, personally, I don't actually even have the CLASSPATH set. This
means the JVM defaults to "." as its value. Thus:

java com.somecompany .test.Test

loads

../com/somecompany/test/Test.class

which contains the package name inside the file:

com.somecompany .test

which the JVM checks against the command line, and sees that it
matches, so everything is good. Thus, I can just invoke any class I
need by changing to its root directory (which, of course, isn't
necessarily the directory containing the class file--in the case of
packages, it's the directory containing the highest-level package
name).

For libraries, I find, easier than downloading the JAR file and adding
it to the CLASSPATH, just put it into the jre/lib/ext directory.
Every JAR file in jre/lib/ext is automatically put in the classpath.
Thus, if I use the MySQL connector, and the JVM tries to find this
class:

com.mysql.jdbc. Driver

it takes the classpath element like this (filename shortened):

/usr/java/j2sdk1.4.2/jre/lib/ext/mysql.jar

appends the names just like I described previously, producing this
physical path:

/usr/java/j2sdk1.4.2/jre/lib/ext/mysql.jar/com/mysql/jdbc/Driver

which looks ridiculous, since it describes a directory inside a file,
but, what happens, is that inside the mysql.jar file is a zipped
directory called com, which contains a directory called mysql, etc.
etc. etc. and everything works.

Basically, just remember:

- - When invoking a class that is in a package, you have to include the
package name on the command line.

- - When invoking a class that is in a package, the class file needs to
be contained in a directory structure matching the package name, OFF
OF whatever element in CLASSPATH is relevant. If there's a package
name, the class file isn't going to be right in the directory, it'll
be in a subdirectory.

- - To include a JAR file, just put the path to the JAR itself in the
CLASSPATH, and the file acts like a root directory in terms of
packaging. It doesn't work just putting the directory containing the
JAR in your classpath, primarily because that would imply the name of
the JAR file is part of the package name (good luck trying to get the
".jar" part to go into the package name without being converted into
a subdirectory <VBG>)

Rational explanation? Lunatic rambling? Probably both. Hope it helped
though :)

- --
Chris
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.2 (GNU/Linux)

iD8DBQE/KYnBwxczzJRavJY RAvGGAKCy9PNnjX fk5jtxHm9utwrGB ZXbzgCglmIS
+gbxA2LvEiioKie WoT9jsxY=
=s0Na
-----END PGP SIGNATURE-----
Jul 17 '05 #11
Chris,

If you are ever unable to get development work, go into teaching.
Great! I see it all now. Couldn't be clearer.

Many thanks.

Peter

Chris wrote:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

<posted & mailed>

Peter Bradley wrote:

Hi guys and gals,

I've never understood $CLASSPATH and packages and friends - but I
had the same problem, which is why I'm reading this thread.

After looking at the replies (and remembering some odd experiences
I'd had with a previous excursion into Javaland) I tried calling the
program from the directory above where it resides.

To illustrate. Here's the code:

<codeextrac t>

package ConnectorJTest;

import java.sql.Connec tion;
import java.sql.Driver Manager;
import java.sql.SQLExc eption;

public class LoadDriver {

public static void main(String[] args) {
try {
Class.forName(" com.mysql.jdbc. Driver").newIns tance();
System.out.prin tln("Success!") ;
} catch (Exception ex) {
// Handle the error
System.out.prin tln("Failure!") ;
}
}
}

</codeextract>

The program is in
/home/peter/connector_j_tes t/ConnectorJTest/LoadDriver.clas s

Now, if I call the program by going to the ConnectorJTest and doing:

java LoadDriver

I get:

<output>

peter@linux :~/connector_j_tes t/ConnectorJTest> java LoadDriver
Exception in thread "main" java.lang.NoCla ssDefFoundError :
LoadDriver (wrong name: ConnectorJTest/LoadDriver)
at java.lang.Class Loader.defineCl ass0(Native Method)
at java.lang.Class Loader.defineCl ass(ClassLoader .java:502)
at


java.security.S ecureClassLoade r.defineClass(S ecureClassLoade r.java:123)
at
java.net.URLCla ssLoader.define Class(URLClassL oader.java:250)
at
java.net.URLCla ssLoader.access $100(URLClassLo ader.java:54)
at java.net.URLCla ssLoader$1.run( URLClassLoader. java:193) at
java.security.A ccessController .doPrivileged(N ative Method)
at
java.net.URLCla ssLoader.findCl ass(URLClassLoa der.java:186)
at java.lang.Class Loader.loadClas s(ClassLoader.j ava:299) at


sun.misc.Launch er$AppClassLoad er.loadClass(La uncher.java:265 )
at java.lang.Class Loader.loadClas s(ClassLoader.j ava:255) at


java.lang.Class Loader.loadClas sInternal(Class Loader.java:315 )
</output>

And if I try the fully qualified name from the same directory, I
get:

<output>

peter@linux :~/connector_j_tes t/ConnectorJTest> java
ConnectorJTes t.LoadDriver
Exception in thread "main" java.lang.NoCla ssDefFoundError :
ConnectorJTes t/LoadDriver

</output>

However if I move up a directory and give the fully qualified name,
I get:

<output>
peter@linux :~/connector_j_tes t> java ConnectorJTest. LoadDriver
Success!

</output>

Any explanation (preferably rational, though :) ) would be
gratefully received

Cheers

Peter

Hi,
After working with Java for a while, I seem to understand the
CLASSPATH/packages thing. Here goes:

Every class has a package. If one isn't specified when it's created,
it's in the root package, but it still "has" a package.

If you create the following file in the current directory:

package com.somecompany .test;

public class Test {
public static void main(String[] args) {
System.out.prin tln("Hello World!");
}
}

and run javac on it directly:
javac Test.java
It produces a file called Test.class in the current directory, which
fails utterly when you try to run it. See, each piece of the package
name is supposed to correspond to a physical directory name in the
filesystem. When you run javac as above, it outputs the class file in
the current directory. If, on the other hand, you were to put the
above source into a file com/somecompany/test/Test.java, staying in
the same directory (not descending in deeper), compiling it would
create com/somecompany/test/Test.class, which you could then run by
typing "java com.somecompany .test.Test". The idea is that by using
the filesystem like this, packages are useful. If two companies make
two classes with the same name, they put them in different packages.
Thus, inside the VM, everyone is sure which class is being referred
to. By using directories in the filesystem, both classes can be put
in "the same place" but they won't overwrite each other.

If you type in "java com.somecompany .test.Test", here's what happens:

The JVM needs to find the class. Ignoring the jre/lib/ext directory
for the time being, we'll assume a CLASSPATH variable set to
/home/me/java. The JVM does this internally:

REALPATH=$CLASS PATH/$PACKAGENAME/$CLASSNAME
Thus, the real path is
/home/me/java/com/somecompany/test/Test.class
which exists.

Why it doesn't work moving into that directory and just typing "java
Test", is because the class file has the package name that it was
meant to be inside of embedded when it's compiled. The JVM detects
that although it seems to be able to find the class, it must be the
wrong file because by typing the above command line, you're asking
for a class in the root package, and the file it found isn't in the
root package. It assumes something is wrong.

Now, personally, I don't actually even have the CLASSPATH set. This
means the JVM defaults to "." as its value. Thus:

java com.somecompany .test.Test

loads

./com/somecompany/test/Test.class

which contains the package name inside the file:

com.somecompany .test

which the JVM checks against the command line, and sees that it
matches, so everything is good. Thus, I can just invoke any class I
need by changing to its root directory (which, of course, isn't
necessarily the directory containing the class file--in the case of
packages, it's the directory containing the highest-level package
name).

For libraries, I find, easier than downloading the JAR file and adding
it to the CLASSPATH, just put it into the jre/lib/ext directory.
Every JAR file in jre/lib/ext is automatically put in the classpath.
Thus, if I use the MySQL connector, and the JVM tries to find this
class:

com.mysql.jdbc. Driver

it takes the classpath element like this (filename shortened):

/usr/java/j2sdk1.4.2/jre/lib/ext/mysql.jar

appends the names just like I described previously, producing this
physical path:

/usr/java/j2sdk1.4.2/jre/lib/ext/mysql.jar/com/mysql/jdbc/Driver

which looks ridiculous, since it describes a directory inside a file,
but, what happens, is that inside the mysql.jar file is a zipped
directory called com, which contains a directory called mysql, etc.
etc. etc. and everything works.

Basically, just remember:

- - When invoking a class that is in a package, you have to include the
package name on the command line.

- - When invoking a class that is in a package, the class file needs to
be contained in a directory structure matching the package name, OFF
OF whatever element in CLASSPATH is relevant. If there's a package
name, the class file isn't going to be right in the directory, it'll
be in a subdirectory.

- - To include a JAR file, just put the path to the JAR itself in the
CLASSPATH, and the file acts like a root directory in terms of
packaging. It doesn't work just putting the directory containing the
JAR in your classpath, primarily because that would imply the name of
the JAR file is part of the package name (good luck trying to get the
".jar" part to go into the package name without being converted into
a subdirectory <VBG>)

Rational explanation? Lunatic rambling? Probably both. Hope it helped
though :)

- --
Chris
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.2 (GNU/Linux)

iD8DBQE/KYnBwxczzJRavJY RAvGGAKCy9PNnjX fk5jtxHm9utwrGB ZXbzgCglmIS
+gbxA2LvEiioKie WoT9jsxY=
=s0Na
-----END PGP SIGNATURE-----


Jul 17 '05 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
9127
by: Andy Howells | last post by:
Can anybody help me on this? I am getting the below error but have not got a clue why. The file in my classpath eing used has the class that it says is not defined. Any ideas? I am running java version 1.4.0 and WMQ Series version 5.3 with CSD04. Exception in thread "main" java.lang.NoClassDefFoundError: com/ibm/mq/server/MQSESSION at com.ibm.mq.MQSESSIONServer.getMQSESSION(MQSESSIONServer.java:67) at...
0
4512
by: Shawn | last post by:
I am getting the following error with a Java Applet being served out by IIS over HTTPS/SSL using a Verisign certificate: java.lang.NoClassDefFoundError: javax/help/HelpSetException at java.lang.Class.getDeclaredConstructors0(Native Method) at java.lang.Class.privateGetDeclaredConstructors(Class.java:1590) at java.lang.Class.getConstructor0(Class.java:1762) at java.lang.Class.newInstance0(Class.java:276) at...
3
6538
by: NathanK | last post by:
Hi Guys, im starting out in java and i'm getting the error: Exception in thread "main" java.lang.NoClassDefFoundError: blurf if i run "javac blurf.java", it compiles fine and without any troubles. when i try to run it (its a simple hello world program) i get the above error.
1
47616
by: greg.knaddison | last post by:
Hi, I'm trying to use the httpclient within Jython (see http://jakarta.apache.org/commons/httpclient/ for more information on the httpclient). My Jython version is: Jython 2.1 on java1.4.2_04 (JIT: null) My Java version is:
0
3722
by: HiteshMehra | last post by:
Hi all, I am getting an error while trying to connect to the MQ. The error is java.lang.NoClassDefFoundError: com/ibm/mq/server/MQSESSION at com.ibm.mq.MQSESSIONServer.getMQSESSION(MQSESSIONServer.java:68) at com.ibm.mq.MQSESSION.getSession(MQSESSION.java:493) at com.ibm.mq.MQManagedConnectionJ11.<init>(MQManagedConnectionJ11.java:155) at...
4
14747
by: jmitch89 | last post by:
I don't why I get this error: Exception in thread "main" java.lang.NoClassDefFoundError The statement below works just fine: java -cp "appframework-1.0.3.jar;swing-worker-1.1.jar";CurrentStrobe.jar com.visionpro.currentstrobe.CurrentStrobeApp However, the statement below produces the error: java -cp "appframework-1.0.3.jar;swing-worker-1.1.jar" -jar CurrentStrobe.jar Exception in thread "main"...
4
5123
by: xtremebass | last post by:
Hi Bytes, i run the sample java code(BarChartDemo.java)which is producing the Bar charts as output if no error in the program, but i got run time error like this, java.lang.NoClassDefFoundError: BarChartDemo (wrong name: org/jfree/chart/demo/BarChartDemo) at java.lang.ClassLoader.defineClass1(Native Method) at java.lang.ClassLoader.defineClass(Unknown Source) at java.security.SecureClassLoader.defineClass(Unknown Source) at...
1
10111
by: rajujrk | last post by:
Hai All, I am Having a problem in the following... import org.apache.commons.net.ftp.FTPClient; import org.apache.commons.net.ftp.FTPFile; import org.apache.commons.io.FileUtils; import java.io.IOException;
1
3509
by: James Kalmadge | last post by:
I have a test program that compiles but does not run. Here is the code which I grabbed from from a web site: import java.awt.*; import java.awt.event.*; import java.io.*; import java.util.*; import java.sql.*; public class ColumnName{
0
9813
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10832
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10539
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10584
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9367
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7782
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5645
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4446
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3108
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.