how can i run a jar file created by maven?


I have a Spring project and am trying to run a jar file created by Maven (install) from the terminal but am getting the following errors:

  • If I simply run "java -jar  name.version.jar ", I get "no main manifest attribute, in name.version.jar"
  • If I run "java -cp name.version.jar <main class>", I get ".. java.lang.ClassNotFoundException: org.springframework.context.ApplicationContext"

Any ideas on how I can run the maven generated jar file directly from the terminal?


1 comment
Comment actions Permalink

I was able to get this to work after some changes:

  1. The error with the "java -jar ..." command turned into the second error (one with "java -cp ..") after I updated the pom file and added the maven-jar-plugin with the corresponding mainClass element.
  2. The error with the ClassNotFoundException was resolved after I added a JAR artifact to the Project Structure and built it. It is important to choose the option to "copy to the output directory and link via manifest" when adding the artifact so that maven generated jar file can find the dependent jar files.

I feel that I am doing something redundant though. With the install phase, maven is putting my jar file to the maven repository, so I'd expect the dependency to be resolved and thus the dependent jar files to be found when I directly run the maven generated jar file. I should not have to build the artifacts (dependents) I think. Am I missing something or is there a potential enhancement here for the IDE?



Please sign in to leave a comment.