JScratch
Loading...
Searching...
No Matches
ProjectRunner.java
Go to the documentation of this file.
1package com.jscratch.compiler;
2
3import com.jscratch.Stage;
4import java.io.File;
5import java.lang.reflect.Method;
6
7public class ProjectRunner {
8 private static java.net.URLClassLoader projectClassLoader;
9
10 public static void mount(File buildDir) {
11 try {
12 projectClassLoader = new java.net.URLClassLoader(
13 new java.net.URL[]{buildDir.toURI().toURL()},
14 ProjectRunner.class.getClassLoader()
15 );
16 } catch (Exception e) {
17 e.printStackTrace();
18 }
19 }
20
21 public static java.net.URLClassLoader getProjectClassLoader() {
22 return projectClassLoader;
23 }
24
25 public static void run(File buildDir, String mainClassName) {
26 // Kill existing stage if it exists
27 if (Stage.instance != null) {
29 }
30
31 // Create a completely new stage instance
32 Stage newStage = new Stage();
33
34 Stage.run(newStage, () -> {
35 try {
36 if (projectClassLoader == null) mount(buildDir);
37
38 Class<?> clazz = projectClassLoader.loadClass("generated." + mainClassName);
39 Method mainMethod = clazz.getMethod("main", String[].class);
40
41 // Execute Main.main(new String[0])
42 mainMethod.invoke(null, (Object) new String[0]);
43
44 } catch (Exception e) {
45 e.printStackTrace();
46 }
47 });
48 }
49}
static Stage instance
Definition Stage.java:27
static void run(Runnable setup)
Definition Stage.java:270
void disposeStage()
Definition Stage.java:316
static java.net.URLClassLoader getProjectClassLoader()
static void mount(File buildDir)
static java.net.URLClassLoader projectClassLoader
static void run(File buildDir, String mainClassName)