What is the best way to run batch with java? [closed]

1

Hello, I would like to know what is the best way to run batch files with java, the ways in which I think it can be done is doing everything from java. For example, I have to execute certain commands via ssh I think I could connect from java and execute the commands using the objects that allow me to do this ... another way I think that is to execute from java the pure batch file that contains the ssh connection and the commands ... and another way that I have listened is using a framework like spring but I really have not used it ... do you recommend me? greetings

    
asked by Erikzon Zarco 08.04.2016 в 18:26
source

3 answers

5
  

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.

    
answered by 08.04.2016 в 19:56
1

You can do it using the class Runtime and using the exec () :

Runtime.getRuntime().exec("cmd /c start C:\path\miarchivo.bat");
    
answered by 08.04.2016 в 19:04
0

I do not know if this serves you. I raise a java.jar from shell: archivo.sh

#!/usr/local/bin/bash
logeo=/ruta/del/log/nombre_archivo_'date +%Y%m%d_%H%M'.log
jar=/ruta/del/jar/Archivo.jar
/opt/java1.5/bin/java -classpath $jar com.paquete.EjecutaClaseConMain > $logeo
gzip $logeo
mv  $logeo.gz /ruta/del/log/comprimido

That I raise with crontab registering the full path to file.sh and when crontab raises the sh, the jar is executed. The jar has a while that does not die until a certain time of the night. When the crontab raises the sh it also generates a log and writes to it with system.out.println while the jar is alive. This log can be seen with tail -f file.sh . At the end of the day when the jar dies due to the time, the log is compressed and stored in a folder. The next day the crontab returns to do the same cycle.

I think this is the opposite of what you want to do, but I feel that it can help you.

    
answered by 08.04.2016 в 22:00