#Input Stream Limit
12 messages · Page 1 of 1 (latest)
Hey, @scarlet meteor!
Please remember to /close this post once your question has been answered!
The purpose of this code is to open an instance of powershell in the backend and return its value once the command is finished executing. The code below works with powershell commands that have a small output but it seems the larger the output the less likely it is to work
package Future;
import java.util.ArrayList;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
public class Main {
public static void main(String[] args) {
Powershell powershell = new Powershell();
Future<ArrayList<String>> fut = powershell.execute("Get-AppxPackage –AllUsers");
try {
fut.get().forEach(System.out::println);
} catch (InterruptedException | ExecutionException e) {
throw new RuntimeException(e);
}
powershell.close();
}
}```
package Future;
import java.io.IOException;
import java.util.ArrayList;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
public class Powershell {
private final ExecutorService executorService = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors() * 2);
public Future<ArrayList<String>> execute(String command) {
String[] commandList = new String[]{"powershell.exe", "-Command", command};
ProcessBuilder pb = new ProcessBuilder(commandList);
try {
Process p = pb.start();
return this.executorService.submit(new Reader(p.getInputStream()));
} catch (IOException ex) {
throw new RuntimeException("Cannot execute PowerShell.exe", ex);
}
}
public void close() {
this.executorService.shutdown();
}
}```
package Future;
import java.io.*;
import java.util.ArrayList;
import java.util.concurrent.Callable;
import java.util.logging.Logger;
public class Reader implements Callable<ArrayList<String>> {
private String message;
private BufferedReader reader;
public Reader(InputStream inputStream) {
this.reader = new BufferedReader(new InputStreamReader(inputStream));
this.message = ( null != message ) ? message : "";
}
/**
* The function is called when the thread is started. It reads the output of the process and prints it to the terminal
*
* @return An ArrayList of Strings
*/
@Override
public ArrayList<String> call() throws Exception {
String line;
ArrayList<String> test = new ArrayList<>();
try {
while (null != (line = this.reader.readLine())) {
test.add(line);
}
} catch (IOException e) {
System.err.println("ERROR: " + e.getMessage());
}
return test;
}
}```
seems like no input is sent in large commands
here is an example of a large powershell script https://github.com/GageAllenCarpenter/Powershell/blob/main/List of Installed Applications
@scarlet meteor have you tried getting error stream? maybe its outputting error
Dana from this server helped me with it. It works now! It was going into the error stream. The solution was to read from a file