Jedis ClassNotFound 和 NoClassDefFoundError

Jedis ClassNotFound and NoClassDefFoundError

我为数据可视化工具开发了一个插件。现在我想在里面使用redis。当我在另一个项目(不在我的插件中)中尝试从 here 获取的以下 redis 代码时,它运行良好。

//Connecting to Redis server on localhost 
  Jedis jedis = new Jedis("localhost"); 
  System.out.println("Connection to server sucessfully"); 
  //check whether server is running or not 
  System.out.println("Server is running: "+jedis.ping()); 

但是当我在我的插件中使用 Jedis 时,我得到了 Caused by: java.lang.NoClassDefFoundError: redis/clients/jedis/JedisCaused by: java.lang.NoClassDefFoundError: redis/clients/jedis/Jedis 错误。为了将我的插件安装到这个数据可视化工具。我需要创建一个我做的 jar 文件,它在不添加 jedit 部件的情况下运行良好。

我正在使用 IntelliJ Idea,我创建了一个工件并从顶部菜单中的构建 - 构建工件选项卡中构建它。我还在 pom.xml 中添加了 jedis jar 文件作为依赖项(这是一个 Maven 项目),我将它添加为项目结构中的库,并且我添加了 jedis jar 文件作为提取目录和库以及工件选项卡项目结构菜单。然后我将 jedis jar 文件添加到我的项目 .classpath 文件中,如下所示:

    <classpathentry kind="src" path="src/main/resources/jedis-2.1.0-sources.jar" including="**/*.java"/>

所以当我打开我的 jar 文件时,我可以看到 "redis/clients/jedis" 路径中有 Jedis.java 文件。我的 jar 文件的根路径中也有 jedis jar 文件。但即使这样也行不通。它在 运行时 给出了上面的错误。我哪里做错了?

经过 loooong 的研究和 "experiments" 我解决了它。这是我添加 pom.xml 以摆脱它的部分。我还使用 "mvn package" 命令创建 jar 文件,而不是从 IntelliJ IDEA 界面创建。

<plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-eclipse-plugin</artifactId>
            <version>2.9</version>
            <configuration>
                <downloadSources>true</downloadSources>
                <downloadJavadocs>false</downloadJavadocs>
            </configuration>
        </plugin>

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>2.3.2</version>
            <configuration>
                <source>1.8.0_161</source>
                <target>1.8.0_161</target>
            </configuration>
        </plugin>

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-shade-plugin</artifactId>
            <version>2.3</version>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>shade</goal>
                    </goals>
                    <configuration>
                        <transformers>
                            <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                <mainClass>App.CytoVisProject</mainClass>
                            </transformer>
                        </transformers>
                    </configuration>
                </execution>
            </executions>
        </plugin>