You need to include the output directory in the compiler path. Notice how I include targetDirectory
in the classpath below:
public final class JavaCompiler { private List<Path> sourcePath = new ArrayList<>(); private List<Path> classPath = new ArrayList<>(); private final Set<DebugType> debugOptions = new HashSet<>(Arrays.asList(DebugType.LINES, DebugType.SOURCE, DebugType.VARIABLES)); public JavaCompiler sourcePath(List<Path> sourcePath) { Preconditions.checkNotNull(sourcePath, "sourcePath may not be null"); for (Path path: sourcePath) { if (!Files.exists(path)) { throw new IllegalArgumentException("sourcePath refers to non-existant path: " + path. toAbsolutePath()); } if (!Files.isDirectory(path)) { throw new IllegalArgumentException("sourcePath refers to a non-directory: " + path. toAbsolutePath()); } } this.sourcePath = ImmutableList.copyOf(sourcePath); return this; } public JavaCompiler classPath(List<Path> classPath) { Preconditions.checkNotNull(classPath, "classPath may not be null"); for (Path path: classPath) { if (!Files.exists(path)) { throw new IllegalArgumentException("classPath refers to non-existant path: " + path. toAbsolutePath()); } } this.classPath = ImmutableList.copyOf(classPath); return this; } public JavaCompiler debug(DebugType... debugOptions) { this.debugOptions.clear(); this.debugOptions.addAll(Arrays.asList(debugOptions)); return this; } public void run(final Collection<Path> sourceFiles, final Path targetDirectory) throws IllegalArgumentException, CompilationException { if (sourceFiles == null) throw new IllegalArgumentException("sourceFiles may not be null"); if (sourceFiles.isEmpty()) return; for (Path file: sourceFiles) { if (!Files.exists(file)) { throw new IllegalArgumentException("sourceFiles refers to a non-existant file: " + file.toAbsolutePath()); } if (!Files.isRegularFile(file)) { throw new IllegalArgumentException("sourceFiles refers to a non-file: " + file.toAbsolutePath()); } } if (targetDirectory == null) throw new IllegalArgumentException("targetDirectory may not be null"); if (!Files.exists(targetDirectory)) { throw new IllegalArgumentException("targetDirectory must exist: " + targetDirectory. toAbsolutePath()); } if (!Files.isDirectory(targetDirectory)) { throw new IllegalArgumentException("targetDirectory must be a directory: " + targetDirectory. toAbsolutePath()); } Set<Path> uniqueSourceFiles = ImmutableSet.copyOf(sourceFiles); Set<Path> uniqueSourcePath = ImmutableSet.copyOf(sourcePath); final javax.tools.JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); if (compiler == null) { throw new AssertionError("javax.tools.JavaCompiler is not available. Is tools.jar missing " + "from the classpath?"); } final DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<>(); final StandardJavaFileManager fileManager = compiler.getStandardFileManager(diagnostics, null, null); Iterable<? extends JavaFileObject> compilationUnits; try { Set<File> modifiedFiles = getModifiedFiles(uniqueSourceFiles, uniqueSourcePath, targetDirectory, new HashSet<Path>()); if (modifiedFiles.isEmpty()) return; compilationUnits = fileManager.getJavaFileObjectsFromFiles(modifiedFiles); } catch (IOException e) { throw new CompilationException(e); } final List<Path> effectiveClasspath = new ArrayList<>(); effectiveClasspath.add(targetDirectory); effectiveClasspath.addAll(classPath); final List<String> options = new ArrayList<>(); options.add("-cp"); options.add(Joiner.on(File.pathSeparatorChar).join(effectiveClasspath)); final StringBuilder debugLine = new StringBuilder("-g:"); for (DebugType type: debugOptions) { switch (type) { case LINES: { debugLine.append("lines,"); break; } case SOURCE: { debugLine.append("source,"); break; } case VARIABLES: { debugLine.append("vars,"); break; } default: throw new AssertionError(type); } } if (!debugOptions.isEmpty()) { debugLine.deleteCharAt(debugLine.length() - ",".length()); options.add(debugLine.toString()); } if (!uniqueSourcePath.isEmpty()) { options.add("-sourcepath"); options.add(Joiner.on(File.pathSeparatorChar).join(uniqueSourcePath)); } options.add("-s"); options.add(targetDirectory.toString()); options.add("-d"); options.add(targetDirectory.toString()); final Writer output = null; final CompilationTask task = compiler.getTask(output, fileManager, diagnostics, options, null, compilationUnits); final boolean result = task.call(); try { printDiagnostics(diagnostics, options, sourceFiles); } catch (IOException e) { throw new BuildException(e); } if (!result) throw new CompilationException(); try { fileManager.close(); } catch (IOException e) { throw new BuildException(e); } } private static File getJavaSource(String className, Set<File> sourcePath) {
source share