Test Foro de elhacker.net SMF 2.1

Programación => Programación General => Java => Mensaje iniciado por: aangrymasther en 4 Marzo 2018, 20:12 PM

Título: Ejecutar programa desde java.
Publicado por: aangrymasther en 4 Marzo 2018, 20:12 PM
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.
Título: Re: Ejecutar programa desde java.
Publicado por: rub'n en 4 Marzo 2018, 21:05 PM
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
Código (python) [Seleccionar]

import os
from stat import *

print "HolA PoC"


Código (java) [Seleccionar]
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

Código (java) [Seleccionar]

HolA PoC

Process finished with exit code 0

Título: Re: Ejecutar programa desde java.
Publicado por: 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.
Título: Re: Ejecutar programa desde java.
Publicado por: rub'n en 10 Marzo 2018, 02:51 AM
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

Código (bash) [Seleccionar]
/home/angrymasther/Escritorio/some.py