简体   繁体   中英

Java EE ClassNotFoundException for MysqlDataSource. What am I doing wrong?

I want to connect to my MySQL database, and query some data. I am getting the following error:

2016-02-28 10:42:17,438] Artifact JavaEESecurity:war exploded: Error during artifact deployment. See server log for details. [2016-02-28 10:42:17,438] Artifact JavaEESecurity:war exploded: java.lang.Exception: {"WFLYCTL0080: Failed services" => {"jboss.deployment.unit.\"JavaEESecurity_war_exploded.war\".INSTALL"
=> "org.jboss.msc.service.StartException in service jboss.deployment.unit.\"JavaEESecurity_war_exploded.war\".INSTALL: WFLYSRV0153: Failed to process phase INSTALL of deployment \"JavaEESecurity_war_exploded.war\"
    Caused by: org.jboss.as.server.deployment.DeploymentUnitProcessingException: 
    java.lang.ClassNotFoundException: com.mysql.jdbc.jdbc2.optional.MysqlDataSource from 
    [Module \"deployment.JavaEESecurity_war_exploded.war:main\" from Service Module Loader]
    Caused by: java.lang.ClassNotFoundException: com.mysql.jdbc.jdbc2.optional.MysqlDataSource from [Module \"deployment.JavaEESecurity_war_exploded.war:main\" from Service Module Loader]"}}

I'm using maven in my project. This is my pom.xml file:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>my.own.group</groupId>
    <artifactId>myOwnArtifactId</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <dependency>
            <groupId>com.sun.faces</groupId>
            <artifactId>jsf-api</artifactId>
            <version>2.2.13</version>
        </dependency>
        <dependency>
            <groupId>javax</groupId>
            <artifactId>javaee-api</artifactId>
            <version>7.0</version>
        </dependency>
        <dependency>
            <groupId>jstl</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.38</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

This is my web.xml file:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">
    <data-source>
        <name>java:global/JavaEESecurity/myDS</name>
        <class-name>com.mysql.jdbc.jdbc2.optional.MysqlDataSource</class-name>
        <server-name>localhost</server-name>
        <database-name>mncpp</database-name>
        <user>root</user>
        <transactional>true</transactional>
        <initial-pool-size>2</initial-pool-size>
        <max-pool-size>10</max-pool-size>
        <min-pool-size>5</min-pool-size>
        <max-statements>0</max-statements>
    </data-source>

    <servlet>
        <servlet-name>Faces Servlet</servlet-name>
        <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>Faces Servlet</servlet-name>
        <url-pattern>*.xhtml</url-pattern>
    </servlet-mapping>
</web-app>

And this is my bean class, in which I'd like to access my database:

@Named("questionListBean")
@RequestScoped
public class QuestionListBean {
    List<String> questions;
    @Resource(lookup = "java:global/JavaEESecurity/myDS")
    DataSource ds;

    public List<String> getQuestions() {
        try (Connection connection = ds.getConnection()) {
            Statement statement = connection.createStatement();
            ResultSet rs = statement.executeQuery("SELECT question FROM questions");
            questions = new ArrayList<>();

            while(rs.next()) {
                questions.add(rs.getString(1));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return questions;
    }
}

Thanks in advance!

The ClassNotFoundException means that the Java runtime (the JVM) that runs your application server (Java EE server) is somehow not able to find the class com.mysql.jdbc.jdbc2.optional.MysqlDataSource . This class is a part of the MySQL JDBC Driver . Since this class is not available when your bean's method getQuestions() is called, the exception is thrown.

There are multiple ways in which you can fix this:

  1. Make your application self contained . This means that you bundle all the classes that your application needs along with it. This is the whole point of a Java .ear (enterprise application archive). Doing this (in theory) means that your application can be ported rather easily. To do this, you can assemble your app in such a way that you specify a runtime dependency on MySQL connector (see mvnrepository for the snippet and add <scope>runtime</scope> ) in a <dependency> element in your pom.xml. Then you can use the Maven ear plugin if you are assembling a .ear or Maven war plugin if you are assembling a .war to bundle everything up.
  2. Make the MySQL driver jar a part of your application server's classpath. That way, other applications can leverage those classes (you may have more than one app using MySQL, for instance). Application servers have this facility so that these classes are made available to apps. Read up your app server's documentation to see how to do it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM