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

67
LINES

< > BotCompany Repo | #1003843 // javaCompileToJar

JavaX fragment (include)

static File javaCompileToJar(S src, File destJar) {
  ret javaCompileToJar(src, "", destJar);
}

// returns path to jar
static synchronized File javaCompileToJar(S src, S dehlibs, File destJar) {
  S javaTarget = null; // use default target
  
  print("Compiling " + l(src) + " chars");
  S md5 = md5(src);
  File jar = destJar;

  Class j = getJavaX();
  if (javaTarget != null)
    setOpt(j, "javaTarget", javaTarget);
  //setOpt(j, "verbose", true);
  File srcDir = cast call(j, "TempDirMaker_make");
  L<S> tok = javaTok(src);
  S className = getNameOfPublicClass(tok);
  if (className == null) className = getNameOfAnyClass(tok);
  if (className == null) fail("No classes defined in source!?");
  S fileName = className + ".java";
  File mainJava = new File(srcDir, fileName);
  //print("main java: " + mainJava.getAbsolutePath());
  saveTextFile(mainJava, src);
  File classesDir = (File) call(j, "TempDirMaker_make");
  new L<File> libraries;
  
  Matcher m = Pattern.compile("\\d+").matcher(dehlibs);
  while (m.find()) {
    S libID = m.group();
    //print("libID=" + quote(libID));
    assertTrue(isSnippetID(libID));
    libraries.add(loadLibrary(libID));
  }
    
  S compilerOutput;
  try {
    compilerOutput = (S) call(j, "compileJava", srcDir, libraries, classesDir);
  } catch (Throwable e) {
    compilerOutput = (S) get(getJavaX(), "javaCompilerOutput");
    fail("Compile Error. " + compilerOutput + " " + e);
  }
  
  if (nempty(compilerOutput)) {
    print("Compiler said: " + compilerOutput);
    //fail("Compile Error. " + compilerOutput);
  }

  // sanity test
  if (!new File(classesDir, className + ".class").exists())
    fail("No class generated (" + className + ")");
      
  // 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);

  //print("Zipping: " + classesDir.getAbsolutePath() + " to " + jar.getAbsolutePath());
  dir2zip_recurse_verbose = false;
  int n = dir2zip_recurse(classesDir, jar); // cache on success only
  //print("Files zipped: " + n);

  return jar;
}

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: 666 / 765
Referenced in: #1006654 - Standard functions list 2 (LIVE, continuation of #761)
#1007180 - javaCompileToJar_standard - outdated