1

I am currently developing a restful api using jax-rs. Project is a maven project. I need to add mysql-connector-bin.jar to my project. This is the pom.xml file that was generated and I added jar file as follows.

<modelVersion>4.0.0</modelVersion>

    <groupId>com.projects.api</groupId>
    <artifactId>messanger</artifactId>
    <packaging>war</packaging>
    <version>0.0.1-SNAPSHOT</version>
    <name>messanger</name>

    <build>
        <finalName>messanger</finalName>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.5.1</version>
                <inherited>true</inherited>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.glassfish.jersey</groupId>
                <artifactId>jersey-bom</artifactId>
                <version>${jersey.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <dependency>
                <groupId>mysql-connector</groupId>
                <artifactId>mysql-connector-java-5.1.38-bin</artifactId>
                <version>5.1.38</version>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <dependencies>
        <dependency>
            <groupId>org.glassfish.jersey.containers</groupId>
            <artifactId>jersey-container-servlet-core</artifactId>
            <!-- use the following artifactId if you don't need servlet 2.x compatibility -->
            <!-- artifactId>jersey-container-servlet</artifactId -->
        </dependency>
        <!-- uncomment this to get JSON support
        <dependency>
            <groupId>org.glassfish.jersey.media</groupId>
            <artifactId>jersey-media-moxy</artifactId>
        </dependency>
        -->
    </dependencies>
    <properties>
        <jersey.version>2.22.2</jersey.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
</project>

When I run he project It shows class not found for the jar file I added. Do you have any idea on this?

Jim Garrison
  • 85,615
  • 20
  • 155
  • 190

2 Answers2

4

replace

<dependency>
    <groupId>mysql-connector</groupId>
    <artifactId>mysql-connector-java-5.1.38-bin</artifactId>
    <version>5.1.38</version>
</dependency>

with

<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>5.1.38</version>
</dependency>

Also always search for meaven dependcies in http://mvnrepository.com to know how to add anything else in the future

Ahmed H. Saab
  • 395
  • 3
  • 14
  • Shouldn't it also be outside the `` section, in the top-level `` section? – Jim Garrison Mar 14 '16 at 03:16
  • is used to inherit versions to child projects, it will work both ways. You can understand it more in this question http://stackoverflow.com/questions/2619598/differences-between-dependencymanagement-and-dependencies-in-maven – Ahmed H. Saab Mar 14 '16 at 03:32
  • Even though I edited as shown above, I still get this error java.lang.ClassNotFoundException: com.mysql.jdbc.Driver – Tharindu Ranasinghe Mar 14 '16 at 08:42
  • Did you download the dependencies by running "mvn clean install" in the terminal or cmd ? – Ahmed H. Saab Mar 14 '16 at 15:57
0
<java.version> 11</java.version>
    <maven.compiler.target> ${java.version}</maven.compiler.target>
     <maven.compiler.Source> ${java.version}</maven.compiler.Source>

<dependencies>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>8.0.20</version>
        <exclusions>
            <exclusion>
                <groupId>com.google.protobuf</groupId>
                <artifactId>protobuf-java</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
</dependencies>
</properties>

you need to go to project setting and change the java.version to 11 or the latest one. Error "Source option 5 is no longer supported. Use 6 or later" on Maven compile