1

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>

    <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 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <version>2.5.1</version>
                <executions>
                    <execution>
                        <id>copy-dependencies</id>
                        <phase>package</phase>
                        <goals>
                            <goal>copy-dependencies</goal>
                        </goals>
                        <configuration>
                            <!-- exclude junit, we need runtime dependency only -->
                            <includeScope>runtime</includeScope>
                            <outputDirectory>${project.build.directory}/dependency-jars/</outputDirectory>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

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

Error: Could not find or load main class com.es.utility.DocumentIndex

Please find my project structure also :

enter image description here

7
  • Have you examined your .jar file to see if your main class is both in the .jar file, and in the right path? In case you haven't done this before, you can change .jar to .zip to help examine the contents. Commented Jul 8, 2018 at 14:06
  • @kshetline - i have only META-INF folder after i extract my .jar file Commented Jul 8, 2018 at 14:31
  • I don't know how this particular build tool you're using works, but you're probably missing a step in the <archive> section that puts all of your class files into the archive. Commented Jul 8, 2018 at 14:35
  • What's the folder structure of your project? Commented Jul 8, 2018 at 14:50
  • Off-topic comment: remove the maven-eclipse-pluigin. It is no longer maintained and does not play well with modern versions of Eclipse and Maven. See Apache Maven Eclipse Plugin (RETIRED) Commented Jul 8, 2018 at 15:02

2 Answers 2

2

If there are no classes in the .jar it's probably because maven can't find them. The default path for java classes in Maven is src/main/java, try moving them there and run Maven again.

Sign up to request clarification or add additional context in comments.

1 Comment

Its solved my issue, but now am getting different issue Error: A JNI error has occurred, please check your installation and try again Exception in thread "main" java.lang.NoClassDefFoundError: org/elasticsearch/Ela sticsearchException i have created a separate question for the same. stackoverflow.com/questions/51240395/…
0

To add to @kshetline's comment, make sure com.es.utility.DocumentIndex is the correct path to your main class. When you unzip the generated jar, DocumentIndex.class should be in the com/es/utility folder.

2 Comments

i have only META-INF folder after i extract my .jar file
It sounds like your Maven build is 1) not building your code, 2) not putting the built artifacts where the packaging step expects, or 3) not calling the packaging step.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.