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

1  
!636
2  
!1000363 // class _x16
3  
4  
main {
5  
  static Method findMainMethod(Class<?> theClass) {
6  
    for (Method method : theClass.getMethods())
7  
      if (method.getName().equals("main") && method.getParameterTypes().length == 1)
8  
        return method;
9  
    throw new RuntimeException("Method 'main' with 1 parameter not found in " + theClass.getName());
10  
  }
11  
  
12  
  psvm {
13  
    String mainSrc = "return (String) o + \" \" + (String) o;";
14  
    String prelude = "public class main { public static Object main(Object o) throws Exception {\n";
15  
    String postlude = "\n}}";
16  
    String src = prelude + mainSrc + postlude;
17  
    File srcDir = _x16.TempDirMaker_make();
18  
    File classesDir = _x16.TempDirMaker_make();
19  
    _x16.saveTextFile(new File(srcDir, "main.java").getPath(), src);
20  
    List<File> libraries = new ArrayList<File>();
21  
    File transpiledDir = _x16.topLevelTranslate(srcDir, libraries);
22  
    String javacOutput = _x16.compileJava(transpiledDir, libraries, classesDir);
23  
    System.out.println(javacOutput);
24  
    URL[] urls = {classesDir.toURI().toURL()};
25  
    
26  
    // make class loader
27  
    URLClassLoader classLoader = new URLClassLoader(urls);
28  
29  
    // load main class
30  
    Class<?> mainClass = classLoader.loadClass("main");
31  
    
32  
    Method m = findMainMethod(mainClass);
33  
    
34  
    Object in = "helloinput";
35  
    Object out = m.invoke(null, in);
36  
    System.out.println(in + " => " + out);
37  
  }
38  
}

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: 584 / 507
Referenced in: [show references]