0

I am trying to run the JUnit Test program through the command line from Java application. In java file, I am using the following commands,

Runtime rt = Runtime.getRuntime();
Process pr = rt.exec("java -jar map.jar time.rel test.txt debug");

In the exec function, I am adding the following command to run the Junit,

java -cp cp.txt org.junit.runner.JUnitCore Testing

In place of cp.txt, I gave the classpath which is very long and this approach throws me the following error,

Cannot run program "java": CreateProcess error=206, The filename or extension is too long

So I want to place the string in cp.txt and give it as classpath in the command. Any help would be greatly appreciated. TIA

harry
  • 45
  • 1
  • 10

1 Answers1

0

You can achieve this by just storing the content of your text file to a variable and then use this as your class path. If your using e.g. bash you can simple do that in a one-liner:

java -cp $(cat cp.txt) org.junit.runner.JUnitCore Testing

If your text file contains newline, you could even simply replace them with ; like this:

java -cp $(cat cp.txt | tr '\n' ';') org.junit.runner.JUnitCore Testing

I'm pretty sure you can do this on Windows too, but there the syntax is probably slightly different. This post should help you there.

Jan Gassen
  • 3,406
  • 2
  • 26
  • 44
  • Even for a variable, there is a limit, so unable to store the entire classpath in the variable. – harry May 30 '18 at 13:25