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

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  
    String javacOutput = _x16.compileJava(srcDir, libraries, classesDir);
22  
    System.out.println(javacOutput);
23  
    URL[] urls = {classesDir.toURI().toURL()};
24  
    
25  
    // make class loader
26  
    URLClassLoader classLoader = new URLClassLoader(urls);
27  
28  
    // load main class
29  
    Class<?> mainClass = classLoader.loadClass("main");
30  
    
31  
    Method m = findMainMethod(mainClass);
32  
    
33  
    Object in = "helloinput";
34  
    Object out = m.invoke(null, in);
35  
    System.out.println(in + " => " + out);
36  
  }
37  
}

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