2

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() +"\nIn file:  \n"+ diagnostic.toString()+"\n\n");
        }
    }
    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:\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
Hip Hip Array
  • 4,665
  • 11
  • 49
  • 80
  • Probably because your code is not formatted properly. – Roman C Sep 10 '12 at 11:18
  • Can you put some of the error messages in the question please? That might help. – Preet Sangha Sep 10 '12 at 11:21
  • 2
    I suspect your class path doesn't include the base directory of the class you are compiling. In this case I wouldn't expect it to look in the same directory. – Peter Lawrey Sep 10 '12 at 11:24
  • For better help sooner, post an [SSCCE](http://sscce.org/). – Andrew Thompson Sep 10 '12 at 11:25
  • @RomanC Since when have compilers cared about 'proper' code formatting? And since when would that cause multiple compilations to fail but not single compilations? – user207421 Sep 10 '12 at 11:29
  • @EJP Poorly formatted code is a complete headache for compilers as for they use it in the not readable format. To better compiler work it should be prepared in the corresponding format. – Roman C Sep 10 '12 at 11:37
  • @RomanC: it is not down to poorly formatted code, this was the first thing i though and I formatted the code and I made no difference. – Hip Hip Array Sep 10 '12 at 11:48
  • @PeterLawrey: does `-classpath` in the `compileOptions` not do this? – Hip Hip Array Sep 10 '12 at 11:53
  • The `-classpath` does this, if it set to include the directories you need. Can you print out the classpath to ensure it include the base directory of the code you expect it to see? – Peter Lawrey Sep 10 '12 at 11:57
  • @RomanC Compilers only care about syntax, not presentation. You can write the entire program one line. As long as it is syntactically correct the compiler couldn't care less. – user207421 Sep 14 '12 at 23:49

1 Answers1

2

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.

Alderath
  • 3,761
  • 1
  • 25
  • 43
  • but within the file that IntegerComparator is in, there is the interface Comparator.java, this is interface that IntegerComparator is implementing... Should this not be picked up by the compilers classpath or am i missing a package import in IntegerComparator.java for this? – Hip Hip Array Sep 10 '12 at 13:07
  • @HipHipArray Oh. My mistake. As there already is a Comparator interface in the standard java libraries, I assumed you were using that. As the interface is defined by you, this is (like people have already said) most likely a classpath problem. Try adding `;C:\projects\Compiler\Studentfilestotest\LAB3\aroc1\a3` to the end of the classpath and see if it works (this is assuming that you are using the default package for your IntegerComparator class). – Alderath Sep 10 '12 at 14:35
  • 2
    I.e., prior to calling the `doCompilation` method, you could try doing `System.setProperty("java.class.path", System.getProperty("java.class.path") + ";C:\\projects\\Compiler\\Studentfilestotest\\LAB3\\aroc1\\a3");` – Alderath Sep 10 '12 at 14:38
  • Yes, this did the trick, it was a classpath problem... thanks for the help:) – Hip Hip Array Sep 10 '12 at 16:18
  • change `java.class.path` via `System.setProperty("java.class.path") ` also fix my issue. – Eric Mar 20 '15 at 20:06