Advertisement
piffy

Comandi shell in Java

Jul 5th, 2021
1,025
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.90 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.concurrent.Executors;
  3. import java.util.function.Consumer;
  4. //Richiede la classe Divoratore: https://pastebin.com/qW2p3Mv7
  5.  
  6. public class Main {
  7.  
  8.     public static void main(String args[]) throws IOException, InterruptedException {
  9.        
  10.         System.out.print("Lancio  di dir o ls nella home directory ");
  11.         boolean isWindows = System.getProperty("os.name").toLowerCase().startsWith("windows");         
  12.         ProcessBuilder builder = new ProcessBuilder();
  13.         if (isWindows) {
  14.             builder.command("cmd.exe", "/c", "dir");
  15.         } else {
  16.             builder.command("sh", "-c", "ls");
  17.         }
  18.         builder.directory(new File(System.getProperty("user.home")));
  19.         Process process = builder.start();
  20.         Divoratore d = new Divoratore(process.getInputStream(), System.out::println);
  21.         Executors.newSingleThreadExecutor().submit(d);
  22.         int exitCode = process.waitFor(); // 0 indica nessun errore.
  23.            
  24.        
  25.     }
  26.    
  27. }
  28.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement