0

I'm trying run a Python file with argument in Java code. When I run the file in Mac Os terminal, it works perfectly, but when I tried in Java code, it doesn't work.

In terminal I type this:

python import.py 10477 > out.txt

That command generates a out.txt correctly, but it doesn't work in my Java code. I tried with this:

String path = "/Users/Atlas/Desktop/Job/Python";
String textValue = textFixVersion.getText(); # Get value from a TextBox

if(releaseRadio.isSelected()) { # Radio Button

     String terminal = "python import.py " + textValue + " > out.txt";

     try {
           Process process = Runtime.getRuntime().exec(terminal,null,new File(path));
     }
     catch (IOException ex) {
           Logger.getLogger(Atlass.class.getName()).log(Level.SEVERE, null, ex);
     }
}

This code is the same when I execute Jar files within my Java code and it working perfectly. I'm working with Python 3.9.10.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
  • It doesn't work because `>` is functionality of your shell, and the way `runtime.exec` works does not involve your shell. You either need to execute without the redirect and read the default output of your process and write it to a file yourself, or you need to call exec in a way that invokes your shell to perform the redirect for you. – Mark Rotteveel Mar 02 '22 at 16:31
  • @MarkRotteveel you are right, I solved with this `exec(new String[]{"bash","-c",terminal},null,new File(path))`. Thank you for your support. – medinaracing Mar 03 '22 at 23:31

0 Answers0