A JNI error has occurred, while executing .jar file from command prompt

匿名 (未验证) 提交于 2019-12-03 01:36:02

问题:

I have created two java file my maven project one is POJO class and another one is java main class file. I want to make my project as an executable jar file which i want to run externally using java -jar command.

Please find my pom.xml file

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>ElasticSearchUtility</groupId> <artifactId>ElasticSearchUtility</artifactId> <version>1.0.0-SNAPSHOT</version> <packaging>jar</packaging> <dependencies>     <dependency>         <groupId>org.elasticsearch</groupId>         <artifactId>elasticsearch</artifactId>         <version>6.1.2</version>     </dependency>     <dependency>         <groupId>org.elasticsearch.client</groupId>         <artifactId>elasticsearch-rest-high-level-client</artifactId>         <version>6.1.2</version>     </dependency>     <dependency>         <groupId>org.elasticsearch.client</groupId>         <artifactId>rest</artifactId>         <version>5.1.2</version>     </dependency>  </dependencies>  <build>          <plugins>             <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>              <!-- Set a compiler level -->             <plugin>                 <groupId>org.apache.maven.plugins</groupId>                 <artifactId>maven-compiler-plugin</artifactId>                 <version>2.3.2</version>                 <configuration>                     <source>1.8</source>                     <target>1.8</target>                 </configuration>             </plugin>              <!-- Make this jar executable -->             <plugin>                 <groupId>org.apache.maven.plugins</groupId>                 <artifactId>maven-jar-plugin</artifactId>                 <configuration>                     <excludes>                         <exclude>**/log4j.properties</exclude>                     </excludes>                     <archive>                         <manifest>                             <addClasspath>true</addClasspath>                             <mainClass>com.es.utility.DocumentIndex</mainClass>                             <classpathPrefix>dependency-jars/</classpathPrefix>                         </manifest>                     </archive>                 </configuration>             </plugin>             <!-- Copy project dependency -->              </plugins>     </build> 

While am executing my jar file using java -jar command. Am getting the error

Error: A JNI error has occurred, please check your installation and try again Exception in thread "main" java.lang.NoClassDefFoundError: org/elasticsearch/Ela sticsearchException         at java.lang.Class.getDeclaredMethods0(Native Method)         at java.lang.Class.privateGetDeclaredMethods(Unknown Source)         at java.lang.Class.privateGetMethodRecursive(Unknown Source)         at java.lang.Class.getMethod0(Unknown Source)         at java.lang.Class.getMethod(Unknown Source)         at sun.launcher.LauncherHelper.validateMainClass(Unknown Source)         at sun.launcher.LauncherHelper.checkAndLoadMain(Unknown Source) Caused by: java.lang.ClassNotFoundException: org.elasticsearch.ElasticsearchExce ption         at java.net.URLClassLoader.findClass(Unknown Source)         at java.lang.ClassLoader.loadClass(Unknown Source)         at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)         at java.lang.ClassLoader.loadClass(Unknown Source)         ... 7 more 

Please find my project structure :

回答1:

Create jar-with-dependencies.

<build>    <plugins>       <plugin>          <artifactId>maven-assembly-plugin</artifactId>          <configuration>             <archive>                <manifest>                   <mainClass>fully.qualified.MainClass</mainClass>                </manifest>             </archive>             <descriptorRefs>                <descriptorRef>jar-with-dependencies</descriptorRef>             </descriptorRefs>          </configuration>          <executions>             <execution>                <id>make-jar</id>                <phase>package</phase>                <goals>                   <goal>single</goal>                </goals>             </execution>          </executions>       </plugin>    </plugins> </build> 

This will create FatJar or UberJar that will contain all dependencies within.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!