4 ms·
Everything Wrong With Java, the example: try { Process p = Runtime.getRuntime().exec( System.getenv("windir") + "\\system32\\ta
by carsongross 10y ago
Everything Wrong With Java, the example:
try {
Process p = Runtime.getRuntime().exec(
System.getenv("windir") + "\\system32\\tasklist.exe");
BufferedReader input = new BufferedReader(
new InputStreamReader(p.getInputStream()));
String line = null;
while ((line = input.readLine()) != null) {
System.out.println(line);
//If line describes a correct process,
//reformat the string to get your information out here.
}
input.close();
} catch (IOException ioe) {
ioe.printStackTrace();
}
This should read:
println( System.exec( "tasklist.exe" ) );
Yes, there should be a stream interface to the output, yes there should be a more complicated mechanism for working with processes in an object oriented manner, but c'mon guys: most of us just want to execute a command and screw with the string output.
- tdb7893 10y agoI don't think that the creation of processes ever got much attention because it really isn't in scope for what most people use java for. If you want to run some program that offloads most of the work to another process you would obviously use some scripting language that is way better for short scripts. You would probably only use java if you needed to do a lot of processing of the results, probably while the other process is still running so streams make sense for the vast majority of Java use cases.
- nradov 10y agoIt's easy enough to write your own library for that. I'm sure some people would prefer to have it built in to the standard library, but every time you add a higher level of abstraction in there it makes the standard library bigger and harder to maintain. So it's always a trade off.
- niftich 10y agoThat's also the old way, as the author says: "Now let's see a slightly more complex example, where one is getting a list of all processes running on the operating system. This is an old implementation with pure Java: [code]" Meanwhile, further down, the new implementation is: private static void listProcesses(){ ProcessHandle.allProcesses().forEach((h) -> printHandle(h)); } private static void printHandle(ProcessHandle procHandle) { // get info from handle ProcessHandle.Info procInfo = procHandle.info(); System.out.println("PID: " + procHandle.getPid()); System.out.print("Start Parameters: "); String[] empty = new String[]{"-"}; for (String arg : procInfo.arguments().orElse(empty)) { System.out.print(arg + " "); } System.out.println(); System.out.println("Path: " + procInfo.command().orElse("-")); System.out.println("Start: " + procInfo.startInstant(). orElse(Instant.now()).toString()); System.out.println("Runtime: " + procInfo.totalCpuDuration(). orElse(Duration.ofMillis(0)).toMillis() + "ms"); System.out.println("User: " + procInfo.user()); } I don't disagree that the traditional Java way of chaining InputStreams to BufferedReaders and whatnot makes for verbose code, but there are in fact many libraries like Guava and Apache Commons that'll give you one-liners for these. Much of the Java stdlib's philosophy was inspired by the single responsibility principle, rather than providing sane defaults for chains of common operations.
- ryenus 10y ago> println( System.exec( "tasklist.exe" ) ); Oh yeah, that would be pipe for Java, possible for a JSR?