0

I can easily compile any Java classes using this code:

    System.out.println("Compiling student source files");
    Path destinationCompiledClassFile = Paths.get("/submissions/" + moduleCode + "/Assessment" + assessmentNumber + "/" + studentId + "/Submission"
            + submissionNumber + "/" + assessmentFolderName + "/" + "build/classes/");

    Path classPath = Paths.get("/submissions/" + moduleCode + "/Assessment" + assessmentNumber + "/" + studentId + "/Submission"
            + submissionNumber + "/" + assessmentFolderName + "/" + "build/classes/oojavabootcamp" + "/");
    Path javaFileToCompile = Paths.get("/submissions/" + moduleCode + "/Assessment" + assessmentNumber + "/" + studentId + "/Submission"
            + submissionNumber + "/" + assessmentFolderName + "/" + "src/OOJavaBootcamp");

    for (String fileToCompile : filesToCompile) {
        String compileCommand = "cmd /c javac -d \"" +  destinationCompiledClassFile
                + "\" -cp \"" + classPath
                + "\" \"" + javaFileToCompile + "/"
                + fileToCompile + ".java\" >> \""
                + submissionPathString + "/"
                + compilerOutputFileName + "\" 2>&1";

I am trying to compile the test.class file in a simple Java application by adding JUnit JAR file in the classpath. This should add the new compiled test.class file to the specified -d directory. I have deleted the existing file in this path 'C:\submissions\SOFT222\Assessment1\10403435\Submission20\OO Java Bootcamp - Java and NetBeans\build\test\classes\oojavabootcamp' and need the new compiled file here.

static String testFileName = "OOJavaBootcampTest";
final static String jUnitFileName = "junit-4.11.jar";

 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\\" + jUnitFileName
            + "\"; \"" + "C:\\submissions\\SOFT222\\Assessment1\\10403435\\Submission20\\OO Java Bootcamp - Java and NetBeans\\test\\oojavabootcamp" + "/"
            + testFileName + ".java\"; >> \"" + ""
            + "C:\\Users\\aookpidi\\Desktop\\testCompilerOutput.txt" + "\" 2>&1";

    System.out.println("Compile student source command: "
            + compileCommand);

The compiler error I keep getting is:

     javac: invalid flag: C:\submissions\SOFT222\junit-4.11.jar;
     Usage: javac <options> <source files>
     use -help for a list of possible options
Buzz
  • 55
  • 1
  • 11
  • Sorry, but why do you want to write a Java program that runs the java compiler? Java is a great programming language; but there much better BUILD SYSTEMS around that do compile your source code and unit test files. Like ... plain old make; or maven, ant, gradle, ... What I am trying to say is: writing a java program to invoke "javac" is like using a hammer to drivel in a screw ... – GhostCat Mar 23 '15 at 10:03
  • @EddyG Yeah I know. I am trying to run students Java submissions and need to replace their submitted files as it might have been tampered with. So i delete and replace it before their tests is ran – Buzz Mar 23 '15 at 10:06
  • 1
    Still I think that you would be better of using a script language like python for such a task. And to come closer to answering your question: you should provide the OUTPUT of your java code. It doesnt matter how your string is put together; what matters is only how the string looks like when you invoke it ... obviously the string that you put together is somehow wrong; and that can only be seen ... when we can inspect that string. – GhostCat Mar 23 '15 at 10:11

1 Answers1

1

Please check this answer to another question:
https://stackoverflow.com/a/2096321/4211402

Basically, it points out that you may need to add \* to the end of directories in building CLASSPATH in Windows.

I hope it helps.

Community
  • 1
  • 1
izce
  • 189
  • 2
  • 10