add new core-java-os module

This commit is contained in:
Loredana
2019-03-11 23:02:10 +02:00
parent 7f24cbad71
commit 63b652270a
17 changed files with 161 additions and 19 deletions
@@ -1,15 +0,0 @@
package com.baeldung.java9.process;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;
public class ChildProcess {
public static void main(String[] args) {
@SuppressWarnings("resource")
Scanner input = new Scanner(System.in);
Logger log = Logger.getLogger(ChildProcess.class.getName());
log.log(Level.INFO, input.nextLine());
}
}
@@ -1,16 +0,0 @@
package com.baeldung.java9.process;
import java.util.logging.Level;
import java.util.logging.Logger;
public class OutputStreamExample {
public static void main(String[] args) {
Logger log = Logger.getLogger(OutputStreamExample.class.getName());
log.log(Level.INFO, Integer.toString(sum(1,2)));
}
public static int sum(int a, int b) {
return a + b;
}
}
@@ -1,7 +0,0 @@
package com.baeldung.java9.process;
public class ProcessCompilationError {
//This method has been written to generate error to display
//how process errorStream() can consume error
public static void();
}
@@ -1,110 +0,0 @@
package com.baeldung.java9.process;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.Optional;
import java.util.logging.Level;
import java.util.logging.Logger;
public class ProcessUnderstanding {
public static int compileAndRunJavaProgram() throws IOException {
Process process = Runtime.getRuntime()
.exec("javac -cp src src\\main\\java\\com\\baeldung\\java9\\process\\OutputStreamExample.java");
process = Runtime.getRuntime()
.exec("java -cp src/main/java com.baeldung.java9.process.OutputStreamExample");
BufferedReader output = new BufferedReader(new InputStreamReader(process.getInputStream()));
int value = Integer.parseInt(output.readLine());
return value;
}
public static String getErrorStreamExample() throws IOException {
Process process = Runtime.getRuntime()
.exec("javac -cp src src\\main\\java\\com\\baeldung\\java9\\process\\ProcessCompilationError.java");
BufferedReader error = new BufferedReader(new InputStreamReader(process.getErrorStream()));
String errorString = error.readLine();
return errorString;
}
public static void creatingNewProcess() throws IOException {
ProcessBuilder builder = new ProcessBuilder("notepad.exe");
Process process = builder.start();
}
public static int filterProcessWithStreamsInSpecificRangeReturnCount() {
return (int) ProcessHandle.allProcesses()
.filter(ph -> (ph.pid() > 10000 && ph.pid() < 50000))
.count();
}
public static void destroyingProcessCreatedBySameProcess() throws IOException, InterruptedException {
ProcessBuilder builder = new ProcessBuilder("notepad.exe");
Process process = builder.start();
Thread.sleep(10000);
process.destroy();
}
public static void destroyingProcessCreatedByDifferentProcess() {
// find out the process id of current running task by checking
// task manager in windows and enter the integer value
Optional<ProcessHandle> optionalProcessHandle = ProcessHandle.of(5232);
ProcessHandle processHandle = optionalProcessHandle.get();
processHandle.destroy();
}
public static int waitForExample() throws IOException, InterruptedException {
ProcessBuilder builder = new ProcessBuilder("notepad.exe");
Process process = builder.start();
return process.waitFor();
}
public static int exitValueExample() throws IOException {
ProcessBuilder builder = new ProcessBuilder("notepad.exe");
Process process = builder.start();
process.destroy();
return process.exitValue();
}
public static void destroyExample() throws IOException, InterruptedException {
ProcessBuilder builder = new ProcessBuilder("notepad.exe");
Process process = builder.start();
Thread.sleep(10000);
process.destroy();
}
public static void destroyForciblyExample() throws IOException, InterruptedException {
ProcessBuilder builder = new ProcessBuilder("notepad.exe");
Process process = builder.start();
Thread.sleep(10000);
process.destroy();
if (process.isAlive()) {
process.destroyForcibly();
}
}
public static void outputStreamDemo() throws IOException, InterruptedException {
Logger log = Logger.getLogger(ProcessUnderstanding.class.getName());
Process pr = Runtime.getRuntime()
.exec("javac -cp src src\\main\\java\\com\\baeldung\\java9\\process\\ChildProcess.java");
final Process process = Runtime.getRuntime()
.exec("java -cp src/main/java com.baeldung.java9.process.ChildProcess");
try (Writer w = new OutputStreamWriter(process.getOutputStream(), "UTF-8")) {
w.write("send to child\n");
}
new Thread(() -> {
try {
int c;
while ((c = process.getInputStream()
.read()) != -1)
System.out.write((byte) c);
} catch (Exception e) {
e.printStackTrace();
}
}).start();
// send to child
log.log(Level.INFO, "rc=" + process.waitFor());
}
}
@@ -1,43 +0,0 @@
package com.baeldung.java9.process;
import java.io.File;
import java.io.IOException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.time.Duration;
import java.time.Instant;
import java.util.stream.Stream;
public class ProcessUtils {
public static String getClassPath() {
String cp = System.getProperty("java.class.path");
System.out.println("ClassPath is " + cp);
return cp;
}
public static File getJavaCmd() throws IOException {
String javaHome = System.getProperty("java.home");
File javaCmd;
if (System.getProperty("os.name").startsWith("Win")) {
javaCmd = new File(javaHome, "bin/java.exe");
} else {
javaCmd = new File(javaHome, "bin/java");
}
if (javaCmd.canExecute()) {
return javaCmd;
} else {
throw new UnsupportedOperationException(javaCmd.getCanonicalPath() + " is not executable");
}
}
public static String getMainClass() {
return System.getProperty("sun.java.command");
}
public static String getSystemProperties() {
StringBuilder sb = new StringBuilder();
System.getProperties().forEach((s1, s2) -> sb.append(s1 + " - " + s2));
return sb.toString();
}
}
@@ -1,21 +0,0 @@
package com.baeldung.java9.process;
import java.util.Optional;
public class ServiceMain {
public static void main(String[] args) throws InterruptedException {
ProcessHandle thisProcess = ProcessHandle.current();
long pid = thisProcess.pid();
Optional<String[]> opArgs = Optional.ofNullable(args);
String procName = opArgs.map(str -> str.length > 0 ? str[0] : null).orElse(System.getProperty("sun.java.command"));
for (int i = 0; i < 10000; i++) {
System.out.println("Process " + procName + " with ID " + pid + " is running!");
Thread.sleep(10000);
}
}
}