How to install JDBC driver in Eclipse web project without facing java.lang.ClassNotFoundexception

后端 未结 13 2824
梦毁少年i
梦毁少年i 2020-11-21 11:36

There is a VERY similar question to mine but in my case I don\'t have any duplicate jars in my build path, so the solution does not work for me. I\'ve searched google for a

相关标签:
13条回答
  • 2020-11-21 11:39

    As for every "3rd-party" library in flavor of a JAR file which is to be used by the webapp, just copy/drop the physical JAR file in webapp's /WEB-INF/lib. It will then be available in webapp's default classpath. Also, Eclipse is smart enough to notice that. No need to hassle with buildpath. However, make sure to remove all unnecessary references you added before, else it might collide.

    An alternative is to install it in the server itself by dropping the physical JAR file in server's own /lib folder. This is required when you're using server-provided JDBC connection pool data source which in turn needs the MySQL JDBC driver.

    See also:

    • How to add JAR libraries to WAR project without facing java.lang.ClassNotFoundException? Classpath vs Build Path vs /WEB-INF/lib
    • How should I connect to JDBC database / datasource in a servlet based application?
    • Where do I have to place the JDBC driver for Tomcat's connection pool?
    • JDBC CLASSPATH Not Working
    0 讨论(0)
  • 2020-11-21 11:41

    Many times I have been facing this problem, I have experienced ClassNotFoundException. if jar is not at physical location.

    So make sure .jar file(mysql connector) in the physical location of WEB-INF lib folder. and make sure restarting Tomcat by using shutdown command in cmd. it should work.

    0 讨论(0)
  • 2020-11-21 11:43

    What you should not do do (especially when working on a shared project)

    Ok, after had the same issue and after reading some answers here and other places. it seems that putting external lib into WEB-INF/lib is not that good idea as it pollute webapp/JRE libs with server-specific libraries - for more information check this answer"

    Another solution that i do NOT recommend is: to copy it into tomcat/lib folder. although this may work, it will be hard to manage dependency for a shared(git for example) project.

    Good solution 1

    Create vendor folder. put there all your external lib. then, map this folder as dependency to your project. in eclipse you need to

    1. add your folder to the build path
      1. Project Properties -> Java build path
      2. Libraries -> add external lib or any other solution to add your files/folder
    2. add your build path to deployment Assembly (reference)
      1. Project Properties -> Deployment Assembly
      2. Add -> Java Build Path Entries
      3. You should now see the list of libraries on your build path that you can specify for inclusion into your finished WAR.
      4. Select the ones you want and hit Finish.

    Good solution 2

    Use maven (or any alternative) to manage project dependency

    0 讨论(0)
  • 2020-11-21 11:44

    Place mysql-connector-java-5.1.6-bin.jar to the \Apache Tomcat 6.0.18\lib folder. Your problem will be solved.

    0 讨论(0)
  • 2020-11-21 11:44

    Just follow these steps:

    1) Install eclipse
    2) Import Apache to eclipse
    3) Install mysql
    4) Download mysqlconnector/J
    5) Unzip the zipped file navigate through it until you get the bin file in it. Then place all files that are present in the folder containing bin to C:\Program Files\mysql\mysql server5.1/ then give the same path as the address while defining the driver in eclipse.

    That's all very easy guys.

    0 讨论(0)
  • 2020-11-21 11:44

    My issue was a little different. Instead of jdbc:oracle:thin:@server:port/service i had it as server:port/service.

    Missing was jdbc:oracle:thin:@ in url attribute in GlobalNamingResources.Resource. But I overlooked tomcat exception's

    java.sql.SQLException: Cannot create JDBC driver of class 'oracle.jdbc.driver.OracleDriver' for connect URL 'server:port/service'
    
    0 讨论(0)
提交回复
热议问题