我正在努力习惯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()+"nn");
}
}
try {
stdFileManager.close();//Close the file manager
} catch (IOException e) {
e.printStackTrace();
}
return compile;
}
以下是我试图编译的一个类的示例
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);
}
}
有人知道这里出了什么问题吗?为什么我的代码找不到实现的类,即使它和实现它的类在同一个文件夹中?
是因为程序包不在程序的顶部吗?
EDIT:这是上述示例的输出错误
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:projectsCompilerCompilerJar Filesjsyntaxpane-0.9.5-b29.jar;
C:projectsCompilerCompilerJar Filesjtar-1.1.jar;
C:projectsCompilerCompilerJar Filessqlitejdbc-v056.jar;
C:Program FilesNetBeans 7.1.2javamodulesextbeansbinding-1.2.1.jar;
C:Program FilesNetBeans 7.1.2javamodulesextAbsoluteLayout.jar;
C:projectsCompilerCompilerJar Filesjunit-4.11-SNAPSHOT-20120416-1530.jar;
C:projectsCompilerCompilerJar Filescommons-io-2.4-bin.zip;
C:projectsCompilerCompilerJar Filescommons-io-2.4commons-io-2.4.jar;
C:projectsCompilerCompilerJar Filespoi-3.8poi-3.8-20120326.jar;
C:projectsCompilerCompilerJar Filesjavamail-1.4.5mail.jar;
C:projectsCompilerCompilerbuildclasses
很可能,这与编译器无关。我的猜测是,这是因为源文件中的一个错误。您是否记得导入适当的类和接口?
您的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);
}
}
忘记导入适当的类通常会导致"找不到符号"错误消息。