Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

67
LINES

< > BotCompany Repo | #1003843 // javaCompileToJar

JavaX fragment (include)

1  
static File javaCompileToJar(S src, File destJar) {
2  
  ret javaCompileToJar(src, "", destJar);
3  
}
4  
5  
// returns path to jar
6  
static synchronized File javaCompileToJar(S src, S dehlibs, File destJar) {
7  
  S javaTarget = null; // use default target
8  
  
9  
  print("Compiling " + l(src) + " chars");
10  
  S md5 = md5(src);
11  
  File jar = destJar;
12  
13  
  Class j = getJavaX();
14  
  if (javaTarget != null)
15  
    setOpt(j, "javaTarget", javaTarget);
16  
  //setOpt(j, "verbose", true);
17  
  File srcDir = cast call(j, "TempDirMaker_make");
18  
  L<S> tok = javaTok(src);
19  
  S className = getNameOfPublicClass(tok);
20  
  if (className == null) className = getNameOfAnyClass(tok);
21  
  if (className == null) fail("No classes defined in source!?");
22  
  S fileName = className + ".java";
23  
  File mainJava = new File(srcDir, fileName);
24  
  //print("main java: " + mainJava.getAbsolutePath());
25  
  saveTextFile(mainJava, src);
26  
  File classesDir = (File) call(j, "TempDirMaker_make");
27  
  new L<File> libraries;
28  
  
29  
  Matcher m = Pattern.compile("\\d+").matcher(dehlibs);
30  
  while (m.find()) {
31  
    S libID = m.group();
32  
    //print("libID=" + quote(libID));
33  
    assertTrue(isSnippetID(libID));
34  
    libraries.add(loadLibrary(libID));
35  
  }
36  
    
37  
  S compilerOutput;
38  
  try {
39  
    compilerOutput = (S) call(j, "compileJava", srcDir, libraries, classesDir);
40  
  } catch (Throwable e) {
41  
    compilerOutput = (S) get(getJavaX(), "javaCompilerOutput");
42  
    fail("Compile Error. " + compilerOutput + " " + e);
43  
  }
44  
  
45  
  if (nempty(compilerOutput)) {
46  
    print("Compiler said: " + compilerOutput);
47  
    //fail("Compile Error. " + compilerOutput);
48  
  }
49  
50  
  // sanity test
51  
  if (!new File(classesDir, className + ".class").exists())
52  
    fail("No class generated (" + className + ")");
53  
      
54  
  // add sources to .jar
55  
  saveTextFile(new File(classesDir, "main.java"), src);
56  
  
57  
  // add information about libraries to jar
58  
  if (nempty(dehlibs))
59  
    saveTextFile(new File(classesDir, "libraries"), dehlibs);
60  
61  
  //print("Zipping: " + classesDir.getAbsolutePath() + " to " + jar.getAbsolutePath());
62  
  dir2zip_recurse_verbose = false;
63  
  int n = dir2zip_recurse(classesDir, jar); // cache on success only
64  
  //print("Files zipped: " + n);
65  
66  
  return jar;
67  
}

Author comment

Began life as a copy of #1003840

download  show line numbers  debug dex  old transpilations   

Travelled to 14 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, ddnzoavkxhuk, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1003843
Snippet name: javaCompileToJar
Eternal ID of this version: #1003843/1
Text MD5: 6aa35e30c24a5e038327b58a3ad659e5
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2017-01-09 22:42:36
Source code size: 2229 bytes / 67 lines
Pitched / IR pitched: No / No
Views / Downloads: 665 / 764
Referenced in: [show references]