2

I want to execute the class file (or jar file) located in the same folder with pom.xml, which contains main method.

Here is my pom.xml

http://maven.apache.org/maven-v4_0_0.xsd"> 4.0.0 tuomas sleep pom 1.0-SNAPSHOT

<build>
    <plugins>
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>exec-maven-plugin</artifactId>
        <version>1.5.0</version>
        <executions>
          <execution>
            <id>sleep</id>
            <phase>verify</phase>
            <goals>
              <goal>java</goal>
            </goals>
          </execution>
        </executions>
        <configuration>
          <mainClass>tuomas.App</mainClass>
          <classpathScope>tuomas</classpathScope>
        </configuration>
      </plugin>
    </plugins>
  </build>

And I get the following error:

[ERROR] Failed to execute goal org.codehaus.mojo:exec-maven-plugin:1.5.0:java (sleep) on project sleep: Execution sleep of goal org.codehaus.mojo:exec-maven-plugin:1.5.0:java failed: Invalid classpath scope: tuomas -> [Help 1]

Here is my folder structure

pom.xml . ..tuomas..App.class

I have also tried not to specify classpathScope attribute in plugin configuration, and got the following error instead.

[WARNING]
java.lang.ClassNotFoundException: tuomas.App
        at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
        at org.codehaus.mojo.exec.ExecJavaMojo$1.run(ExecJavaMojo.java:281)
        at java.lang.Thread.run(Thread.java:745)

Regardless of if the class file is under tuomas folder or in the project root. Also tried install the jar to local maven repository with no success.

How to configure this plugin to find the class from the project folder?

Tuomas Toivonen
  • 21,690
  • 47
  • 129
  • 225

1 Answers1

2

First, get rid of the <classpathScope> parameter. There is no scope called toumas in Maven, only these. The default is runtime which should work for most people.

Provided your main method is located in the package toumas and in the file App.java, it should work.

Your statement:

"I have also tried not to specify classpathScope attribute in plugin configuration, and got the following error instead."

makes me wonder if your Java file really is under src/main/java?

Daniel
  • 4,033
  • 4
  • 24
  • 33
  • `makes me wonder if your Java file really is under src/main/java?` exactly what happened to me when I created a new Java project in Intellij and later added pom.xml to it. The directory structures were not the same. – maksimov Mar 02 '17 at 15:48
  • As a side note, your answer points to the wrong scope. This is the dependency scope while the correct reference is for the [classpathScope](https://www.mojohaus.org/exec-maven-plugin/exec-mojo.html#classpathScope). – Stelios Adamantidis Nov 27 '19 at 12:57