Results 1 to 19 of 19
- 11-10-2010, 11:58 AM #1
Member
- Join Date
- Aug 2010
- Posts
- 67
- Rep Power
- 0
Error -- java.lang.ClassNotFoundException
Hey there.
When I run my code, I get this exception:
Here is the code:Java Code:Error -- java.lang.ClassNotFoundException: org.apache.derby.jdbc.EmbeddedDriver
I do not understand why I am getting this error, as I have set the "org.apache.derby.jdbc.EmbeddedDriver" class in the eclipse class path:Java Code:import java.io.*; import java.sql.*; public class Presidents { String home, system; public Presidents() { // set the database's directory home = System.getProperty("user.home", "."); system = home + File.separatorChar + ".database"; System.setProperty("derby.system.home", system); } public void createDatabase() { // create the database String data = "jdbc:derby:presidents;create=true"; try { // load the driver Class.forName("org.apache.derby.jdbc.EmbeddedDriver"); // create the connection Connection conn = DriverManager.getConnection(data); Statement st = conn.createStatement(); // create the contacts table int result = st.executeUpdate( "CREATE TABLE contacts (" + "dex INTEGER NOT NULL PRIMARY KEY " + "GENERATED ALWAYS AS identity " + "(START WITH 1, INCREMENT BY 1), " + "name VARCHAR(40), " + "address1 VARCHAR(40), " + "address2 VARCHAR(40), " + "phone VARCHAR(20), " + "email VARCHAR(40))"); // insert four records into the new table result = st.executeUpdate( "INSERT INTO contacts (name, address1, address2, " + "phone, email) VALUES(" + "'Jimmy Carter', " + "'Carter Presidential Center', " + "'1 Copenhill, Atlanta, GA 30307', " + "'(404) 727-7611', " + "'carterweb@emory.edu')"); result = st.executeUpdate( "INSERT INTO contacts (name, address1, address2, " + "phone, email) VALUES(" + "'George Bush', " + "'Box 79798', " + "'Houston, TX 77279', " + "'(409) 260-9552', " + "'library@bush.nara.gov')"); result = st.executeUpdate( "INSERT INTO contacts (name, address1, address2, " + "phone, email) VALUES(" + "'Bill Clinton', " + "'15 Old House Lane', " + "'Chappaqua, NY 10514', " + "'(501) 370-8000', " + "'info@clintonpresidentialcenter.com')"); result = st.executeUpdate( "INSERT INTO contacts (name, address1, address2, " + "phone, email) VALUES(" + "'George W. Bush', " + "'White House, 1600 Pennsylvania Ave.', " + "'Washington, DC 20500', " + "'(202) 456-1414', " + "'president@whitehouse.gov')"); st.close(); System.out.println("Database created in " + system); } catch (Exception e) { System.out.println("Error -- " + e.toString()); } } public void readDatabase() { String data = "jdbc:derby:presidents"; try { // load the driver and connect to the database Class.forName("org.apache.derby.jdbc.EmbeddedDriver"); Connection conn = DriverManager.getConnection( data, "", ""); // load all records from the contacts table Statement st = conn.createStatement(); ResultSet rec = st.executeQuery( "SELECT * FROM contacts ORDER BY name"); // loop through each record and display its fields while(rec.next()) { System.out.println(rec.getString("name") + "\n" + rec.getString("address1") + "\n" + rec.getString("address2") + "\n" + rec.getString("phone") + "\n" + rec.getString("email") + "\n"); } st.close(); } catch (Exception e) { System.out.println("Error -- " + e.toString()); } } public static void main(String[] arguments) { Presidents prez = new Presidents(); String[] d = new String[]{"create"}; if (d[0].equals("create")) { prez.createDatabase(); } if (d[0].equals("read")) { prez.readDatabase(); } } }

Help please?
Thanks :)
- 11-10-2010, 12:13 PM #2
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
How do you run the code?
- 11-10-2010, 12:48 PM #3
Member
- Join Date
- Nov 2010
- Posts
- 3
- Rep Power
- 0
Hi,
As i'm very much new in eclipse.the error msg below is frustrating me all the time.
java.lang.NoClassDefFoundError: Prix
Caused by: java.lang.ClassNotFoundException: Prix
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
Exception in thread "main"
could u guys please help me out...
Thanks.
- 11-10-2010, 01:33 PM #4
Member
- Join Date
- Aug 2010
- Posts
- 67
- Rep Power
- 0
- 11-10-2010, 01:37 PM #5
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
Do you have a class called "Prix"?
Anyway, regarding that classpath, that's the build path isn't it? That's not the runtime path. I don't have Eclipse here, but I suspect your jar file needs to be added to the projects list of libraries, which isn't part of preferences (can't guarantee that bit).
- 11-10-2010, 01:51 PM #6
Member
- Join Date
- Nov 2010
- Posts
- 3
- Rep Power
- 0
To be precise guys This is the code and the error massage..
The code
class Rix {
double width;
double height;
double depth;
}
// This class declares an object of type Box.
class BoxDemo {
public static void main(String args[]) {
Rix mybox = new Rix();
double vol;
// assign values to mybox's instance variables
mybox.width = 10;
mybox.height = 20;
mybox.depth = 15;
// compute volume of box
vol = mybox.width * mybox.height * mybox.depth;
System.out.println("Volume is " + vol);
}
}
The error massage
java.lang.NoClassDefFoundError: Prix
Caused by: java.lang.ClassNotFoundException: Prix
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
Exception in thread "main"
and yes prix is a class as shown above
- 11-10-2010, 02:42 PM #7
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
Did you ever have a class Prix?
The JVM doesn't tend to get that sort of thing wrong.
Do a clean and build.
- 11-11-2010, 10:06 AM #8
Member
- Join Date
- Nov 2010
- Posts
- 3
- Rep Power
- 0
Thanks guys.....
- 03-02-2012, 12:30 PM #9
Member
- Join Date
- Feb 2012
- Posts
- 15
- Rep Power
- 0
Re: Error -- java.lang.ClassNotFoundException
I am getting a similar problem
I'm using netbeans and have the Apache derby software installed.
From within netbeans (6.8), the program runs fine, connects to the Database and everything.
I then clean and build and when I try executing the JAR file in the \dist directory, the Classnotfound exception is thrown up every time.
I've set the Classpath to the derby\lib\ directory - no joy.
I've included the derby.JAR file in the "c:\Program Files\Java\jre6\lib\" directory - no joy.
I've included the derby.jar file in the dist\lib\ directory - no joy.
I've tried including the derby.jar file in the dist folder next to my executable jar - no joy.
I've added the JAR file to the compile libraries in the project properties menu in netbeans - no joy.
How hard can it be?
Can anyone throw me a bone here?
- 03-02-2012, 12:38 PM #10
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
Re: Error -- java.lang.ClassNotFoundException
You shouldn't really resurrect ancient threads like this.
Where is the derby.jar located in your project?
Which class path?
You should never do this. Always a mistake, even if it gets something to work it is a hack.
I'm guessing that the derby.jar is not part of your project then. It needs to be included as a jar so that Netbeans knows to include it in the dist.
ditto
It need to be part of the runtime libraries.
I haven't got Netbeans here, but this is all down to your runtime libraries and getting Netbeans to recognise that derby is required.
SO first step is where is the derby.jar?Please do not ask for code as refusal often offends.
- 03-02-2012, 01:26 PM #11
Member
- Join Date
- Feb 2012
- Posts
- 15
- Rep Power
- 0
Re: Error -- java.lang.ClassNotFoundException
Sorry. Most forums have rules against duplicate threads, I thought this would be the cleanest way to request some help
It's not in my project. I'm trying to use the interdependencies and get the compiler to detect the JAR classes in the "c:\program files\sun\javadb\lib" folder
The ClassPath environment variable on my computer - it is currently set to "c:\program files\sun\javadb\lib\; %DERBY_HOME%\lib\derby.jar; c:\program files\sun\javadb\lib\derby.jar"
point taken.
I've included the line "import org.apache.derby.jdbc.ClientDriver;" in my code. Everything runs like a dream when I'm running the program in netbeans but it's when I try running the executable JAR built in the "\dist" folder it all goes pear shaped.
I've added it to the runtime libraries. I've done this via project -> properties, Libraries -> run, Add Library, Create... and creating a new library with the derby.jar file only.
I've also created a library with the entire folder "c:\program files\sun\javadb\lib" in it.
When I add this library to either "libraries -> run" or "libraries - compile", the "\lib" directory in my "dist\" folder dissappears and the JAR file won't run, at all!
removing them, it will run but when Calling the DB Connection throws the Classnotfound exception.
(Normally the \dist\lib\ folder contains the files appframework-1.0.3.jar and swing-worker-1.1.jar)
derby.jar is currelty residing in the folder "c:\program files\sun\javadb\lib\".
- 03-02-2012, 01:43 PM #12
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
Re: Error -- java.lang.ClassNotFoundException
It tends to result in old threads that often have irrelevant info in them clogging up the front pages. If there's something in an old thread you think is relevant then provide a link to that thread.
Nope. You shouldn't use a CLASSPATH environment variable at all. For starters, executable jar files don't use them, and neither will your IDE. So they're pointless.
You should have a lib folder in your project workspace.
That folder should contain all runtime dependencies (including the derby.jar).
Strip out any other includes/library paths/whatever that involve derby.
Ensure that this lib directory is on the build and runtime paths in Netbeans for the project (in the same way as the other two jars you are dependent on).
Derby should be treated no differently to any other dependency.Please do not ask for code as refusal often offends.
- 03-02-2012, 02:22 PM #13
Member
- Join Date
- Feb 2012
- Posts
- 15
- Rep Power
- 0
Re: Error -- java.lang.ClassNotFoundException
When I create the project in netbeans there is no "LIB" folder.
build\
dist\
src\
test\
build.xml
manifest.mf
I configure the dependencies via the libraries, in which the library for the Swing Application network is created.
I also add a new library called javaDB that points to the derby.jar file in the "c:\program files\sun\javadb\lib\" folder.
When I press "Clean and Build" in netbeans, a "\LIB" folder is crearted in the "\DIST" folder along with my executable JAR file.
The 3 .JAR files included from the dependencies/libraries are copied into this folder by netbeans on clean and build.
Without these JAR files, the program crashes out without a Main class.
It will run with them but the derby.jar files still throws a class not found exception.
If I remove the libraries and only include the .JAR files copied into A LIB\ directory copied into the project workspace, then remove all libraries other than a pointer to this new lib\ directory, the compiler and code validation fails.
- 03-02-2012, 02:28 PM #14
Member
- Join Date
- Feb 2012
- Posts
- 15
- Rep Power
- 0
Re: Error -- java.lang.ClassNotFoundException
only in the dist folder - put there by Netbeans on compile.
netbeans puts all the JAR files selected from the compile libraries in here.
Done. it only resides in the classpath. The only libraries point to the Jar files for the swing application framework library and the library containing derby.jar
How do I do this? Should the Libraries set this up for me? The other JARs are used only on the "Compile" library tab, the "run" library tab only contains "classpath for compiling sources" and "compiled sources"
It isn't but still seems to be throwing a wobbly.
- 03-02-2012, 02:50 PM #15
Member
- Join Date
- Feb 2012
- Posts
- 15
- Rep Power
- 0
Re: Error -- java.lang.ClassNotFoundException
Great.
I tried removing all other instances of "derby.jar" on my computer.
Now netbeans won't connect to any database or services... at all!
I've put them all back but I still have resolution issues and the servers won't run.
Stupid Java.
I don't know why I'm bothering!
- 03-02-2012, 02:56 PM #16
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
Re: Error -- java.lang.ClassNotFoundException
Because you are using an IDE without knowing how classpaths etc work in Java.
Hardly Java's fault.
I didn't say remove Derby from other places, I said (in your project) strip out derby from classpaths/build paths.
You need a lib directory.
Create one (Netbeans doesn't prevent you).
Inside that directory place all your dependent jars.
Add that directory to the project build and runtime path.
Exactly how that works for Netbeans I can't say as I haven't got it on this machine.Please do not ask for code as refusal often offends.
- 03-02-2012, 03:02 PM #17
Member
- Join Date
- Feb 2012
- Posts
- 15
- Rep Power
- 0
Re: Error -- java.lang.ClassNotFoundException
More frustration than anything.
I had read that having multiple instances can cause conflicts. I recorded where I was deleting them but it made no difference when I was putting it back together. When I create a LIB folder that contains the relevant .JARs and pointed the Run and Compile library tabs, this made no difference.
I was under the impression that the Classpath was the environment variable that was required in order for the compiler to find JAR or CLASS files for libraries.
It sounds like netbeans pays no heed to this environment variable, so why whe I point my libraries to the relevant JAR files, does it still not be able to find the class?
- 03-02-2012, 03:20 PM #18
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
Re: Error -- java.lang.ClassNotFoundException
No it's not.
Java only uses that classpath if no other has been provided, and an executable jar never uses it.
And IDE will use a classpath defined by the project.
So it's next to useless and simply gets people confused.
I don't know how you are "pointing your libraries".
In Netbeans I used the equivalent of "add jars" in Eclipse.Please do not ask for code as refusal often offends.
- 03-02-2012, 04:25 PM #19
Member
- Join Date
- Feb 2012
- Posts
- 15
- Rep Power
- 0
Re: Error -- java.lang.ClassNotFoundException
From what i can tell, adding the libraries in netbeans is similar. This is what I've done above.
I have on the other hand messed up my installation of Java attempting to fix my resolution issues :-(
I'll have to uninstall and start again, so I'll goive it a bash and see where I can pick it up.
Similar Threads
-
java.lang.ClassNotFoundException: com.sun.star.lang.XEventListener
By Leroy in forum Advanced JavaReplies: 8Last Post: 11-04-2010, 01:46 PM -
cannot find symbol method initCause(java.lang.ClassNotFoundException)
By Taxi in forum CLDC and MIDPReplies: 8Last Post: 10-17-2010, 10:24 AM -
java.lang.ClassNotFoundException
By ravian in forum New To JavaReplies: 1Last Post: 01-07-2008, 10:31 AM -
java.lang.ClassNotFoundException: javax.xml.parsers.SAXParserFactory
By simon in forum EclipseReplies: 1Last Post: 07-23-2007, 11:54 PM -
java.lang.ClassNotFoundException: sun.jdbc.odbc.JdbcOdbcDriver
By Marcus in forum JDBCReplies: 1Last Post: 06-27-2007, 05:57 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks