0

I have problem after build project on maven in jar.

I make package and create .jar file:

mvn package

after i run my jar file

java -jar artifactId.jar

and get error:

Error: Could not find or load main class com.emercit.app.App

If i run class in target directory, i get similar error:

cd target/classes/com/emercit/app
java App
Error: Could not find or load main class App

My code in App class:

package com.emercit.app;

public class App extends Application {

   /**

     Program code

  **/

    }

public static void main (String[] args) {


    //Init form
   Thread myThready = new Thread(() -> {
        launch(args);
    });
    myThready.start();

    }
 }

My build properties in pom.xml:

 <build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
            </configuration>
        </plugin>


        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <configuration>
                <archive>
                    <manifest>
                        <addClasspath>false</addClasspath>
                        <classpathPrefix>lib/</classpathPrefix>
                        <mainClass>com.emercit.app.App</mainClass>
                    </manifest>
                </archive>
            </configuration>
        </plugin>

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-dependency-plugin</artifactId>
            <executions>
                <execution>
                    <id>copy</id>
                    <phase>install</phase>
                    <goals>
                        <goal>copy-dependencies</goal>
                    </goals>
                    <configuration>
                        <outputDirectory>
                            ${project.build.directory}/lib
                        </outputDirectory>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>
Wolfgang
  • 109
  • 1
  • 10
  • could you share github link? – Malakai Mar 09 '17 at 06:56
  • Isn't your main() outside of the class? – Mehraj Malik Mar 09 '17 at 06:57
  • https://github.com/sterkhov/BuildError – Wolfgang Mar 09 '17 at 07:00
  • App contain main(), and in IDEA project it work – Wolfgang Mar 09 '17 at 07:02
  • You shouldn't run `App` in `target/classes/com/emercit/app` from console, you run `com.emercit.app.App` from `target/classes`. – Jiri Tousek Mar 09 '17 at 07:05
  • `Error: Could not find or load main class App` means that the class App were not found - are you sure that maven even generates the classfiles for you? Have you placed the App-class under the correct project structure? Meaning `/src/main/com/../../App`? If not, you'll might have to add a ``-configuration. – vegaasen Mar 09 '17 at 07:06
  • No matter. I get Error: Could not find or load main class com.emercit.app.App – Wolfgang Mar 09 '17 at 07:07
  • @Sterkhov dont forget the fact that this still is java cmdline application. With that being said, you lack buildmanifest.mf file – Malakai Mar 09 '17 at 07:07
  • Possible duplicate of [What does "Could not find or load main class" mean?](http://stackoverflow.com/questions/18093928/what-does-could-not-find-or-load-main-class-mean) – Seelenvirtuose Mar 09 '17 at 07:11
  • @Reborn How i can add this property? – Wolfgang Mar 09 '17 at 07:13
  • @Sterkhov you need to make new file called as Manifest.mf and specify there your path to main class. Since, it is maven project 1) make manifest.mf file with args 2)mvn package 3) make jar as mentioned here wikihow.com/Create-JAR-File (step 3 already includes manifest creation) This is not 100% working – Malakai Mar 09 '17 at 07:34
  • `false` should be set to true otherwise your .jar won't be able to find the dependencies copied by the maven-dependency-plugin. – Luciano van der Veekens Mar 09 '17 at 08:08

3 Answers3

0

Build your jar with this plugin and specify the main class, this will add the correct META-INF in your generated jar.

 <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>3.0.0</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                        <configuration>
                            <transformers>
                                <transformer
                                        implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                    <manifestEntries>
                                        <Main-Class>com.barclaycard.blackboxtester.Application</Main-Class>
                                        <Build-Number>123</Build-Number>
                                    </manifestEntries>
                                </transformer>
                            </transformers>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
Essex Boy
  • 7,565
  • 2
  • 21
  • 24
0

Your setup is similar to how I do it except you need to change this parameter in your POM file to be a value of "true".

<addClasspath>true</addClasspath>

Without it you would have to include dependencies on the command line and you are not. Java won't find jars just because they are in a directory. This command will add a "classpath" in the MANIFEST.MF file withing the JAR.

The generated MANIFEST.MF file would contain a line something like this (taken from one of my opensource projects). Your jar dependencies would of course be different.

Class-Path: calendar-2.2.3.jar algebrain-2.2.3.jar commons-codec-1.9.j
 ar argument-4.3.8.jar slf4j-api-1.7.13.jar slf4j-log4j12-1.7.13.jar l
 og4j-1.2.17.jar
fedup
  • 1,209
  • 11
  • 26
0

The file structure while creating any mavenized project should src/main/java and package should be include in class file other wise it will throw error Could not find or load main class

Amit
  • 7
  • 5