title: How to set the source for compilation by a CompilationTask
body: <p>I do not know how to set the source file for a <code>compilationTask</code>. </p>
  
  <p>I tried this:</p>
  
  <pre><code>JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
  
  List&lt;String&gt; optionList = new ArrayList&lt;String&gt;(Arrays.asList("-d","build/classes"));
  
  List&lt;String&gt; classes = new ArrayList&lt;String&gt;();
  classes.add("src/Hello.java");
  CompilationTask task = compiler.getTask(null, null, null, optionList, classes, null);
  
  task.call();
  </code></pre>
  
  <p>But I get the following error:</p>
  
  <blockquote>
    <p>Exception in thread "main" java.lang.IllegalArgumentException: Not a valid class name: src/Hello.java</p>
  </blockquote>
  
  <p>of course, if I put null instead of classes I get "no source files" as no source file was given. I tried using the run function of the <code>JavaCompiler</code> before this but I could not specify the options in the string arguments (Or I do not know how).</p>
  
  <p>Here is the solution:</p>
  
  <pre><code>JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
  
          StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null);
          List&lt;String&gt; optionList = new ArrayList&lt;String&gt;(Arrays.asList("-d","build/classes"));
  
   Iterable&lt;? extends JavaFileObject&gt; classes = fileManager.getJavaFileObjectsFromFiles(Arrays.asList(new File("src/Hello.java")));
  
  
  CompilationTask task = compiler.getTask(null, null, null, optionList,null, classes);
  
  task.call();
  </code></pre>