private static void addAllJavaFiles(File folder, List<String> result) throws IOException { for (File f : folder.listFiles()) { if (f.isDirectory()) { addAllJavaFiles(f, result); } else if (f.getName().endsWith(".java")) { result.add(f.toString()); System.out.println("Java file: " + f.getName()); System.out.println("------------------------------\n"); Path path = f.toPath(); Files.copy(path, System.out); System.out.println("\n------------------------------\n"); } } }
/** * Compiles java source files in the provided source directory, to the provided class directory. * This also runs findbugs on the compiled classes, throwing an exception if findbugs fails. */ public static void compileSrcFiles(String sourceDirPath, String classDirPath) throws Exception { List<String> srcFiles = new ArrayList<>(); addAllJavaFiles(new File(sourceDirPath), srcFiles); File classDir = new File(classDirPath); classDir.mkdir(); List<String> argList = new ArrayList<>(); argList.add("-d"); argList.add(classDir.getAbsolutePath()); argList.add("-classpath"); argList.add(System.getProperty(PROPERTY_CLASSPATH) + System.getProperty("path.separator") + classDirPath); argList.addAll(srcFiles); // argList.toArray() for large size had trouble String[] args = new String[argList.size()]; for (int i = 0; i < argList.size(); i++) { args[i] = argList.get(i); } JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); int err = compiler.run(null, System.out, System.out, args); if (err != 0) throw new RuntimeException("compiler errors, see system.out"); runFindbugs(classDir); }