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

38
LINES

< > BotCompany Repo | #1000365 // Quick Java compile and in-memory run test (for solvers - now with translations enabled)

JavaX source code - run with: x30.jar

!636

!1000363 // class _x16

main {
  static Method findMainMethod(Class<?> theClass) {
    for (Method method : theClass.getMethods())
      if (method.getName().equals("main") && method.getParameterTypes().length == 1)
        return method;
    throw new RuntimeException("Method 'main' with 1 parameter not found in " + theClass.getName());
  }
  
  psvm {
    String mainSrc = "return (String) o + \" \" + (String) o;";
    String prelude = "public class main { public static Object main(Object o) throws Exception {\n";
    String postlude = "\n}}";
    String src = prelude + mainSrc + postlude;
    File srcDir = _x16.TempDirMaker_make();
    File classesDir = _x16.TempDirMaker_make();
    _x16.saveTextFile(new File(srcDir, "main.java").getPath(), src);
    List<File> libraries = new ArrayList<File>();
    File transpiledDir = _x16.topLevelTranslate(srcDir, libraries);
    String javacOutput = _x16.compileJava(transpiledDir, libraries, classesDir);
    System.out.println(javacOutput);
    URL[] urls = {classesDir.toURI().toURL()};
    
    // make class loader
    URLClassLoader classLoader = new URLClassLoader(urls);

    // load main class
    Class<?> mainClass = classLoader.loadClass("main");
    
    Method m = findMainMethod(mainClass);
    
    Object in = "helloinput";
    Object out = m.invoke(null, in);
    System.out.println(in + " => " + out);
  }
}

Author comment

Began life as a copy of #1000364

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

Snippet ID: #1000365
Snippet name: Quick Java compile and in-memory run test (for solvers - now with translations enabled)
Eternal ID of this version: #1000365/1
Text MD5: 03e4d9ba51a29bf3de35a49a097d4a79
Author: stefan
Category: javax
Type: JavaX source code
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2015-07-29 19:03:55
Source code size: 1422 bytes / 38 lines
Pitched / IR pitched: No / Yes
Views / Downloads: 581 / 504
Referenced in: [show references]