|
1 | | -package a1_java.lang; |
2 | | -import java.io.*; |
3 | | -import java.util.Map; |
4 | | -public class b11_RuntimeExample { |
5 | | - @SuppressWarnings({ "removal", "deprecation" }) |
6 | | - public static void main(String[] args) { |
| 1 | +package a1_java.lang; |
| 2 | + |
| 3 | +import java.io.*; |
| 4 | +import java.util.Map; |
| 5 | + |
| 6 | +public class b11_RuntimeExample { |
| 7 | + |
| 8 | + @SuppressWarnings({"removal", "deprecation"}) |
| 9 | + public static void main(String[] args) { |
7 | 10 | // Method 1: Get Runtime Instance |
8 | 11 | // This method returns the current runtime object, allowing access to JVM runtime methods |
9 | | - Runtime runtime = Runtime.getRuntime(); |
10 | | - System.out.println("1. Runtime Instance obtained successfully."); |
| 12 | + Runtime runtime = Runtime.getRuntime(); |
| 13 | + System.out.println("1. Runtime Instance obtained successfully."); |
| 14 | + |
11 | 15 | // Method 2 & 3: Memory Management Methods |
12 | 16 | // These methods provide information about JVM memory usage |
13 | | - long totalMemory = runtime.totalMemory(); |
14 | | - long freeMemory = runtime.freeMemory(); |
15 | | - System.out.println("2. Total Memory: " + totalMemory + " bytes"); |
16 | | - System.out.println("3. Free Memory: " + freeMemory + " bytes"); |
| 17 | + long totalMemory = runtime.totalMemory(); |
| 18 | + long freeMemory = runtime.freeMemory(); |
| 19 | + System.out.println("2. Total Memory: " + totalMemory + " bytes"); |
| 20 | + System.out.println("3. Free Memory: " + freeMemory + " bytes"); |
| 21 | + |
17 | 22 | // Method 4: Garbage Collection |
18 | 23 | // Suggests the JVM to run garbage collection process |
19 | | - runtime.gc(); |
20 | | - System.out.println("4. Garbage Collection suggested."); |
| 24 | + runtime.gc(); |
| 25 | + System.out.println("4. Garbage Collection suggested."); |
| 26 | + |
21 | 27 | // Method 5: Run Finalization |
22 | 28 | // Suggests running pending finalizer methods |
23 | | - runtime.runFinalization(); |
24 | | - System.out.println("5. Finalization process suggested."); |
| 29 | + runtime.runFinalization(); |
| 30 | + System.out.println("5. Finalization process suggested."); |
| 31 | + |
25 | 32 | // Method 7 & 8 & 9: Execute External Processes |
26 | | - try { |
| 33 | + try { |
27 | 34 | // Execute a simple command and read its output |
28 | | - Process process1 = runtime.exec("echo Hello, Runtime!"); |
29 | | - String output1 = readProcessOutput(process1); |
30 | | - System.out.println("7. Simple command execution result: " + output1); |
| 35 | + Process process1 = runtime.exec("echo Hello, Runtime!"); |
| 36 | + String output1 = readProcessOutput(process1); |
| 37 | + System.out.println("7. Simple command execution result: " + output1); |
| 38 | + |
31 | 39 | // Execute a command array and read its output |
32 | | - String[] cmdArray = {"cmd", "/c", "dir"}; |
33 | | - Process process2 = runtime.exec(cmdArray); |
34 | | - String output2 = readProcessOutput(process2); |
35 | | - System.out.println("8. Command array execution result length: " + output2.length()); |
| 40 | + String[] cmdArray = {"cmd", "/c", "dir"}; |
| 41 | + Process process2 = runtime.exec(cmdArray); |
| 42 | + String output2 = readProcessOutput(process2); |
| 43 | + System.out.println("8. Command array execution result length: " + output2.length()); |
| 44 | + |
36 | 45 | // Execute command with environment variables and read its output |
37 | | - String[] envp = {"PATH=" + System.getenv("PATH")}; |
38 | | - Process process3 = runtime.exec("echo %PATH%", envp); |
39 | | - String output3 = readProcessOutput(process3); |
40 | | - System.out.println("9. Command with environment variables execution result: " + output3); |
41 | | - } catch (IOException e) { |
42 | | - System.err.println("Error executing processes: " + e.getMessage()); } |
| 46 | + String[] envp = {"PATH=" + System.getenv("PATH")}; |
| 47 | + Process process3 = runtime.exec("echo %PATH%", envp); |
| 48 | + String output3 = readProcessOutput(process3); |
| 49 | + System.out.println("9. Command with environment variables execution result: " + output3); |
| 50 | + } catch (IOException e) { |
| 51 | + System.err.println("Error executing processes: " + e.getMessage()); |
| 52 | + } |
| 53 | + |
43 | 54 | // Method 10: Add Shutdown Hook |
44 | 55 | // Registers a thread to be executed when JVM is shutting down |
45 | | - Thread shutdownHook = new Thread(() -> { |
46 | | - System.out.println("10. Shutdown hook executed: Performing cleanup."); }); |
47 | | - runtime.addShutdownHook(shutdownHook); |
| 56 | + Thread shutdownHook = new Thread(() -> { |
| 57 | + System.out.println("10. Shutdown hook executed: Performing cleanup."); |
| 58 | + }); |
| 59 | + runtime.addShutdownHook(shutdownHook); |
| 60 | + |
48 | 61 | // Method 11: Remove Shutdown Hook (demonstration) |
49 | | - boolean hookRemoved = runtime.removeShutdownHook(shutdownHook); |
50 | | - System.out.println("11. Shutdown hook removal status: " + hookRemoved); |
| 62 | + boolean hookRemoved = runtime.removeShutdownHook(shutdownHook); |
| 63 | + System.out.println("11. Shutdown hook removal status: " + hookRemoved); |
| 64 | + |
51 | 65 | // Method 12: Get Environment Variables |
52 | 66 | // Retrieves all environment variables |
53 | | - Map<String, String> environmentVariables = System.getenv(); |
54 | | - System.out.println("12. Environment Variables:"); |
55 | | - environmentVariables.forEach((key, value) -> |
56 | | - System.out.println(key + " = " + value) ); |
| 67 | + Map<String, String> environmentVariables = System.getenv(); |
| 68 | + System.out.println("12. Environment Variables:"); |
| 69 | + environmentVariables.forEach((key, value) -> |
| 70 | + System.out.println(key + " = " + value)); |
| 71 | + |
57 | 72 | // Method 13: toString Representation |
58 | 73 | // Returns a string representation of the Runtime object |
59 | | - String runtimeString = runtime.toString(); |
60 | | - System.out.println("13. Runtime toString: " + runtimeString); |
| 74 | + String runtimeString = runtime.toString(); |
| 75 | + System.out.println("13. Runtime toString: " + runtimeString); |
| 76 | + |
61 | 77 | // Method 14: Available Processors |
62 | 78 | // Returns the number of processors available to the JVM |
63 | | - int processors = runtime.availableProcessors(); |
64 | | - System.out.println("14. Available Processors: " + processors); } |
| 79 | + int processors = runtime.availableProcessors(); |
| 80 | + System.out.println("14. Available Processors: " + processors); |
| 81 | + } |
| 82 | + |
65 | 83 | // Utility method to read output from a Process |
66 | | - private static String readProcessOutput(Process process) throws IOException { |
| 84 | + private static String readProcessOutput(Process process) throws IOException { |
67 | 85 | // Read the output of the process |
68 | | - try (BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()))) { |
69 | | - StringBuilder output = new StringBuilder(); |
70 | | - String line; |
71 | | - while ((line = reader.readLine()) != null) { |
72 | | - output.append(line).append("\n"); } |
73 | | - return output.toString().trim(); |
74 | | - } |
75 | | - } |
| 86 | + try (BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()))) { |
| 87 | + StringBuilder output = new StringBuilder(); |
| 88 | + String line; |
| 89 | + while ((line = reader.readLine()) != null) { |
| 90 | + output.append(line).append("\n"); |
| 91 | + } |
| 92 | + return output.toString().trim(); |
| 93 | + } |
| 94 | + } |
76 | 95 | } |
0 commit comments