Getting “java.lang.NoClassDefFoundError: com/beust/jcommander/ParameterException”Exception

后端 未结 4 2035
梦谈多话
梦谈多话 2021-01-13 12:46

I wanted to invoke testng programmatically. Not eclipse plug-in.

I have associated \"testng-6.8.21.jar\" and running through eclipse and i ran below code:

         


        
4条回答
  •  爱一瞬间的悲伤
    2021-01-13 13:49

    As @sgrillon correctly pointed out, you need the correct Maven dependency, but also the shade plugin (https://maven.apache.org/plugins/maven-shade-plugin) to package a Uber-jar including all Maven dependencies for easy execution. This is what should be included in your pom.xml:

    ...
      
        
          com.beust
          jcommander
          1.48
        
      
    ...
        
          ...
          
            maven-compiler-plugin
            3.1
            
              1.8
              1.8
            
          
    
          
            org.apache.maven.plugins
            maven-shade-plugin
            2.3
            
              
                package
                
                  shade
                
                
                  true
                  runnable
                
              
            
          
    
        
    

    After you build the Maven package, you'll get your regular my-app-1.0-SNAPSHOT.jar file and also a my-app-1.0-SNAPSHOT-runnable.jar. This is what you should run, with the command:

    $ java -jar my-app-1.0-SNAPSHOT-runnable.jar
    

    You can verify with this command:

    $ jar tvf my-app-1.0-SNAPSHOT-runnable.jar
    

    that the shaded jar contains the JCommander classes (and those of all the other Maven dependencies), while the regular one doesn't.

提交回复
热议问题