简体   繁体   English

从JAR加载类时发生ClassNotFoundException

[英]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. 我正在尝试创建一个连接到Impala的简单程序,执行查询并返回查询结果。 However, I got stuck at the very beginning: for some reason I am unable to load Impala JDBC Driver class from a JAR-file. 但是,我一开始就陷入了困境:由于某种原因,我无法从JAR文件加载Impala JDBC Driver类。

The JAR file I am trying to load is located in folder lib/ which is in my project's root folder. 我要加载的JAR文件位于项目的根文件夹中的lib /文件夹中。 Otherwise my project follows normal Maven directory layout. 否则,我的项目将遵循常规的Maven目录布局。 I have added the path to the JAR file to my project's pom.xml. 我已将JAR文件的路径添加到项目的pom.xml中。 I have checked the MANIFEST.MF and the path is there. 我已经检查了MANIFEST.MF,并且路径在那。 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. 我尝试使用-classpath lib / ImpalaJDBC41.jar选项运行程序,并且还尝试将JAR文件放入src / main / resources /文件夹。

Here's part of my pom.xml where I set the Class-Path: 这是我的pom.xml的一部分,在其中设置了类路径:

<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.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: 这是我的Java代码:

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. 无论我尝试做什么,都会得到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. 据我所知,在您的pom.xml配置标记中,删除<Class-Path>标记并添加如下所示的类路径。 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. 另外, “ org.apache.maven.plugins.shade.resource.ManifestResourceTransformer”中没有类路径标签。

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

</configuration>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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