简体   繁体   中英

ClassNotFoundException when loading class from JAR

I am trying to create a simple program that connects to Impala, performs a query and returns the result of the query. However, I got stuck at the very beginning: for some reason I am unable to load Impala JDBC Driver class from a JAR-file.

The JAR file I am trying to load is located in folder lib/ which is in my project's root folder. Otherwise my project follows normal Maven directory layout. I have added the path to the JAR file to my project's pom.xml. I have checked the MANIFEST.MF and the path is there. I have tried running my program with -classpath lib/ImpalaJDBC41.jar option and I also tried to put the JAR file to src/main/resources/ folder.

Here's part of my pom.xml where I set the Class-Path:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-shade-plugin</artifactId>
    <version>3.2.1</version>
    <executions>
        <!-- Run shade goal on package phase -->
        <execution>
            <phase>package</phase>
            <goals>
                <goal>shade</goal>
            </goals>
            <configuration>
                <transformers>
                    <!-- add Main-Class to manifest file -->
                    <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                        <manifestEntries>
                            <Main-Class>com.example.App</Main-Class>
                            <Class-Path>lib/ImpalaJDBC41.jar</Class-Path>
                        </manifestEntries>
                    </transformer>
                </transformers>
            </configuration>
        </execution>
    </executions>
</plugin>

Here's my MANIFEST.MF

Manifest-Version: 1.0
Archiver-Version: Plexus Archiver
Built-By: 400594
Class-Path: lib/ImpalaJDBC41.jar
Created-By: Apache Maven 3.5.3
Build-Jdk: 1.8.0_172
Main-Class: com.example.App

Here's my Java code:

package com.example;

import java.lang.Class;

public final class App {
    private App() {
    }

    /**
     * @param args The arguments of the program.
     */
    public static void main(String[] args) {  
        try {
            Class<?> cls = Class.forName("com.cloudera.impala.jdbc41.Driver");
            System.out.println("Class found: " + cls.getName());
        } catch (ClassNotFoundException ex) {
            System.out.println("Class not found: " + ex);
        }
    }
}

No matter what I try to do, I get the java.lang.ClassNotFoundException. I suspect that this problem is somehow related to classpath but I can't figure out what it is.

As far i have understand is that in your pom.xml configuration tag remove <Class-Path> tag and add class path as below. This would solve your problem. Here is the link for more clarification . Also "org.apache.maven.plugins.shade.resource.ManifestResourceTransformer" doesnot have classpath tag in it.

<configuration>
          <additionalClasspathElements>
            <additionalClasspathElement>path/to/additional/resources</additionalClasspathElement>

</configuration>

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