0

I have a java program that if you press a button, a python script with an argmument is executed. What I thought is: executing in the terminal pytho initConfig.py arg. This is my code:

  String[] cmd = {"cd "+ System.getProperty("user.dir")+"/src/main/resources/", "python initConfig.py", getUser().getId()};
    Process pb;
    try {
        pb = Runtime.getRuntime().exec(cmd);
        String line;
        BufferedReader input = new BufferedReader(new InputStreamReader(pb.getInputStream()));
        while ((line = input.readLine()) != null) {
            System.out.println(line);
        }
        input.close();
    } catch (IOException e) {
        e.printStackTrace();
    }

And the error I´m getting is ´File not found´.

This is my folder structure:

  project
     src
        main
           java
               com
                 controller
                     FileWhereTheButtonIs.java
           resources
             initConfig.py
daniel gon
  • 159
  • 2
  • 14
  • [try this](https://stackoverflow.com/questions/8405723/how-to-set-working-directory-with-processbuilder) – ZhenyaM Jan 15 '19 at 20:20
  • You say, your file is in a (parent) folder called "project"... but you try to open the file from your %user_dir%/src/main/resources` ...and if your user name/home is not "project", then this might be already it. – xerx593 Jan 15 '19 at 20:28
  • or if you want to stay with `Runtime` [one of](https://docs.oracle.com/javase/8/docs/api/java/lang/Runtime.html#exec-java.lang.String-java.lang.String:A-java.io.File-) the [overloads](https://docs.oracle.com/javase/8/docs/api/java/lang/Runtime.html#exec-java.lang.String:A-java.lang.String:A-java.io.File-) that takes an argument for the working directory. Because `Runtime` (and `ProcessBuilder`) is not a shell and `cd` only works in a shell, not as a program. – dave_thompson_085 Jan 16 '19 at 01:21

0 Answers0