6

I type the following in a Windows shell in the root of a Maven project that contains a class with a

public static void main(String[] args)

method that I'd like to run.

mvn exec:java -Dexec.mainClass="com.spp.config.main.SqlGeneratorHarness" -e

The class exists and is compiled in that package (i.e., target/classes/com/spp/config/main/SqlGeneratorHarness.class).

I see...

+ Error stacktraces are turned on.  
[INFO] Scanning for projects...  
[INFO] Searching repository for plugin with prefix: 'exec'.  
[INFO] ------------------------------------------------------------------------  
[ERROR] BUILD FAILURE  
[INFO] ------------------------------------------------------------------------  
[INFO] Invalid task '.mainClass=com.spp.config.main.SqlGeneratorHarness': you must specify a valid 
       lifecycle phase, or a goal in the format plugin:goal or pluginGroupId:pluginArtifactId:pluginVersion:goal
[INFO] ------------------------------------------------------------------------
[INFO] Trace org.apache.maven.BuildFailureException: Invalid task' .mainClass=com.spp.config.main.SqlGeneratorHarness': you must specify 
       a valid lifecycle phase, or a goal in the format plugin:goal or pluginGroupId:pluginArtifactId:pluginVersion:goal
       at org.apache.maven.lifecycle.DefaultLifecycleExecutor.getMojoDescriptor(DefaultLifecycleExecutor.java:1830)
       at org.apache.maven.lifecycle.DefaultLifecycleExecutor.segmentTaskListByAggregationNeeds(DefaultLifecycleExecutor.java:462)
       at org.apache.maven.lifecycle.DefaultLifecycleExecutor.execute(DefaultLifecycleExecutor.java:175)
       at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:328)
       at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:138)
       at org.apache.maven.cli.MavenCli.main(MavenCli.java:362)
       at org.apache.maven.cli.compat.CompatibleMain.main(CompatibleMain.java:60)
       at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
       at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
       at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
       at java.lang.reflect.Method.invoke(Method.java:597)
       at org.codehaus.classworlds.Launcher.launchEnhanced(Launcher.java:315)
       at org.codehaus.classworlds.Launcher.launch(Launcher.java:255)
       at org.codehaus.classworlds.Launcher.mainWithExitCode(Launcher.java:430)
       at org.codehaus.classworlds.Launcher.main(Launcher.java:375)
[INFO] ------------------------------------------------------------------------
[INFO] Total time: < 1 second
[INFO] Finished at: Tue Sep 27 14:33:52 PDT 2011
[INFO] Final Memory: 3M/122M
[INFO] ------------------------------------------------------------------------

I've tried variations like

mvn exec:exec -Dexec.executable="java" [...]

and

mvn org.codehaus.mojo:exec-maven-plugin:1.2.1:java [...]

to no avail. What gives?

I'm running Maven 2.2.1, Java JDK 1.6.0_27 on Windows 7 Enterprise 64-bit.

A snippet from my pom.xml for the exec-maven-plugin is...

<plugin> 
   <groupId>org.codehaus.mojo</groupId> 
   <artifactId>exec-maven-plugin</artifactId> 
   <version>${exec-maven-plugin.version}</version> 
</plugin> 

where the version is set in the <properties>

Chris Phillipson
  • 795
  • 1
  • 15
  • 34
  • Maven does not found the exec plugin. Show/check your `pom.xml` and `settings.xml`. – palacsint Sep 27 '11 at 22:23
  • I can see that version 1.2.1 is downloaded into my local .m2 repository. If I manually delete the folder for exec-maven-plugin and retry I get the same exception after it downloads and install the plugin into my local .m2 repo. – Chris Phillipson Sep 28 '11 at 15:15
  • A snippet from my pom.xml for the exec-maven-plugin is... ` org.codehaus.mojo exec-maven-plugin ${exec-maven-plugin.version} ` where the version is set in the `` section to be 1.2.1. – Chris Phillipson Sep 28 '11 at 15:15
  • This probably seems insultingly crazy, but... is it at all possible that you've consistently inserted a space or otherwise caused a parsing error directly after `-Dexec` - before the period? – Ed Staub Sep 28 '11 at 20:03

3 Answers3

19

So... revisiting this... if you use Windows PowerShell, you will get the exception I originally reported. If, however, you use cmd.exe, then you should be able to run the class with the command as I posted (with or without wrapping double-quotes).

Chris Phillipson
  • 795
  • 1
  • 15
  • 34
  • 4
    I wasted a **ridiculous** amount of time trying to run Maven archetype:generate with `-D` arguments with `.` in the arguments from PowerShell. Using `bash` via Cygwin via GitBash it works fine, and `cmd.exe` as well. PowerShell is **THE SUCK!** It is the `.` that PowerShell is trying to parse, once I got a real `bash` shell on my machine, I didn't even try and figure out how to make PowerShell work, ah the warm fuzzies of a shell that works! –  Sep 29 '11 at 21:32
  • I suspected you might be using PowerShell. I've never used it myself, but I'm not surprised that was the problem. – Ryan Stewart Sep 30 '11 at 23:50
  • 3
    This answer is about quotes in powershell http://stackoverflow.com/a/21913401/2822347 So typing (see quotes!) `mvn exec:java "-Dexec.mainClass=com.mypackage.Logger"` may solve the problem – Victor Bashurov Feb 27 '15 at 09:11
  • I meet this problem in powershell,and run the same script in cmd,it ok! – Edidada Apr 30 '20 at 01:47
1

There's something very basic going wrong. Try losing the quotation marks. They're not needed there. In fact, try starting with something simpler, like

mvn -e exec:java -Dexec.mainClass=foo 

That should give you the error "An exception occured while executing the Java class. foo", and the root cause should be a ClassNotFoundException:

Caused by: java.lang.ClassNotFoundException: foo
        at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
        at java.security.AccessController.doPrivileged(Native Method)

Type it out yourself. Don't copy/paste the command from somewhere else.

Ryan Stewart
  • 126,015
  • 21
  • 180
  • 199
  • I did as you said. Even without the double-quotes I get the same exception as I posted. As well, if I type in what you suggested to see if I could get the ClassNotFoundException, I rather received the same Exception as for my own class. Something is not quite right. I can issue a `mvn clean install` to build my project and I have no problems. It must center around config of the maven-exec-plugin. Any other ideas? – Chris Phillipson Sep 28 '11 at 15:08
  • How long have you been using this installation of Maven? Have you ever successfully run `exec:java` with it? If you still have the Maven zip/tar around, check the checksum against those [listed on the download page](http://maven.apache.org/download.html). Perhaps just try downloading and installing Maven again. It seems like it must be a problem either with Maven itself or with the OS's parsing of command line args. Are you using plain old Windows command prompt (`cmd.exe`)? If not, try it from there. – Ryan Stewart Sep 29 '11 at 02:36
  • Oh, you could also try adding a space after the `-D`, like: `...-D exec.mainClass=...`. It's a perfectly legit alternative, and it might shed some light on what's going on. – Ryan Stewart Sep 29 '11 at 02:38
  • Due to time crunch and problems getting exec-maven-plugin to work, I opted for a script. I use http://maven.apache.org/plugins/maven-dependency-plugin/build-classpath-mojo.html to get a string of the effective classpath, then I can just run utility class using java -cp {classpath} com.spp.config.main.SqlGeneratorHarness. – Chris Phillipson Sep 29 '11 at 20:41
  • Ryan, the Maven version I use comes bundled with SpringSource ToolSuite (STS). It's Maven 2.2.1 and it came bundles with STS 2.7.1. – Chris Phillipson Sep 29 '11 at 20:51
0

Just a quick checklist:

  1. Rename ~/.m2 folder and run mvn exec:java ... again and let Maven to download everything again from central.
  2. Check that your exec plugin is downloaded from central. (Check that your local repository contains the same files as the central.)
  3. Looking for suspicious repository and pluginRepository tags in the pom.xml (and parent poms as well).
  4. Looking for suspicious repository, pluginRepository and mirror tags in the settings.xml.
  5. Checking the project in an other machine - copy it and try to build/run exec:java in an other machine.
  6. Download Maven 3.x and try to run the exec with it.
palacsint
  • 28,416
  • 10
  • 82
  • 109