0

I want to set main class in pom.xml to run exec:java I checked lots of resources but still have the same error.

I tried putting it in under execution tag.

<build>
    <plugins>
        <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>exec-maven-plugin</artifactId>
        <version>1.2.1</version>
        <executions>
            <execution>
                <goals>
                    <goal>java</goal>
                </goals>
                <configuration>
                    <mainClass>com.mertilovski.app.Game</mainClass> 
                </configuration>
            </execution>
        </executions>

        </plugin>
    </plugins>
</build>

My main class in under app directory.

TicTacToe/TicTacToe-Game/src/main/java/com/mertilovski/app

pom.xml is in TicTacToe directory. That is where I run

mvn exec:java -Dexec.mainClass="com.mertilovski.app.Game"

I tried making an package com.mertilovski.app; declaration on top.

mvn exec:java -Dexec.mainClass="src.main.java.com.mertilovski.app.com.mertilovski.app.Game "

Result is :

[INFO] Scanning for projects...
[INFO]                                                                         
[INFO] ------------------------------------------------------------ 
------------
[INFO] Building TicTacToe-Game 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------ 
------------
[INFO] 
[INFO] >>> exec-maven-plugin:1.2.1:java (default-cli) > validate @ 
TicTacToe-Game >>>
[INFO] 
[INFO] <<< exec-maven-plugin:1.2.1:java (default-cli) < validate @ 
TicTacToe-Game <<<
[INFO] 
[INFO] --- exec-maven-plugin:1.2.1:java (default-cli) @ TicTacToe- 
Game ---
[WARNING] 
java.lang.ClassNotFoundException: com.mertilovski.app.Game
at java.net.URLClassLoader.findClass(URLClassLoader.java:382)
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:285)
at java.lang.Thread.run(Thread.java:748)
[INFO] ------------------------------------------------------------ 
------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------ 
------------
[INFO] Total time: 0.639 s
[INFO] Finished at: 2019-03-26T01:30:18+03:00
[INFO] Final Memory: 8M/106M
[INFO] ------------------------------------------------------------ 
------------
[ERROR] Failed to execute goal org.codehaus.mojo:exec-maven- 
plugin:1.2.1:java (default-cli) on project TicTacToe-Game: An 
exception occured while executing the Java class. 
com.mertilovski.app.Game -> [Help 1]
[ERROR]   
[ERROR] To see the full stack trace of the errors, re-run Maven 
with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug 
logging.
[ERROR] 
[ERROR] For more information about the errors and possible 
solutions, please read the following articles:
[ERROR] [Help 1] 

http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException

mertk
  • 19
  • 1
  • 10
  • Please provide the whole exception. Also if you're on Windows - see https://stackoverflow.com/questions/9846046/run-main-class-of-maven-project – Not a JD Mar 25 '19 at 22:33
  • Did you compile the code first? – Slaw Mar 25 '19 at 22:34
  • Yes I did. I am in linux. I edited it for whole exception. Thank you. – mertk Mar 25 '19 at 22:39
  • "java.lang.ClassNotFoundException: com.mertilovski.app.Game". The working copy/directory you're calling Maven from, does not have this class on the classpath. From the directory you're running Maven, do a >> find . -type f -name "Game.class" << and see if you can see the class file there. – Not a JD Mar 25 '19 at 22:40
  • ./src/main/java/com/mertilovski/app/Game.class and ./target/classes/Game.class – mertk Mar 25 '19 at 22:45
  • I run it as this : mvn exec:java -Dexec.mainClass="src.main.java.com.mertilovski.app.Game" Still same problem.Changed the pom with src.main.java.com.mertilovski.app.Game – mertk Mar 25 '19 at 22:47
  • It's strange that your `Game.class` file is ending up in the default package in the output directory. There also shouldn't be any `*.class` files inside the `src` directory. Does `Game` have the appropriate `package com.mertilovski.app;` declaration? – Slaw Mar 25 '19 at 22:50
  • I added package declaration but it caused this mess. I am really sorry. I tried using these paths but still the same problem. ./src/main/java/com/mertilovski/app/com/mertilovski/app/Game.class and ./src/main/java/com/mertilovski/app/Game.class and ./target/classes/Game.class – mertk Mar 25 '19 at 22:57

2 Answers2

1

Your XML structure seems incorrect as configuration node should be outside executions (docs):

        <executions>
            <execution>
                <goals>
                    <goal>java</goal>
                </goals>
            </execution>
        </executions>
        <configuration>
            <mainClass>com.mertilovski.app.Game</mainClass> 
        </configuration>
Marcin Orlowski
  • 72,056
  • 11
  • 123
  • 141
0

Firstly I declared a package com.mertilovski.app in top of my Game.java file. Than I ran the

 mvn exec:java -Dexec.mainClass="com.mertilovski.app.Game"

pom.xml looks like this (remaining part is same)

                <configuration>
                    <mainClass>com.mertilovski.app.Game</mainClass> 
                </configuration>

When I run find . -type f -name "Game.class" I get :

./src/main/java/com/mertilovski/app/com/mertilovski/app/Game.class
./target/classes/com/mertilovski/app/Game.class
mertk
  • 19
  • 1
  • 10