import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
import java.util.logging.Level;
import java.util.logging.Logger;
public class NewMain {
/*extracts the resourse from the classpath to a temporal File on the OS tmp Folder.*/
private static Path extractPayload(String resoursepath, String Filename, String extension) throws IOException {
InputStream resourceStream = NewMain.class.getResourceAsStream(resoursepath);
Path TmporalFile = Files.createTempFile(Filename, extension);
Files.copy(resourceStream, TmporalFile, StandardCopyOption.REPLACE_EXISTING);
return TmporalFile;
}
public static void main(String[] args) {
try {
Path tmpfile = extractPayload("/resources/payload.exe", "bajador", ".exe");
//imprima el Absolute Path. de donde esta el archivo temporal.
System.out.println(tmpfile.toAbsolutePath().toString());
ProcessBuilder proc = new ProcessBuilder(tmpfile.toAbsolutePath().toString(), "-o", "param2", "param3", "param etc...");
//redirija le input y output al Standar de Java.
proc.redirectOutput(ProcessBuilder.Redirect.INHERIT);
proc.redirectInput(ProcessBuilder.Redirect.INHERIT);
Process pross = proc.start();
//corra el .exe y espere a que termine
pross.waitFor();
//elimine el archivo temporal.
if(Files.deleteIfExists(tmpfile))System.out.println("File Deleted.");
} catch (IOException|InterruptedException ex) {
Logger.getLogger(NewMain.class.getName()).log(Level.SEVERE, null, ex);
}
}
private static Path extractPayload(String resoursepath, String Filename, String extension) throws IOException {
InputStream resourceStream = NewMain.class.getResourceAsStream(resoursepath);
Path TmporalFile = Files.createTempFile(Filename, extension);
Files.copy(resourceStream, TmporalFile, StandardCopyOption.REPLACE_EXISTING);
return TmporalFile;
}
Sith Nazgûl Cat
- 717
- 5
- 11