OK after few tries, I would like to rephrase my question :
"I have developed Web App with Angular 5 (frontend), spring-boot (backend) AND Java 8
The next step is to launch partner software, installed on remote server, from the interface. It's an .exe program with some parameters, But I wish to test by just launching putty
My java class (by using @Ankesh answer)
@Service
public class DictService extends CoreServices {
public ApiResponse<?> launch(String idWS, String ipp, String nom, String prenom, String ddn) {
try {
boolean isWindows = System.getProperty("os.name")
.toLowerCase().startsWith("windows");
String path = "C:\\klinck\\PuTTY\\putty.exe";
ProcessBuilder builder = new ProcessBuilder();
if (isWindows) {
// builder.command("cmd.exe", "/c", "dir");
builder.command("cmd.exe", "/c", path);
} else {
// this is for bash on linux (can be omitted)
builder.command("sh", "-c", "ls");
}
System.out.println(builder);
// builder.directory(new File(System.getProperty("user.home")));
// Start the process here
// Redirect the errorstream
builder.redirectErrorStream(true);
System.out.println(""+builder.redirectErrorStream());
System.out.println("before start");
Process process = builder.start();
System.out.println("after start");
// Follow the process to get logging if required
StreamGobbler streamGobbler =
new StreamGobbler (process.getInputStream(), System.out::println);
//Submit log collection to and executor for proper scheduling and collection of loggs
System.out.println("before executors submit");
Executors.newSingleThreadExecutor().submit(streamGobbler);
// Collect exit code
System.out.println("before waitFor");
int exitCode = process.waitFor();
System.out.println("after waitFor");
// validate if the appliction exited without errors using exit code
assert exitCode == 0;
} catch (Exception ure) {
return new ApiResponse<>(false, "Une erreur interne est survenue. Merci de contacter le support", null, 0);
}
return new ApiResponse<>(true, null, null, 0);
}
private static class StreamGobbler implements Runnable {
private InputStream inputStream;
private Consumer<String> consumer;
public StreamGobbler(InputStream inputStream, Consumer<String> consumer) {
this.inputStream = inputStream;
this.consumer = consumer;
}
@Override
public void run() {
System.out.println("StreamGlobber run");
new BufferedReader(new InputStreamReader(inputStream)).lines()
.forEach(consumer);
}
}
}
A process in launched but the GUI doen't appear.
I noticed that this process locks tomcat log files (stderr and stdout)
I saw that it's possible : Best Way to Launch External Process from Java Web-Service? Executing external Java program from a webapp
But I don't succeed to adapt this code.
Here is log :
java.lang.ProcessBuilder@7b1ead05
true
before start
after start
before executors submit
before waitFor
StreamGlobber run
It seems like process is launched.
I don't understand....How can I solve that ?
You can use your SpringBoot
backend to achieve this.
In java ProcessBuilder
class can help you launch a command (which will run your executable
). Here we are running cmd.exe
which is available on system path.
ProcessBuilder builder = new ProcessBuilder();
if (isWindows) {
builder.command("cmd.exe", "/c", "dir");
} else {
// this is for bash on linux (can be omitted)
builder.command("sh", "-c", "ls");
}
builder.directory(new File(System.getProperty("user.home")));
// Start the process here
Process process = builder.start();
// Follow the process to get logging if required
StreamGobbler streamGobbler =
new StreamGobbler(process.getInputStream(), System.out::println);
//Submit log collection to and executor for proper scheduling and collection of loggs
Executors.newSingleThreadExecutor().submit(streamGobbler);
// Collect exit code
int exitCode = process.waitFor();
// validate if the appliction exited without errors using exit code
assert exitCode == 0;
Reference : https://www.baeldung.com/run-shell-command-in-java