Results 1 to 19 of 19
  1. #1
    dewitrydan is offline Member
    Join Date
    Aug 2010
    Posts
    67
    Rep Power
    0

    Default Error -- java.lang.ClassNotFoundException

    Hey there.
    When I run my code, I get this exception:
    Java Code:
    Error -- java.lang.ClassNotFoundException: org.apache.derby.jdbc.EmbeddedDriver
    Here is the code:
    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();
            }
        }    
    }
    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:


    Help please?
    Thanks :)

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,439
    Rep Power
    18

    Default

    How do you run the code?

  3. #3
    Princegame is offline Member
    Join Date
    Nov 2010
    Posts
    3
    Rep Power
    0

    Default

    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.

  4. #4
    dewitrydan is offline Member
    Join Date
    Aug 2010
    Posts
    67
    Rep Power
    0

    Default

    Quote Originally Posted by Tolls View Post
    How do you run the code?
    In eclipse, I right clicked "Presidants.java"
    clicked "Run As"
    then clicked "Java Application"

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,439
    Rep Power
    18

    Default

    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).

  6. #6
    Princegame is offline Member
    Join Date
    Nov 2010
    Posts
    3
    Rep Power
    0

    Default

    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

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,439
    Rep Power
    18

    Default

    Did you ever have a class Prix?
    The JVM doesn't tend to get that sort of thing wrong.
    Do a clean and build.

  8. #8
    Princegame is offline Member
    Join Date
    Nov 2010
    Posts
    3
    Rep Power
    0

    Default

    Thanks guys.....

  9. #9
    deadlyBuzz is offline Member
    Join Date
    Feb 2012
    Posts
    15
    Rep Power
    0

    Default 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?

  10. #10
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,439
    Rep Power
    18

    Default Re: Error -- java.lang.ClassNotFoundException

    You shouldn't really resurrect ancient threads like this.

    Quote Originally Posted by deadlyBuzz View Post
    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.
    Where is the derby.jar located in your project?

    Quote Originally Posted by deadlyBuzz View Post
    I've set the Classpath to the derby\lib\ directory - no joy.
    Which class path?

    Quote Originally Posted by deadlyBuzz View Post
    I've included the derby.JAR file in the "c:\Program Files\Java\jre6\lib\" directory - no joy.
    You should never do this. Always a mistake, even if it gets something to work it is a hack.

    Quote Originally Posted by deadlyBuzz View Post
    I've included the derby.jar file in the dist\lib\ directory - no joy.
    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.
    Quote Originally Posted by deadlyBuzz View Post
    I've tried including the derby.jar file in the dist folder next to my executable jar - no joy.
    ditto
    Quote Originally Posted by deadlyBuzz View Post
    I've added the JAR file to the compile libraries in the project properties menu in netbeans - no joy.
    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.

  11. #11
    deadlyBuzz is offline Member
    Join Date
    Feb 2012
    Posts
    15
    Rep Power
    0

    Default Re: Error -- java.lang.ClassNotFoundException

    Quote Originally Posted by Tolls View Post
    You shouldn't really resurrect ancient threads like this.
    Sorry. Most forums have rules against duplicate threads, I thought this would be the cleanest way to request some help

    Quote Originally Posted by Tolls View Post
    Where is the derby.jar located in your project?
    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

    Quote Originally Posted by Tolls View Post
    Which class path?
    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"


    Quote Originally Posted by Tolls View Post
    You should never do this. Always a mistake, even if it gets something to work it is a hack.
    point taken.

    Quote Originally Posted by Tolls View Post
    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.
    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.

    Quote Originally Posted by Tolls View Post
    ditto
    Quote Originally Posted by Tolls View Post
    It need to be part of the runtime libraries.
    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)

    Quote Originally Posted by Tolls View Post
    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?
    derby.jar is currelty residing in the folder "c:\program files\sun\javadb\lib\".

  12. #12
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,439
    Rep Power
    18

    Default Re: Error -- java.lang.ClassNotFoundException

    Quote Originally Posted by deadlyBuzz View Post
    Sorry. Most forums have rules against duplicate threads, I thought this would be the cleanest way to request some help
    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.

    Quote Originally Posted by deadlyBuzz View Post
    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"
    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.
    Quote Originally Posted by deadlyBuzz View Post
    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\".
    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.

  13. #13
    deadlyBuzz is offline Member
    Join Date
    Feb 2012
    Posts
    15
    Rep Power
    0

    Default 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.

  14. #14
    deadlyBuzz is offline Member
    Join Date
    Feb 2012
    Posts
    15
    Rep Power
    0

    Default Re: Error -- java.lang.ClassNotFoundException

    Quote Originally Posted by Tolls View Post
    You should have a lib folder in your project workspace.
    only in the dist folder - put there by Netbeans on compile.

    Quote Originally Posted by Tolls View Post
    That folder should contain all runtime dependencies (including the derby.jar).
    netbeans puts all the JAR files selected from the compile libraries in here.

    Quote Originally Posted by Tolls View Post
    Strip out any other includes/library paths/whatever that involve derby.
    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

    Quote Originally Posted by Tolls View Post
    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).
    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"

    Quote Originally Posted by Tolls View Post
    Derby should be treated no differently to any other dependency.
    It isn't but still seems to be throwing a wobbly.

  15. #15
    deadlyBuzz is offline Member
    Join Date
    Feb 2012
    Posts
    15
    Rep Power
    0

    Default 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!

  16. #16
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,439
    Rep Power
    18

    Default 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.

  17. #17
    deadlyBuzz is offline Member
    Join Date
    Feb 2012
    Posts
    15
    Rep Power
    0

    Default 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?

  18. #18
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,439
    Rep Power
    18

    Default 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.

  19. #19
    deadlyBuzz is offline Member
    Join Date
    Feb 2012
    Posts
    15
    Rep Power
    0

    Default 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

  1. Replies: 8
    Last Post: 11-04-2010, 01:46 PM
  2. Replies: 8
    Last Post: 10-17-2010, 10:24 AM
  3. java.lang.ClassNotFoundException
    By ravian in forum New To Java
    Replies: 1
    Last Post: 01-07-2008, 10:31 AM
  4. Replies: 1
    Last Post: 07-23-2007, 11:54 PM
  5. Replies: 1
    Last Post: 06-27-2007, 05:57 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •