如何在数组中编译多个Java文件

时间:2015-03-31 19:02:46

标签: java

static String[] filesToCompile = new String[]{"Player", "OOJavaBootcamp"};

我想使用下面的代码在数组中编译这两个Java文件,但它只编译数组中的Player对象。

 static String[] filesToCompile = new String[]{"Player", "OOJavaBootcamp"};
//Compile the student src file 
     System.out.println("\nCompiling tests file");
     Path destinationCompiledClassFile = Paths.get("C:\\Users\\aookpidi\\Desktop\\STUDEN~1\\SOFT22~1.2-6\\ALECCA~1\\OOJAVA~1\\build\\classes");
    Path classPath = Paths.get("C:\\Users\\aookpidi\\Desktop\\STUDEN~1\\SOFT22~1.2- 6\\ALECCA~1\\OOJAVA~1\\build\\classes\\OOJavaBootcamp\\");
    Path javaFileToCompile = Paths.get("C:\\Users\\aookpidi\\Desktop\\STUDEN~1\\SOFT22~1.2-6\\ALECCA~1\\OOJAVA~1\\src\\OOJavaBootcamp\\");
    Path submissionPathString = Paths.get("C:\\Users\\aookpidi\\Desktop\\outputFiles");
    String compilerOutputFileName = "compilerOutput.txt";

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

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

这是控制台输出:

         Compiling tests file
   Compile student source command: cmd /c javac -d "C:\Users\aookpidi\Desktop\STUDEN~1\SOFT22~1.2-6\ALECCA~1\OOJAVA~1\build\classes" -cp "C:\Users\aookpidi\Desktop\STUDEN~1\SOFT22~1.2-6\ALECCA~1\OOJAVA~1\build\classes\OOJavaBootcamp" "C:\Users\aookpidi\Desktop\STUDEN~1\SOFT22~1.2-6\ALECCA~1\OOJAVA~1\src\OOJavaBootcamp/Player.java" >> "C:\Users\aookpidi\Desktop\outputFiles\compilerOutput.txt" 2>&1
   Compile student source command: cmd /c javac -d "C:\Users\aookpidi\Desktop\STUDEN~1\SOFT22~1.2-6\ALECCA~1\OOJAVA~1\build\classes" -cp "C:\Users\aookpidi\Desktop\STUDEN~1\SOFT22~1.2-6\ALECCA~1\OOJAVA~1\build\classes\OOJavaBootcamp" "C:\Users\aookpidi\Desktop\STUDEN~1\SOFT22~1.2-6\ALECCA~1\OOJAVA~1\src\OOJavaBootcamp/OOJavaBootcamp.java" >> "C:\Users\aookpidi\Desktop\outputFiles\compilerOutput.txt" 2>&1
   BUILD SUCCESSFUL (total time: 2 seconds)

编译器输出如下所示:

   C:\Users\aookpidi\Desktop\STUDEN~1\SOFT22~1.2-6\ALECCA~1\OOJAVA~1\src\OOJavaBootcamp\OOJavaBootcamp.java:18: error: cannot find symbol
    Player p1 = new Player("Peter");
    ^
   symbol:   class Player
   location: class OOJavaBootcamp
    C:\Users\aookpidi\Desktop\STUDEN~1\SOFT22~1.2-  6\ALECCA~1\OOJAVA~1\src\OOJavaBootcamp\OOJavaBootcamp.java:18: error: cannot find symbol
    Player p1 = new Player("Peter");
                    ^
   symbol:   class Player
   location: class OOJavaBootcamp
   C:\Users\aookpidi\Desktop\STUDEN~1\SOFT22~1.2-6\ALECCA~1\OOJAVA~1\src\OOJavaBootcamp\OOJavaBootcamp.java:19: error: cannot find symbol
    Player p2 = new Player("Paul");
    ^
    symbol:   class Player
    location: class OOJavaBootcamp
    C:\Users\aookpidi\Desktop\STUDEN~1\SOFT22~1.2-6\ALECCA~1\OOJAVA~1\src\OOJavaBootcamp\OOJavaBootcamp.java:19: error: cannot find symbol
    Player p2 = new Player("Paul");
                    ^
    symbol:   class Player
   location: class OOJavaBootcamp
   C:\Users\aookpidi\Desktop\STUDEN~1\SOFT22~1.2-6\ALECCA~1\OOJAVA~1\src\OOJavaBootcamp\OOJavaBootcamp.java:21: error: cannot find symbol
    Player m = new Player("Mary");
    ^
    symbol:   class Player
    location: class OOJavaBootcamp
    C:\Users\aookpidi\Desktop\STUDEN~1\SOFT22~1.2-6\ALECCA~1\OOJAVA~1\src\OOJavaBootcamp\OOJavaBootcamp.java:21: error: cannot find symbol
    Player m = new Player("Mary");
                   ^
    symbol:   class Player
    location: class OOJavaBootcamp
    C:\Users\aookpidi\Desktop\STUDEN~1\SOFT22~1.2-6\ALECCA~1\OOJAVA~1\src\OOJavaBootcamp\OOJavaBootcamp.java:23: error: cannot find symbol
    Player.printAllPlayers();
    ^
   symbol:   variable Player
   location: class OOJavaBootcamp
   7 errors

OOJavaBootcamp文件包含:

         public class OOJavaBootcamp {

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    // TODO code application logic here
    Player p1 = new Player("Peter");
    Player p2 = new Player("Paul");
    p2.setAge(18);
    Player m = new Player("Mary");
    m.setAge(30);
    Player.printAllPlayers();
}

}

请问我做错了什么?我是新手程序员 感谢

0 个答案:

没有答案