-3

I can successsfully compile any java classes using this sample code:

  static String testFileName = "OOJavaBootcampTest";
  System.out.println("Compiling student test source file");

  String compileCommand = "cmd /c javac -d \"" + "C:\\submissions\\SOFT222\\Assessment1\\10403435\\Submission20\\OO Java Bootcamp - Java and NetBeans\\build\\test\\classes"
     + "\" -cp \"" + "C:\\submissions\\SOFT222\\Assessment1\\10403435\\Submission20\\OO Java Bootcamp - Java and NetBeans\\build\\test\\classes\\oojavabootcamp\"" + " "
     + "\" \"" + "C:\\submissions\\SOFT222\\Assessment1\\10403435\\Submission20\\OO Java Bootcamp - Java and NetBeans\\test\\oojavabootcamp" + "/"
     + testFileName + ".java\" >> \"" + ""
     + "C:\\Users\\aookpidi\\Desktop\\compilerOutput.txt" + "\" 2>&1";


        System.out.println("Compile student source command: "
                + compileCommand);
        try {
            Runtime.getRuntime().exec(compileCommand).
                    waitFor(5000, TimeUnit.MILLISECONDS);
        } catch (IOException | InterruptedException ex) {
        }

This works fine but compiling the Junit test class with this same code but jUnit classpath added:

final static String jUnitFileName = "junit-4.11.jar";
...
String compileCommand = "cmd /c javac -d \"" + "C:\\submissions\\SOFT222\\Assessment1\\10403435\\Submission20\\OO Java Bootcamp - Java and NetBeans\\build\\test\\classes"
   + "\" -cp \"" + "C:\\submissions\\SOFT222\\Assessment1\\10403435\\Submission20\\OO Java Bootcamp - Java and NetBeans\\build\\test\\classes\\oojavabootcamp\"" + " "
   + "\"C:\\submissions\\SOFT222\\" + jUnitFileName
   + "\" \"" + "C:\\submissions\\SOFT222\\Assessment1\\10403435\\Submission20\\OO Java Bootcamp - Java and NetBeans\\test\\oojavabootcamp" + "/"
   + testFileName + ".java\" >> \"" + ""
   + "C:\\Users\\aookpidi\\Desktop\\compilerOutput.txt" + "\" 2>&1";

The test.java file doesnt seem to copy to the specified directory in -d. This is error in the compilerOutput file:

 javac: invalid flag: C:\submissions\SOFT222\junit-4.11.jar
 Usage: javac <options> <source files>
 use -help for a list of possible options

Junit is definitely in the specified location.

Thomas Weller
  • 55,411
  • 20
  • 125
  • 222
Buzz
  • 55
  • 1
  • 11
  • which is test which is code? They both seems same to me? – SMA Mar 20 '15 at 13:42
  • @SMA I indicated clearly before the second code wa spasted that the jUnit classpath is added there. Cheers for the vote lol :)) – Buzz Mar 20 '15 at 13:46
  • `C:\submissions\SOFT222\junit-4.11.jar` is interpreted at a flag. Look at the command line. – Thomas Weller Mar 20 '15 at 13:47
  • possible duplicate of [Including jars in classpath on commandline (javac or apt)](http://stackoverflow.com/questions/2096283/including-jars-in-classpath-on-commandline-javac-or-apt) – Thomas Weller Mar 20 '15 at 13:47
  • sorry am not the downvoter but you could be more clear on by just pasting relevant code. – SMA Mar 20 '15 at 13:48
  • I have tried interpreting it as a flag but it comes up with a different error saying 'cannot find or load main class' – Buzz Mar 20 '15 at 14:07
  • @SMA I dont really mind tbh lol its a learning process :)) – Buzz Mar 20 '15 at 14:07

1 Answers1

0

Classpath entries need to be separated by a ; (on Windows)

String compileCommand = "cmd /c javac -d \"" + "C:\\submissions\\SOFT222\\Assessment1\\10403435\\Submission20\\OO Java Bootcamp - Java and NetBeans\\build\\test\\classes"
            + "\" -cp \"" + "C:\\submissions\\SOFT222\\Assessment1\\10403435\\Submission20\\OO Java Bootcamp - Java and NetBeans\\build\\test\\classes\\oojavabootcamp\"" + ";"
            + "\"C:\\submissions\\SOFT222\\" + jUnitFileName
            + "\" \"" + "C:\\submissions\\SOFT222\\Assessment1\\10403435\\Submission20\\OO Java Bootcamp - Java and NetBeans\\test\\oojavabootcamp" + "/"
            + testFileName + ".java\" >> \"" + ""
            + "C:\\Users\\aookpidi\\Desktop\\compilerOutput.txt" + "\" 2>&1"
Neil Masson
  • 2,609
  • 1
  • 15
  • 23