sclass CompileJavaToJar { S src; S dehlibs; File destJar; S progIDForRename; S progID; S sourceLevel = "11"; IF1 compileSrcLib; // before you use this, add a RAM disk cleaner bool useRAMDisk; L localLibraries; S mainClassName; File mainJava; new JavaCompilation jc; S compilerOutput; swappable void addMoreFiles(File classesDir) {} run { print("Compiling " + nChars(src)); S dummyClass = "main"; if (progIDForRename != null) { dummyClass = dummyMainClassName(progIDForRename); src += "\nclass " + dummyClass + "{}"; } S md5 = md5(src); File jar = destJar; File srcDir = tempDir(); S fileName = dummyClass + ".java"; // derive name of main Java file from source LS tok = javaTok(src); S packageName = tok_packageName(tok); S cname = tok_firstClassName(tok); if (nempty(packageName)) { fileName = packageName.replace(".", "/") + "/" + cname + ".java"; mainClassName = packageName + "." + cname; } else mainClassName = cname; mainJava = new File(srcDir, fileName); print("CompileJavaToJar main java: " + mainJava.getAbsolutePath()); print("CompileJavaToJar mainClassName: " + mainClassName); saveTextFile(mainJava, src); File classesDir = useRAMDisk ? tempDirPossiblyInRAMDisk() : tempDir(); saveTextFile(newFile(classesDir, fileName), src); //print("Compiling to " + f2s(classesDir)); try { L libraries = cloneList(localLibraries); Matcher m = Pattern.compile("\\d+").matcher(dehlibs); while (m.find()) { S libID = m.group(); //print("libID=" + quote(libID)); assertTrue(isSnippetID(libID)); print("Adding library " + libID); temp tempSetTL(loadLibraryOrSrcLib_compiler, compileSrcLib); libraries.add(loadLibraryOrSrcLib(libID)); } libraries.add(pathToJavaxJar()); try { jc.srcDir = srcDir; jc.libraries = libraries; jc.classesDir = classesDir; jc.sourceLevel = sourceLevel; jc.run(); compilerOutput = jc.compilerOutput; } catch (Throwable e) { compilerOutput = jc.compilerOutput; //fail("Compile Error. " + cleanJavaCompilerOutput(compilerOutput) + " " + e); compilerOutput = indentx("> ", cleanJavaCompilerOutput(compilerOutput)); if (!swic(e.getMessage(), "Java compiler returned errors.")) compilerOutput = appendWithNewLine(compilerOutput, str(e)); //printStackTrace(e); fail(compilerOutput, e); } compilerOutput = cleanJavaCompilerOutput("Annotation processing got disabled, since it requires a 1.6 compliant JVM"); if (nempty(compilerOutput)) { print("Compiler said: " + compilerOutput); //fail("Compile Error. " + compilerOutput); } // sanity test if (!anyFileWithExtensionInDir(classesDir, ".class")) { //printWithIndent("SRC> ", src); fail("No classes generated (was compiling " + nChars(src) + ")"); } // add sources to .jar //saveTextFile(new File(classesDir, "main.java"), src); // add information about libraries to jar if (nempty(dehlibs)) saveTextFile(new File(classesDir, "libraries"), dehlibs); // add prog id to jar saveTextFile(new File(classesDir, "progID"), progID); // save pointer to main Java source //saveTextFile(new File(classesDir, "main-src"), fileName); addMoreFiles(classesDir); //print("Zipping: " + classesDir.getAbsolutePath() + " to " + jar.getAbsolutePath()); dir2zip_recurse_verbose = false; long time = sysNow(); int n = dir2zip_recurse(classesDir, jar); // cache on success only done2_always(time, nFiles(n) + " zipped"); } finally { if (isInRAMDisk(classesDir)) deleteDirectory(classesDir); } } }