mr.incredible
mr.incredible

Reputation: 4185

Java Maven Could not find or load main class

I've just started to learn Java but have issues almost at every step... At least I want to make this super simple getting started tutorial work (from official Maven page) :)

I've done every step like in this tutorial and have built the project but can't run it.

So, after mvn package I have BUILD SUCCESSFUL but when I'am trying to launch jar file by this command java -cp target/my-app-1.0-SNAPSHOT.jar com.mycompany.app.App an error occurs:

Error: Could not find or load main class com.mycompany.app.App
Caused by: java.lang.ClassNotFoundException: com.mycompany.app.App

How to fix it?

java version "11.0.1" 2018-10-16 LTS

Apache Maven 3.6.0

mvn archetype:generate -DgroupId=com.mycompany.app -DartifactId=my-app -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

Project structure is created automatically by this command as shown in the docs

my-app
 |-src
 |  |-main
 |     |-java
 |        |-com
 |           |-mycompany
 |              |-app
 |                 |-App.java
 |-pom.xml
 |-target

And the App.java code:

 package com.mycompany.app;

 /**
  * Hello world!
  *
 */
 public class App 
 {
     public static void main( String[] args )
     {
        System.out.println( "Hello World!" );
    }
}

And 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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.mycompany.app</groupId>
  <artifactId>maven-test</artifactId>
  <packaging>jar</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>maven-test</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
  <properties>
    <maven.compiler.source>1.6</maven.compiler.source>
    <maven.compiler.target>1.6</maven.compiler.target>
  </properties>
</project>

Also I had to add these lines to make work mvn package command:

<properties>
   <maven.compiler.source>1.6</maven.compiler.source>
   <maven.compiler.target>1.6</maven.compiler.target>
</properties>

Upvotes: 1

Views: 2135

Answers (2)

Chirlo
Chirlo

Reputation: 6132

Based on your the output of the jar command, (the NoSuchFileException from the comments), it seems you were running the java command from some directory other than that of the maven project. @Sgrillon's answer works because he changes directory to that of my-app, runs the mvn package command and then the java command from the same directory. Then the target/my-app-1.0-SNAPSHOT.jar is there (generated by maven) and can be found by the java command to include it in its classpath (-cp) This is not a java or maven problem but an issue with the file's path.

EDIT: I just realized on your pom.xml that your artifact was named maven-test

  <artifactId>maven-test</artifactId>

In this case, the jar would have been target/maven-test-1.0-SNAPSHOT.jar instead of my-app... and that's why neither java nor jar could find it.

Upvotes: 1

St&#233;phane GRILLON
St&#233;phane GRILLON

Reputation: 11864

You can re-try with this 4 commnds:

> mvn archetype:generate -DgroupId=com.mycompany.app -DartifactId=my-app -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
> cd my-app
> mvn package
> java -cp target/my-app-1.0-SNAPSHOT.jar com.mycompany.app.App

enter image description here

Upvotes: 1

Related Questions