I am attempting to write a Java project, which will compile any Java project and export a *.jar file. The program expects 3 runtime arguments, which I am specifying as:
C:\dev\Kronos\Kronos
vendor
src\com\starworks\kronos
Here is my code so far:
public class GenJar {
private static String PROJECT_DIR;
private static String DEPENDENCIES_DIR;
private static String SRC_DIR;
private static final List<String> s_classpath = new ArrayList<String>();
private static final List<String> s_sourceFiles = new ArrayList<String>();
public static void main(String[] args) {
PROJECT_DIR = args[0];
DEPENDENCIES_DIR = PROJECT_DIR + File.separator + args[1];
SRC_DIR = PROJECT_DIR + File.separator + args[2];
File dependenciesDir = new File(DEPENDENCIES_DIR);
for (File file : dependenciesDir.listFiles()) {
if (!file.getName().endsWith(".jar") || (file.getName().contains("sources") || file.getName().contains("javadoc"))) {
continue;
}
s_classpath.add(file.getPath());
System.out.println("Added dependency: " + file.getPath());
}
File srcDir = new File(SRC_DIR);
for (File file : srcDir.listFiles()) {
addSourceFiles(file);
}
int success = compile();
if (success != 0) {
System.err.println("Compilation failed!");
System.exit(1);
}
System.out.println("Compilation successful!");
Manifest manifest = new Manifest();
manifest.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION, "1.0");
manifest.getMainAttributes().put(Attributes.Name.CLASS_PATH, String.join(";", s_classpath));
exportJar();
}
private static int compile() {
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
return compiler.run(null, null, null, "-d", PROJECT_DIR + File.separator + "bin", "-cp", String.join(File.pathSeparator, s_classpath), String.join(" ", s_sourceFiles));
}
private static void exportJar() {
}
private static void addSourceFiles(File file) {
if (file.isDirectory()) {
File[] files = file.listFiles();
if (files != null) {
for (File f : files) {
addSourceFiles(f);
}
}
} else {
s_sourceFiles.add(file.getPath());
System.out.println("Added source file: " + file.getPath());
}
}
}
When running the program, this error occurs:
(%fn[x]% stands for file name, since they are quite long; filename e.g. C:\dev\Kronos\Kronos\src\com\starworks\kronos\Configuration.java)
error: Invalid filename: %fn[0]% %fn[1]% %fn[2]% .. %fn[N]%
Usage: javac <options> <source files>
use --help for a list of possible options
Compilation failed!
I believe the issue boils down to not knowing how to properly target multiple source files for compilation, but I am not sure what the issue is precisely. Any assistance would be apricated.
I wrote an application which targets the directory of a Java project, to then be exported as a *.jar file. I expected the code to compile the source directory of a java project, but the files aren't being targeted in the proper way.