2

JavaCompilerに慣れて、それを使用してプログラムをコンパイルしようとすると、単一のファイルで構成されるプログラムを正常にコンパイルできますが、複数のファイルを使用してプロジェクトをコンパイルしようとすると、他のクラスを実装するファイルのコンパイルや、クラスが実装されたクラスのメソッドを使用する場合は常にエラーが発生します。

これが私がJavaコードをコンパイルするために使用しているコードです

private final String  directoryForBin = "C:/TempBINfolder/bin";

public List doCompilation(String sourceCode, String locationOfFile) {
   List<String> compile = new ArrayList<>();

    SimpleJavaFileObject fileObject = new DynamicJavaSourceCodeObject(locationOfFile, sourceCode);
    JavaFileObject javaFileObjects[] = new JavaFileObject[]{fileObject};

    JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();

    StandardJavaFileManager stdFileManager = compiler.getStandardFileManager(null, Locale.getDefault(), null);

    Iterable<? extends JavaFileObject> compilationUnits = Arrays.asList(javaFileObjects);

    // creates a Temp BIN foler on the C: drive to add .class files for compiling      
    new File(directoryForBin).mkdirs();

    String[] compileOptions = new String[]{"-d", directoryForBin, "-classpath", System.getProperty("java.class.path")};
    Iterable<String> compilationOptions = Arrays.asList(compileOptions);

    DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<JavaFileObject>();

    CompilationTask compilerTask = compiler.getTask(null, stdFileManager, diagnostics, compilationOptions, null, compilationUnits);

    boolean status = compilerTask.call();

    if (!status) {//If compilation error occurs 
        // Iterate through each compilation problem and print it
        for (Diagnostic diagnostic : diagnostics.getDiagnostics()) { 
            compile.add(diagnostic.getKind().toString()+" on line  "+ diagnostic.getLineNumber() +"\nIn file:  \n"+ diagnostic.toString()+"\n\n");
        }
    }
    try {
        stdFileManager.close();//Close the file manager
    } catch (IOException e) {
        e.printStackTrace();
    }

    return compile;
}

これが私がコンパイルしようとしているクラスの1つのサンプルです

    public class IntegerComparator 
             implements Comparator<Integer>
{
   public IntegerComparator(){}

   public int compare(Integer a, Integer b)
   {  int aValue = a.intValue();
      int bValue = b.intValue();
      return (aValue - bValue);
   }

}

ここで何が問題になっているのか、実装されているクラスと同じフォルダーにあるのに、コードが実装されているクラスを見つけられないのは誰か知っていますか?

パッケージがプログラムの一番上にないからですか?

編集:これは上記の例の出力エラーです

ERROR on line  8
In file:  
/projects/Compiler/Studentfilestotest/LAB3/aroc1/a3/IntegerComparator.java:8: error: cannot find symbol
             implements Comparator<Integer>
                        ^
  symbol: class Comparator

これがSystem.getProperty("java.class.path")戻ってきたものです

C:\projects\Compiler\Compiler\Jar Files\jsyntaxpane-0.9.5-b29.jar;
C:\projects\Compiler\Compiler\Jar Files\jtar-1.1.jar;
C:\projects\Compiler\Compiler\Jar Files\sqlitejdbc-v056.jar;
C:\Program Files\NetBeans 7.1.2\java\modules\ext\beansbinding-1.2.1.jar;
C:\Program Files\NetBeans 7.1.2\java\modules\ext\AbsoluteLayout.jar;
C:\projects\Compiler\Compiler\Jar Files\junit-4.11-SNAPSHOT-20120416-1530.jar;
C:\projects\Compiler\Compiler\Jar Files\commons-io-2.4-bin.zip;
C:\projects\Compiler\Compiler\Jar Files\commons-io-2.4\commons-io-2.4.jar;
C:\projects\Compiler\Compiler\Jar Files\poi-3.8\poi-3.8-20120326.jar;
C:\projects\Compiler\Compiler\Jar Files\javamail-1.4.5\mail.jar;
C:\projects\Compiler\Compiler\build\classes
4

1 に答える 1

2

ほとんどの場合、これはコンパイラとは何の関係もありません。私の推測では、これはソースファイルのエラーが原因であると思われます。適切なクラスとインターフェースをインポートすることを覚えていますか?

ファイルIntegerComparator.javaにはインポートが含まれている必要があります。

import java.util.Comparator; // <--- Import!

public class IntegerComparator 
      implements Comparator<Integer>{

   public IntegerComparator(){}

   public int compare(Integer a, Integer b){
      int aValue = a.intValue();
      int bValue = b.intValue();
      return (aValue - bValue);
   }
}

適切なクラスをインポートし忘れると、「シンボルが見つかりません」というエラーメッセージが表示されることがよくあります。

于 2012-09-10T12:30:22.267 に答える