Buenas, os quería hacer una pregunta, ¿sabeis como hacer para lanzar un comando que lance un programa python (python <direccion> o ejecute un script de bash (./script)?
Encontré que se podía hacer con esto pero no me funcionó:
String[] command = {"sh","-c",".python /home/angrymasther/Escritorio/some.py"};
Process p = Runtime.getRuntime().exec(command);
p.waitFor();
El programa funciona sin errores, pero no ejecuta el código python, que debería crear un directorio ejemplo.
Se que el código python no es porque lo e probado xD.
¿Alguna idea? Gracias de antemano.
Que raro debería de funcionar, quizás es porque no abriste un stream, usa un bloque try with resources es autocloseable linea 27 :xD
import os
from stat import *
print "HolA PoC"
package testing.foro.ExecPython;
import util.ShowData;
import javax.swing.*;
import javax.swing.filechooser.FileNameExtensionFilter;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.file.Files;
import java.nio.file.Path;
public class EjecutarPython {
public EjecutarPython() throws InterruptedException, IOException {
final String file = getPath().toAbsolutePath().toString();
//String[] command = {"sh","-c",".python", file};
String[] command = {"python", file };
Process p = Runtime.getRuntime().exec(command);
p.waitFor();
String line = "";
try(BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()))) {
while((line = br.readLine()) != null) {
ShowData.println(line);
}
}
p.destroy();
}
private Path getPath() {
JFileChooser chooser = new JFileChooser();
chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
//chooser.setFileFilter(new FileNameExtensionFilter(".py",new String[]{".py"})); please [color=red]FIXME[/color]
final int result = chooser.showOpenDialog(null);
if(result == JFileChooser.CANCEL_OPTION) {
System.exit(2);
}
return chooser.getSelectedFile().toPath();
}
public static void main(String ...agra) throws IOException, InterruptedException, ClassNotFoundException, UnsupportedLookAndFeelException, InstantiationException, IllegalAccessException {
UIManager.setLookAndFeel("javax.swing.plaf.nimbus.NimbusLookAndFeel");
new EjecutarPython();
}
}
Salida
HolA PoC
Process finished with exit code 0
Hola Rub'n, gracias por tu respuesta. Fue mala mía, funcionó cuando puse una ruta absoluta.
Cita de: aangrymasther en 5 Marzo 2018, 13:40 PM
Hola Rub'n, gracias por tu respuesta. Fue mala mía, funcionó cuando puse una ruta absoluta.
hommie, y esto que es? Absoluto o Relativo ? :o hasta donde se es Absoluto
/home/angrymasther/Escritorio/some.py