11

How to write a proper Hello World program in Java:

public class ProperJavaProgram {
    public static void main(String[] args) {
        try {
            // Write the hello world file
            List<String> lines = Arrays.asList(
                    "#include <stdio.h>",
                    "int main() {",
                    "printf(\"Hello World!\\n\");",
                    "return 0;",
                    "}"
            );
            Path file = Paths.get("awesome-program.c");
            Files.write(file, lines, Charset.forName("UTF-8"));

            // Execute the file
            executeCommand("cc awesome-c-program.c -o awesome-executable");
            executeCommand("./awesome-executable");
        } catch (IOException e) {
            System.out.println("You're screwed, just use Java and get over it. " + e);
        }
    }

    public static void executeCommand(String command) {
        try {
            Process p = Runtime.getRuntime().exec(command); // Run the process
            BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream())); // Get the output
            String s; // Print out the output
            while ((s = stdInput.readLine()) != null) {
                System.out.println(s);
            }
        } catch (IOException e) {
            System.out.println("You're screwed, just use Java and get over it. " + e); // UR SCREWED
        }
    }
}

Comments
Add Comment