One way is to use the maven dependency plugin in the test project to copy the commandlineapp (and its dependencies) to the test project:
<plugin>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<phase>process-test-resources</phase>
<goals>
<goal>unpack-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/lib</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
Then from within the test project, you can get access to the target folder:
File testClassesFolder =
new File(CommandLineAppTest.class.getClassLoader().getResource(".").getFile());
We need to create a string with the class path. The classpath
String classPath = testClassesFolder.getParent() + File.separator + "lib";
Then pass this to ProcessBuilder:
ProcessBuilder pb =
new ProcessBuilder(
javaExecutablePath, "-cp", classPath, "my.org.CommandLineApp"
);