Uses 3106K of libraries. Click here for Pure Java version (12527L/77K).
1 | sclass CompileJavaToJar { |
2 | S src; |
3 | S dehlibs; |
4 | File destJar; |
5 | S progIDForRename; |
6 | S progID; |
7 | S sourceLevel = "11"; |
8 | IF1<S, File> compileSrcLib; |
9 | |
10 | // before you use this, add a RAM disk cleaner |
11 | bool useRAMDisk; |
12 | L<File> localLibraries; |
13 | S mainClassName; |
14 | File mainJava; |
15 | new JavaCompilation jc; |
16 | S compilerOutput; |
17 | |
18 | swappable void addMoreFiles(File classesDir) {} |
19 | |
20 | run { |
21 | print("Compiling " + nChars(src)); |
22 | S dummyClass = "main"; |
23 | if (progIDForRename != null) { |
24 | dummyClass = dummyMainClassName(progIDForRename); |
25 | src += "\nclass " + dummyClass + "{}"; |
26 | } |
27 | S md5 = md5(src); |
28 | File jar = destJar; |
29 | |
30 | File srcDir = tempDir(); |
31 | S fileName = dummyClass + ".java"; |
32 | |
33 | // derive name of main Java file from source |
34 | LS tok = javaTok(src); |
35 | S packageName = tok_packageName(tok); |
36 | S cname = tok_firstClassName(tok); |
37 | if (nempty(packageName)) { |
38 | fileName = packageName.replace(".", "/") + "/" + cname + ".java"; |
39 | mainClassName = packageName + "." + cname; |
40 | } else |
41 | mainClassName = cname; |
42 | mainJava = new File(srcDir, fileName); |
43 | |
44 | print("CompileJavaToJar main java: " + mainJava.getAbsolutePath()); |
45 | print("CompileJavaToJar mainClassName: " + mainClassName); |
46 | saveTextFile(mainJava, src); |
47 | File classesDir = useRAMDisk ? tempDirPossiblyInRAMDisk() : tempDir(); |
48 | saveTextFile(newFile(classesDir, fileName), src); |
49 | //print("Compiling to " + f2s(classesDir)); |
50 | try { |
51 | L<File> libraries = cloneList(localLibraries); |
52 | |
53 | Matcher m = Pattern.compile("\\d+").matcher(dehlibs); |
54 | while (m.find()) { |
55 | S libID = m.group(); |
56 | //print("libID=" + quote(libID)); |
57 | assertTrue(isSnippetID(libID)); |
58 | print("Adding library " + libID); |
59 | temp tempSetTL(loadLibraryOrSrcLib_compiler, compileSrcLib); |
60 | libraries.add(loadLibraryOrSrcLib(libID)); |
61 | } |
62 | |
63 | libraries.add(pathToJavaxJar()); |
64 | |
65 | try { |
66 | jc.srcDir = srcDir; |
67 | jc.libraries = libraries; |
68 | jc.classesDir = classesDir; |
69 | jc.sourceLevel = sourceLevel; |
70 | jc.run(); |
71 | compilerOutput = jc.compilerOutput; |
72 | } catch (Throwable e) { |
73 | compilerOutput = jc.compilerOutput; |
74 | //fail("Compile Error. " + cleanJavaCompilerOutput(compilerOutput) + " " + e); |
75 | compilerOutput = indentx("> ", cleanJavaCompilerOutput(compilerOutput)); |
76 | if (!swic(e.getMessage(), "Java compiler returned errors.")) |
77 | compilerOutput = appendWithNewLine(compilerOutput, str(e)); |
78 | //printStackTrace(e); |
79 | fail(compilerOutput, e); |
80 | } |
81 | |
82 | compilerOutput = cleanJavaCompilerOutput("Annotation processing got disabled, since it requires a 1.6 compliant JVM"); |
83 | |
84 | if (nempty(compilerOutput)) { |
85 | print("Compiler said: " + compilerOutput); |
86 | //fail("Compile Error. " + compilerOutput); |
87 | } |
88 | |
89 | // sanity test |
90 | if (!anyFileWithExtensionInDir(classesDir, ".class")) { |
91 | //printWithIndent("SRC> ", src); |
92 | fail("No classes generated (was compiling " + nChars(src) + ")"); |
93 | } |
94 | |
95 | // add sources to .jar |
96 | //saveTextFile(new File(classesDir, "main.java"), src); |
97 | |
98 | // add information about libraries to jar |
99 | if (nempty(dehlibs)) |
100 | saveTextFile(new File(classesDir, "libraries"), dehlibs); |
101 | |
102 | // add prog id to jar |
103 | saveTextFile(new File(classesDir, "progID"), progID); |
104 | |
105 | // save pointer to main Java source |
106 | //saveTextFile(new File(classesDir, "main-src"), fileName); |
107 | |
108 | addMoreFiles(classesDir); |
109 | |
110 | //print("Zipping: " + classesDir.getAbsolutePath() + " to " + jar.getAbsolutePath()); |
111 | dir2zip_recurse_verbose = false; |
112 | long time = sysNow(); |
113 | int n = dir2zip_recurse(classesDir, jar); // cache on success only |
114 | done2_always(time, nFiles(n) + " zipped"); |
115 | } finally { |
116 | if (isInRAMDisk(classesDir)) deleteDirectory(classesDir); |
117 | } |
118 | } |
119 | } |
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: | 181 / 280 |
Version history: | 21 change(s) |
Referenced in: | [show references] |