1

I am trying to run .py script in Java, but when I run the java code it doesn't show any output. What I am doing wrong? I tried with:

ArrayList<String> command = new ArrayList<String>();
    //xterm will be launched, if platform is Linux.
    command.add("xterm");
    command.add("-c");
    command.add("python");
    command.add("/home/clef/Escritorio/use_archive.py");
    command.add("/home/clef/classification/STOP_WORDS.tar.gz");
    command.add("/home/clef/Escritorio/Prueba_linea/000006/6.jpg");
    command.add(" > ~/Escritorio/mike.txt");
    //command.add("--revert");    // switch to revert the patch

    ProcessBuilder pb = new ProcessBuilder(command);

    Process p = null;

    if (pb != null) {

        try {
            p = pb.start();
        } catch (IOException e) {
            e.printStackTrace();
        }
        if (p != null) {
            try {
                p.waitFor();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
    BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));

    String line = "";
    ArrayList<String> allOut = new ArrayList<>();
    System.out.println("salida");
    try
    {
        System.out.println("SALIDA-----------:");
        while ((line = reader.readLine())!= null)
        {
            System.out.println(line);
            allOut.add(line);
        }
    } catch (IOException ex)
    {
        //allOut = "0";
        //Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        //System.out.println("erro3");        }
        System.out.println("error");
    }

The script is from NVIDIA DIGITS framewrok, but I can't run any python script from Java. If I run the .py script in terminal I get output:

Unknown file: solver.prototxt
    Unknown file: train_val.prototxt
    Processed 1/1 images in 0.076470 seconds ...
    -------- Prediction for /home/clef/Escritorio/Prueba_linea/000006/6.jpg --------
     51.4607% - "0"
      7.7899% - "1"
      5.7782% - "2"
      5.6086% - "3"
      5.2513% - "4"

    Script took 0.523756 seconds.
5
  • Why are you using xterm? Commented Apr 16, 2016 at 5:28
  • I also tried with /bin/bash but did not work well Commented Apr 16, 2016 at 6:00
  • You don't need to use either of those, python will run by itself... Commented Apr 16, 2016 at 6:07
  • I think so, but script doesn't work if I run it from java, I try commands like 'ls' 'cd ..' but I can't run python scripts from java. Commented Apr 16, 2016 at 6:12
  • What do you mean it 'doesn't work' Commented Apr 16, 2016 at 6:17

1 Answer 1

1

Try this:

Process p = Runtime.getRuntime().exec("python yourapp.py");

Exec runs system commands, if you have python installed, this will run the python file like you would in the command terminal of your operating system.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.