Jedis ClassNotFound 和 NoClassDefFoundError

Jol*_*ger 5 java maven redis cytoscape.js

我开发了一个数据可视化工具插件。现在我想在里面使用redis。当我在另一个项目(不在我的插件内)中尝试下面的 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()); 
Run Code Online (Sandbox Code Playgroud)

但是当我在插件中使用 Jedis 时,我遇到了Caused by: java.lang.NoClassDefFoundError: redis/clients/jedis/Jedis错误Caused 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"/>
Run Code Online (Sandbox Code Playgroud)

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

Jol*_*ger 2

经过长时间的研究和“实验”,我解决了它。这是我添加 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>
Run Code Online (Sandbox Code Playgroud)