Uses 3106K of libraries. Click here for Pure Java version (12527L/77K).
sclass CompileJavaToJar { S src; S dehlibs; File destJar; S progIDForRename; S progID; S sourceLevel = "11"; IF1<S, File> compileSrcLib; // before you use this, add a RAM disk cleaner bool useRAMDisk; L<File> 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<File> 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); } } }
Began life as a copy of #1011328
download show line numbers debug dex old transpilations
Travelled to 4 computer(s): bhatertpkbcr, ekrmjmnbrukm, mowyntqkapby, mqqgnosmbjvj
No comments. add comment
Snippet ID: | #1033529 |
Snippet name: | CompileJavaToJar [Java compiler stage 2] - successor of javaCompileToJar_optionalRename |
Eternal ID of this version: | #1033529/22 |
Text MD5: | 0916fece61937e41294caa182c7797a8 |
Transpilation MD5: | affbfa4b5b703c52a888367d2fde7189 |
Author: | stefan |
Category: | javax |
Type: | JavaX fragment (include) |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2022-03-06 23:19:09 |
Source code size: | 4110 bytes / 119 lines |
Pitched / IR pitched: | No / No |
Views / Downloads: | 179 / 278 |
Version history: | 21 change(s) |
Referenced in: | [show references] |