public static void main()

in runtime/java/v17/lib/src/Launcher.java [114:211]


     public static void main(String[] args) throws Exception {

         initMain(args);

         // exit after main class loading if "exit" specified
         // used to check healthy launch after init
         if(args.length >1 && args[1] == "-exit")
             System.exit(0);

         // install a security manager to prevent exit
         //installSecurityManager();

         BufferedReader in = new BufferedReader(
                 new InputStreamReader(System.in, StandardCharsets.UTF_8));
         PrintWriter out = new PrintWriter(
                 new OutputStreamWriter(
                         new FileOutputStream("/dev/fd/3"), StandardCharsets.UTF_8));
         JsonParser json = new JsonParser();
         JsonObject emptyForJsonObject = json.parse("{}").getAsJsonObject();
         JsonArray emptyForJsonArray = json.parse("[]").getAsJsonArray();
         Boolean isJsonObjectParam = true;
         String input = "";
         while (true) {
             try {
                 input = in.readLine();
                 if (input == null)
                     break;
                 JsonElement element = json.parse(input);
                 JsonObject payloadForJsonObject = emptyForJsonObject.deepCopy();
                 JsonArray payloadForJsonArray = emptyForJsonArray.deepCopy();
                 Map<String, String> env = new HashMap<>();
                 if (element.isJsonObject()) {
                     // collect payload and environment
                     for (Map.Entry<String, JsonElement> entry : element.getAsJsonObject().entrySet()) {
                         if (entry.getKey().equals("value")) {
                             if (entry.getValue().isJsonObject())
                                 payloadForJsonObject = entry.getValue().getAsJsonObject();
                             else {
                                 payloadForJsonArray = entry.getValue().getAsJsonArray();
                                 isJsonObjectParam = false;
                             }
                         } else {
                             env.put(String.format("__OW_%s", entry.getKey().toUpperCase()),
                                     entry.getValue().getAsString());
                         }
                     }
                     augmentEnv(env);
                 }

                 Method m = null;
                 if (isJsonObjectParam) {
                     m = mainClass.getMethod(mainMethodName, new Class[] { JsonObject.class });
                 } else {
                     m = mainClass.getMethod(mainMethodName, new Class[] { JsonArray.class });
                 }
                 m.setAccessible(true);
                 int modifiers = m.getModifiers();
                 if ((m.getReturnType() != JsonObject.class && m.getReturnType() != JsonArray.class) || !Modifier.isStatic(modifiers) || !Modifier.isPublic(modifiers)) {
                     throw new NoSuchMethodException(mainMethodName);
                 }
                 mainMethod = m;

                 Object response;
                 if (isJsonObjectParam) {
                     response = invokeMain(payloadForJsonObject, env);
                 } else {
                     response = invokeMain(payloadForJsonArray, env);
                 }
                 out.println(response.toString());
             } catch(NullPointerException npe) {
                 System.out.println("the action returned null");
                 npe.printStackTrace(System.err);
                 JsonObject error = new JsonObject();
                 error.addProperty("error", "the action returned null");
                 out.println(error.toString());
                 out.flush();
             } catch(InvocationTargetException ite) {
                 Throwable ex = ite;
                 if(ite.getCause() != null)
                     ex = ite.getCause();
                 ex.printStackTrace(System.err);
                 JsonObject error = new JsonObject();
                 error.addProperty("error", ex.getMessage());
                 out.println(error.toString());
                 out.flush();
             } catch (Exception ex) {
                 ex.printStackTrace(System.err);
                 JsonObject error = new JsonObject();
                 error.addProperty("error", ex.getMessage());
                 out.println(error.toString());
                 out.flush();
             }
             out.flush();
             System.out.flush();
             System.err.flush();
         }
         //uninstallSecurityManager();
     }