Duplicate :
|
JavaCompilerTool.setSourcePath() clears sourcepath in JavaCompilerTool.setOption("-sourcepath"). Tried in Solaris SunOS 5.10 Generic sun4u sparc SUNW,Ultra-60 <java-version> bash-3.00$ java -version java version "1.6.0-beta" Java(TM) 2 Runtime Environment, Standard Edition (build 1.6.0-beta-b59a) Java HotSpot(TM) Client VM (build 1.6.0-beta-b59a, mixed mode) </java-version> To re-produce the problem : 1) Keep the files Foo.java,JavacOptionsTest03.java in the same directory. 2) Create one directory with the name "Pack1" in the directory where Foo.java is kept 3) Keep Bar1.java in Pack1 4) Create one directory with the name "Pack2" in the directory where Foo.java is kept 5) Keep Bar2.java in Pack2 6) Run JavacOptionTest03. Testcases: <JavacOptionsTest03.java> import java.io.File; import java.io.OutputStream; import java.io.PrintWriter; import java.util.ArrayList; import java.util.List; import javax.tools.*; public class JavacOptionsTest03 { public static void main(String... arg) throws Exception { JavaCompilerTool javac = ToolProvider.defaultJavaCompiler(); JavaFileManager jfm = javac.getStandardFileManager(); javac.setOption("-sourcepath","Pack1"); List<File> cpFiles= new ArrayList<File>(); //cpFiles.add(new File("Pack1")); cpFiles.add(new File("Pack2")); javac.setClassPath(cpFiles); JavaFileObject jfo = jfm.getFileForInput("Foo.java"); Iterable<DiagnosticMessage> iteDM = javac.run((PrintWriter)null, jfo).getDiagnostics(); for(DiagnosticMessage message : iteDM) { System.out.println("\nMessage : "+message.toString()); } } } </JavacOptionsTest03.java> <Foo.java> public class Foo { public static void main(String[] args) { Bar1 bar1 = new Bar1(); Bar2 bar2 = new Bar2(); } } </Foo.java> <Bar1> public class Bar1 { } </Bar1> <Bar2> public class Bar2 { } </Bar2> Output of the code when JavacOptionsTest02 is run <Output> bash-3.00$ java JavacOptionsTest03 Message : Foo.java:4: cannot find symbol symbol : class Bar2 location: class Foo Message : Foo.java:4: cannot find symbol symbol : class Bar2 location: class Foo </Output> This behaviuor is not mentioned in the API docs If the line "//cpFiles.add(new File("Pack1"));" is uncommented, Then this will work fine OR Change the line javac.setOption("-sourcepath","Pack1") to javac.setOption("-sourcepath","Pack1","Pack2"); and remove the code javac.setSourcePath(..); If setSourcePath is used , user is not allowd to use setOption("-sourcepath"). I think compiler should append setOption stuff to setSourcePath instead of clearing up.