java.lang.ClassNotFoundException: (Driver Class) While Datasource got using JNDI

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dmjpro
    Top Contributor
    • Jan 2007
    • 2476

    java.lang.ClassNotFoundException: (Driver Class) While Datasource got using JNDI

    My context.xml...
    Code:
    <Context path="/JNDITest">
        <Resource name="jdbc/dataSource" auth="Container"
                type="javax.sql.DataSource"
    
                driverClassName="org.postgresql.Driver"
                url="jdbc:postgresql://10.29.33.90:5432/Admin"
                username="erp"
                password="iiterp"
    
                maxIdle="50"
                maxWait="60000"
                maxActive="10"/>
    </Context>
    My Java Code ....
    Code:
    Context ctx = new InitialContext();
                DataSource ds= (DataSource)ctx.lookup("java:/comp/env/jdbc/dataSource");
                Connection l_con = ds.getConnection();
    It's working fine when I use Windows, but it's not working when I use Linux.
    My Application server is Tomcat.
    I mean to say why does it behave in different operating System .. Where I gone wrong ?
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Is your postgreSQL jar in your classpath on your linux machine?

    kind regards,

    Jos

    Comment

    • dmjpro
      Top Contributor
      • Jan 2007
      • 2476

      #3
      If the Jar is not in the classpath then why it runs properly in Windows ?

      The jars are kept into the the folder WEB-INF/lib and i am making the war file and deploy it into the server...In Windows the war file is running OK ..but why not in Linux?

      Comment

      • dmjpro
        Top Contributor
        • Jan 2007
        • 2476

        #4
        This is my another test ............... in Linux Tomcat Server.
        My context.xml is ...
        Code:
        <Resource name="jdbc/EmployeeDB" auth="Container"
                    type="javax.sql.DataSource"
             description="Employees Database for HR Applications"
        driverClassName="org.postgresql.Driver"
        url="jdbc:postgresql://10.29.32.68:5432/Admin"
        username="erp"
        password="iiterp"/>
        In my home.jsp the code snippet is ....
        Code:
        DriverManager.registerDriver(new org.postgresql.Driver());
                    Connection l_con = DriverManager.getConnection("jdbc:postgresql://10.29.32.68:5432/Admin","erp","iiterp");
                    
                    Context ctx = new InitialContext();
                    DataSource ds= (DataSource)ctx.lookup("java:/comp/env/jdbc/EmployeeDB");
                    Connection l_con1 = ds.getConnection();

        When it runs on Linux Tomcat server it shows an error in line ...
        Code:
        Connection l_con1 = ds.getConnection();
        If the jar is not in class path then why it could execute successfully the line ...
        Code:
        DriverManager.registerDriver(new org.postgresql.Driver());
                    Connection l_con = DriverManager.getConnection("jdbc:postgresql://10.29.32.68:5432/Admin","erp","iiterp");
        Please Help!

        Comment

        • dmjpro
          Top Contributor
          • Jan 2007
          • 2476

          #5
          Now I could figure out the problem ...
          Actually i didn't deploy into the Tomcat server running under Windows. ;)
          Actually what happened, i was using Netbeans simply ran the Application and it got into work .. I don't understand how it did run.
          But when i deployed the war file into the Window Tomcat Server then the same problem i was having.
          Why does it happen .. when i load the Driver class using DriverManager then it looks from the WEB-INF/ib but when i try to get connection using JNDI look then it looks for another directory?

          Comment

          • dmjpro
            Top Contributor
            • Jan 2007
            • 2476

            #6
            Yeah now it's working i put the driver class into the ext directory.
            But still i am waiting for the answer ... why does it behave like differently in two cases ?

            Comment

            • JosAH
              Recognized Expert MVP
              • Mar 2007
              • 11453

              #7
              Originally posted by dmjpro
              Yeah now it's working i put the driver class into the ext directory.
              But still i am waiting for the answer ... why does it behave like differently in two cases ?
              You gave the answer yourself: you didn't deploy on your Windows machine but made NetBeans do all the work for you. Make sure the 'environments' are identical on both computers. btw, don't just put your jars in the /ext directory to make things 'work'; most likely those jars don't belong there. Read this.

              kind regards,

              Jos

              Comment

              • dmjpro
                Top Contributor
                • Jan 2007
                • 2476

                #8
                What Environments u telling about ?
                And i meant to say why it looks from different locations .. ?

                Comment

                • JosAH
                  Recognized Expert MVP
                  • Mar 2007
                  • 11453

                  #9
                  Originally posted by dmjpro
                  What Environments u telling about ?
                  And i meant to say why it looks from different locations .. ?
                  By equal 'environments' (mind the quotes) I meant: equal Java JRE versions, equal classpaths, equal available jars etc. I don't know what you mean by 'why it looks from different locations"; please elaborate.

                  kind regards,

                  Jos

                  Comment

                  • dmjpro
                    Top Contributor
                    • Jan 2007
                    • 2476

                    #10
                    When i try to connect using ...
                    Code:
                    DriverManager.registerDriver(new DriverClass());
                    Connection l_con = DriverManager.getConnection(......);
                    Then it looks from WEB-INF/lib but i use JNDI look up then it looks from class path .. I did mean that ... ;)

                    Comment

                    • r035198x
                      MVP
                      • Sep 2006
                      • 13225

                      #11
                      On tomcat the WEB-INF/lib folder is on the path by default.

                      Comment

                      Working...