JavaCompiler 无法正确编译文件 [英] JavaCompiler not compiling files properly

查看:25
本文介绍了JavaCompiler 无法正确编译文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试习惯 JavaCompiler 并尝试使用它编译程序,我可以成功编译包含单个文件的程序,但是当我尝试编译包含多个文件的项目时.我在编译实现其他类的文件以及该类使用已实现类中的方法时出错.

这是我用来编译java代码的代码

private final String directoryForBin = "C:/TempBINfolder/bin";公共列表 doCompilation(String sourceCode, String locationOfFile) {列表<字符串>compile = new ArrayList<>();SimpleJavaFileObject fileObject = new DynamicJavaSourceCodeObject(locationOfFile, sourceCode);JavaFileObject javaFileObjects[] = new JavaFileObject[]{fileObject};JavaCompiler 编译器 = ToolProvider.getSystemJavaCompiler();StandardJavaFileManager stdFileManager = compiler.getStandardFileManager(null, Locale.getDefault(), null);可迭代编译单位 = Arrays.asList(javaFileObjects);//在 C: 驱动器上创建一个 Temp BIN 文件夹以添加用于编译的 .class 文件新文件(directoryForBin).mkdirs();String[] compileOptions = new String[]{"-d", directoryForBin, "-classpath", System.getProperty("java.class.path")};可迭代的<字符串>编译选项 = Arrays.asList(compileOptions);诊断收集器诊断 = 新的诊断收集器();CompilationTask compilerTask = compiler.getTask(null, stdFileManager, 诊断, 编译选项, 空, 编译单元);布尔状态 = compilerTask.call();if (!status) {//如果出现编译错误//遍历每个编译问题并打印出来对于(诊断诊断:diagnostics.getDiagnostics()){compile.add(diagnostic.getKind().toString()+" on line "+ diagnostic.getLineNumber() +"
在文件中:
"+ diagnostic.toString()+"

");}}尝试 {stdFileManager.close();//关闭文件管理器} catch (IOException e) {e.printStackTrace();}返回编译;}

这是我尝试编译的其中一个类的示例

 公共类 IntegerComparator实现 Comparator{公共整数比较器(){}公共 int 比较(整数 a,整数 b){ int aValue = a.intValue();int bValue = b.intValue();返回 (aValue - bValue);}}

有谁知道这里出了什么问题,为什么我的代码没有找到实现的类,即使它与实现它的类在同一个文件夹中?

是不是因为包不在程序的顶部?

这是上面例子的输出错误

错误在第 8 行在文件中:/projects/Compiler/Studentfilestotest/LAB3/aroc1/a3/IntegerComparator.java:8: 错误:找不到符号实现 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.2javamodulesexteansbinding-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:projectsCompilerCompileruildclasses

解决方案

很可能,这与编译器无关.我的猜测是这是因为源文件中的错误.您是否记得导入适当的类和接口?>

您的 IntegerComparator.java 文件需要包含导入:

import java.util.Comparator;//<--- 导入!公共类 IntegerComparator实现 Comparator{公共整数比较器(){}公共 int 比较(整数 a,整数 b){int aValue = a.intValue();int bValue = b.intValue();返回 (aValue - bValue);}}

忘记导入适当的类通常会导致找不到符号"错误消息.

I am trying to get used to the JavaCompiler and trying to compile programs with it, I can successfully compile programs that comprises of single file but when im trying to compile projects with multiple files. I get errors on compiling files that implement other classes and where ever the class uses a method from the implemented class.

Here is the code that I am using to compile the java code

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() +"
In file:  
"+ diagnostic.toString()+"

");
        }
    }
    try {
        stdFileManager.close();//Close the file manager
    } catch (IOException e) {
        e.printStackTrace();
    }

    return compile;
}

Here is a sample of one of the classes I am trying to 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);
   }

}

Does anybody know what is going wrong here and why my code is not finding the implemented class even though it is in the same folder as that of the class implementing it?

Is it because the package is not located at the top of the program?

EDIT: This is the output error for the above example

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

This is what System.getProperty("java.class.path") is returning

C:projectsCompilerCompilerJar Filesjsyntaxpane-0.9.5-b29.jar;
C:projectsCompilerCompilerJar Filesjtar-1.1.jar;
C:projectsCompilerCompilerJar Filessqlitejdbc-v056.jar;
C:Program FilesNetBeans 7.1.2javamodulesexteansbinding-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:projectsCompilerCompileruildclasses

解决方案

Most likely, this has nothing to do with the compiler. My guess is that this is because of an error in the source file. Did you remember to import the appropriate classes and interfaces?

Your IntegerComparator.java file needs to contain the import:

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);
   }
}

Forgetting to import appropriate classes often results in the "cannot find symbol" error messages.

这篇关于JavaCompiler 无法正确编译文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆