2017-12-20 2 views
0

Ich habe diesen Code:ClassNotFoundException, wenn sie mit Maven Ausführung aber keine Fehler mit Intellij

public static void main(String[] args) throws ClassNotFoundException { 
    ClassLoader scl = ClassLoader.getSystemClassLoader(); 
    Class<?> c = scl.loadClass("junit.framework.TestCase"); 
    System.out.println(c); 
} 

Es kompiliert und läuft ganz gut in IntelliJ (Ausgabe von class junit.framework.TestCase).

Aber wenn ich es mit Exec-Maven-Plugin mit läuft es scheitert.

[INFO] Error stacktraces are turned on. 
[INFO] Scanning for projects... 
[INFO]                   
[INFO] ------------------------------------------------------------------------ 
[INFO] Building vev-dynamic-testing 0.0.1-SNAPSHOT 
[INFO] ------------------------------------------------------------------------ 
[INFO] 
[INFO] --- exec-maven-plugin:1.6.0:java (default-cli) @ vev-dynamic-testing --- 
[WARNING] 
java.lang.ClassNotFoundException: junit.framework.TestCase 
    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 istic.fr.vev_dynamic_testing.App.main(App.java:119) 
    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.mojo.exec.ExecJavaMojo$1.run(ExecJavaMojo.java:282) 
    at java.lang.Thread.run(Thread.java:748) 
[INFO] ------------------------------------------------------------------------ 
[INFO] BUILD FAILURE 
[INFO] ------------------------------------------------------------------------ 
[INFO] Total time: 1.565 s 
[INFO] Finished at: 2017-12-21T11:23:56+01:00 
[INFO] Final Memory: 9M/150M 
[INFO] ------------------------------------------------------------------------ 
[ERROR] Failed to execute goal org.codehaus.mojo:exec-maven-plugin:1.6.0:java (default-cli) on project vev-dynamic-testing: An exception occured while executing the Java class. junit.framework.TestCase -> [Help 1] 
org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal org.codehaus.mojo:exec-maven-plugin:1.6.0:java (default-cli) on project vev-dynamic-testing: An exception occured while executing the Java class. junit.framework.TestCase 
    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. junit.framework.TestCase 
    at org.codehaus.mojo.exec.ExecJavaMojo.execute(ExecJavaMojo.java:339) 
    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: junit.framework.TestCase 
    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 istic.fr.vev_dynamic_testing.App.main(App.java:119) 
    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.mojo.exec.ExecJavaMojo$1.run(ExecJavaMojo.java:282) 
    at java.lang.Thread.run(Thread.java:748) 
[ERROR] 
[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 

Der Klassenpfad ist klar gebrochen, aber ich sehe nicht wo.

Hier ist der pom.xml Eintrag des Plugins, nur für den Fall.

<plugin> 
    <groupId>org.codehaus.mojo</groupId> 
    <artifactId>exec-maven-plugin</artifactId> 
    <version>1.6.0</version> 
    <configuration> 
    <mainClass>istic.fr.vev_dynamic_testing.App</mainClass> 
    <arguments> 
     <argument>-classpath</argument> 
     <classpath/> 
    </arguments> 
    </configuration> 
</plugin> 

POM enthält junit als Abhängigkeit (in 4,12, ohne scope Attribut).

+0

Und was sehen Sie, wenn du machst es mit 'mvn -e'? –

+0

Junit befindet sich normalerweise im Testumfang, der dem exec-maven-plugin nicht zur Verfügung steht. –

Antwort

0

Sie benötigen JUnit als Maven dependancy im pom enthalten:

<dependency> 
<groupId>junit</groupId> 
<artifactId>junit</artifactId> 
<version>4.12</version> 
</dependency> 

Junit bereits mit IntelliJ gebündelt ist, weshalb der Code dort läuft ohne Abhängung

+0

Ich habe schon Junit un die Pom (in 4.12 ohne 'Scope' angegeben). – Umbra

+0

Versuchen Sie, den Kompilierbereich für jUnit anzugeben. – Spasoje

Verwandte Themen