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

37
LINES

< > BotCompany Repo | #1000364 // Quick Java compile and in-memory run test (for solvers, developing)

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>();
    String javacOutput = _x16.compileJava(srcDir, 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);
  }
}

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: #1000364
Snippet name: Quick Java compile and in-memory run test (for solvers, developing)
Eternal ID of this version: #1000364/1
Text MD5: 0ec93f7415347b7691d513f2d588a976
Author: stefan
Category: javax
Type: JavaX source code
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2015-07-29 18:33:21
Source code size: 1346 bytes / 37 lines
Pitched / IR pitched: No / Yes
Views / Downloads: 623 / 512
Referenced in: [show references]