DmitrySemenov
DmitrySemenov

Reputation: 10315

Maven fails to compile java code (java.lang.ClassNotFoundException) with `mvn exec:java` call

Trying to follow instructions to compile kafka sample streams app

https://github.com/timothyrenner/kafka-streams-ex/tree/master/not-looking-at-facebook

I'm using Oracle java8 @ Fedora25

➜  not-looking-at-facebook git:(master) java -version                                                        
java version "1.8.0_121"
Java(TM) SE Runtime Environment (build 1.8.0_121-b13)
Java HotSpot(TM) 64-Bit Server VM (build 25.121-b13, mixed mode)

mvn -X exec:java produces the following error:

java.lang.ClassNotFoundException: io.github.timothyrenner.kstreamex.notification.NotLookingAtFacebook
        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)
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 0.517 s
[INFO] Finished at: 2017-03-21T16:17:41-07:00
[INFO] Final Memory: 17M/481M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.codehaus.mojo:exec-maven-plugin:1.4.0:java (default-cli) on project not-looking-at-facebook: An exception occured while executing the Java class. io.github.timothyrenner.kstreamex.notification.NotLookingAtFacebook -> [Help 1]
org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal org.codehaus.mojo:exec-maven-plugin:1.4.0:java (default-cli) on project not-looking-at-facebook: An exception occured while executing the Java class. io.github.timothyrenner.kstreamex.notification.NotLookingAtFacebook
        at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:212)
        at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:153)
        at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:145)
        at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:116)
        at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:80)
        at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build(SingleThreadedBuilder.java:51)
        at org.apache.maven.lifecycle.internal.LifecycleStarter.execute(LifecycleStarter.java:128)
        at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:307)
        at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:193)
        at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:106)
        at org.apache.maven.cli.MavenCli.execute(MavenCli.java:863)
        at org.apache.maven.cli.MavenCli.doMain(MavenCli.java:288)
        at org.apache.maven.cli.MavenCli.main(MavenCli.java:199)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:498)
        at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(Launcher.java:289)
        at org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java:229)
        at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(Launcher.java:415)
        at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:356)
Caused by: org.apache.maven.plugin.MojoExecutionException: An exception occured while executing the Java class. io.github.timothyrenner.kstreamex.notification.NotLookingAtFacebook
        at org.codehaus.mojo.exec.ExecJavaMojo.execute(ExecJavaMojo.java:345)
        at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:134)
        at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:207)
        ... 20 more
Caused by: java.lang.ClassNotFoundException: io.github.timothyrenner.kstreamex.notification.NotLookingAtFacebook
        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)
[ERROR] 
[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

I had to fix it with mvn clean test and then mvn exec:java worked. What was the problem?

(I'm new to maven/java ecosystem)

Upvotes: 1

Views: 3858

Answers (1)

Catchwa
Catchwa

Reputation: 5855

Your invocation of mvn exec:java failed because nothing had been compiled - and therefore the main class in question was missing.

Running mvn clean test invokes the maven-compiler-plugin, meaning that subsequent attempts to use the exec-maven-plugin (which is what's called when you invoke the exec:java goal) succeed.

If you take a look at this question, you'll see that it's not overly simple to bind the compilation of your source code directly to the exec:java goal. However, you can chain goals together so something like this will probably work:

mvn clean test exec:java

Upvotes: 3

Related Questions