Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

From Maven, how do I run a class that lives under src/test/java?

I have inherited a codebase :)

Under src/test/java/ there's a file that I need to run (I need to run its public static void main(String[] args), not a @Test method within it).

The closest I have got is:

mvn -e exec:java -Dexec.mainClass="com.me.packagex.RunFile" -Dexec.classpathScope="test" 

but that then fails, and it appears to be because RunFile wants to use classes that exist under src/main/java/com/me/packagex/ (notice, /main/, not /test/). The files under there are in the same package as RunFile, i.e. 'package com.me.packagex;'.

If I remove the -Dexec.classpathScope="test" then it can't find RunFile at all. It's as if I need to give it two scopes, but it doesn't accept "test,compile".

The person I've inherited this from (dearly departed) used to run it from Eclipse. I need a way to run it from the command-line.

I hope this is clearly explained.

tyvm,


This is promising. Pascal, I've tried your example and that doesn't work for me.

Although now I look at it - it's not finding Demo, rather than not finding Dog.

Apache Maven 2.2.1 (rdebian-1) Java version: 1.6.0_18 Java home: /usr/lib/jvm/java-6-openjdk/jre Default locale: en_GB, platform encoding: UTF-8 OS name: "linux" version: "2.6.32-25-generic" arch: "i386" Family: "unix"  $ mvn -e exec:java -Dexec.mainClass="com.stackoverflow.Demo" -Dexec.classpathScope="test"  [ERROR] BUILD ERROR [INFO] ------------------------------------------------------------------------ [INFO] An exception occured while executing the Java class. com.stackoverflow.Demo  [INFO] ------------------------------------------------------------------------ [INFO] Trace org.apache.maven.lifecycle.LifecycleExecutionException: An exception occured while executing the Java class. com.stackoverflow.Demo         at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeGoals(DefaultLifecycleExecutor.java:719)         at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeStandaloneGoal(DefaultLifecycleExecutor.java:569)         at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeGoal(DefaultLifecycleExecutor.java:539)         at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeGoalAndHandleFailures(DefaultLifecycleExecutor.java:387)         at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeTaskSegments(DefaultLifecycleExecutor.java:348)         at org.apache.maven.lifecycle.DefaultLifecycleExecutor.execute(DefaultLifecycleExecutor.java:180)         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) Caused by: org.apache.maven.plugin.MojoExecutionException: An exception occured while executing the Java class. com.stackoverflow.Demo         at org.codehaus.mojo.exec.ExecJavaMojo.execute(ExecJavaMojo.java:346)         at org.apache.maven.plugin.DefaultPluginManager.executeMojo(DefaultPluginManager.java:490)         at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeGoals(DefaultLifecycleExecutor.java:694)         ... 17 more Caused by: java.lang.ClassNotFoundException: com.stackoverflow.Demo         at java.net.URLClassLoader$1.run(URLClassLoader.java:202)         at java.security.AccessController.doPrivileged(Native Method)         at java.net.URLClassLoader.findClass(URLClassLoader.java:190)         at java.lang.ClassLoader.loadClass(ClassLoader.java:307)         at java.lang.ClassLoader.loadClass(ClassLoader.java:248)         at org.codehaus.mojo.exec.ExecJavaMojo$1.run(ExecJavaMojo.java:284)         at java.lang.Thread.run(Thread.java:619) 
like image 477
woddle Avatar asked Oct 30 '10 20:10

woddle


People also ask

How do I run a specific class in maven?

In order to do this, we can use the exec-maven-plugin. To be more specific, the exec:java goal from this plugin executes the supplied Java class with the enclosing project's dependencies as the classpath. As shown above, we're using the exec. mainClass system property to pass the fully qualified class name.

What is SRC test java in maven?

src/main/java places your code that use for real production. src/test/java places your test use case code, like junit test. These codes would be executed when doing maven package things. These codes won't be packaged to your war or jar file.

How do I add resources to SRC test?

Right click on maven project --->Click on Build Path ----->Click on New Source Folder. New source folder window will open, give the name to your folder example - src/test/source. click on Finish.

Which folder will have the test classes in a maven java project?

Test reports are available in consumerBanking\target\surefire-reports folder. Maven compiles the source code file(s) and then tests the source code file(s).


2 Answers

I faced the same problem and figured it out.

Shortly speaking, the class must be compiled before exec:java goal. (It surely works without test-compile phase if the class is already compiled by other user action. Note that Pascal Thivent, in his answer, invoked mvn test before exec:java.)

$ mvn -Dexec.mainClass=... -Dexec.classpathScope=test test-compile exec:java 

You can prove it for yourself if you want to see the ClassNotFoundException again.

$ mvn -Dexec.mainClass=... -Dexec.classpathScope=test clean exec:java 
like image 127
Jin Kwon Avatar answered Sep 28 '22 23:09

Jin Kwon


(...) I hope this is clearly explained.

Not bad but I can't reproduce. I created a project:

 $ mvn archetype:generate -DgroupId=com.stackoverflow \                          -DartifactId=Q4060613 \                          -Dversion=1.0-SNAPSHOT \                          -DarchetypeArtifactId=maven-archetype-quickstart  

Then cded into it and created a Dog class (under src/main/java):

$ cd Q4060613 $ cat > src/main/java/com/stackoverflow/Dog.java package com.stackoverflow;  public class Dog {     public String bark() {         return "woof!";     } } 

and created a Demo class (under src/test/java):

$ cat > src/test/java/com/stackoverflow/Demo.java  package com.stackoverflow;  public class Demo {     public static void main(String[] args) {         System.out.println(new Dog().bark());     } } 

After compiling the source code, running the command you provided works as expected:

 $ mvn test ... $ mvn exec:java -Dexec.mainClass="com.stackoverflow.Demo" -Dexec.classpathScope="test" [INFO] Scanning for projects... ... [INFO] --- exec-maven-plugin:1.2:java (default-cli) @ Q4060613 --- woof! [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ ... 

Something else must be wrong.

like image 34
Pascal Thivent Avatar answered Sep 28 '22 23:09

Pascal Thivent