I would like to know what is the best way to run batch files with java
You have two ways of doing it. One is using Runtime
:
Runtime.getRuntime().exec("comando", new String[]{ "argumento" });
The other is using ProcessBuilder
:
Process p = new ProcessBuilder("comando", "argumento").start();
"comando"
is the name of your file (.bat, .sh) that you are going to execute.
The difference is that the first one is a wrapper method to create an instance of ProcessBuilder
and execute the process. If you want to add configuration as in what folder is the process to be executed, send the output of the process to a log file or to a console, among others, use the second form. If you do not need to handle any of that, use the first one.
Another way I think that is to execute from java the pure batch file that contains the connection ssh and the commands
This really depends on order rather than on Java itself. If your ssh connection configuration can vary a lot, then you may want to keep that configuration in your process file (.bat, .sh). Otherwise, if that configuration is static and can not / should be altered with ease, then you may want to "protect" it in the Java code. There is no concrete answer for this.
Another way I've heard is using a framework like spring but I really have not used it
Spring Batch performs batch data processing work. For example, you have a table with a thousand records, you need that each record is processed in batch to produce certain information, that the execution is in parallel, add tolerance to failures, etc., then for this scenario you use Spring Batch. If you want to run a .bat or .sh file, Spring Batch is probably not what you're looking for.