0

I have two different python executable programs that reference either a picture or a .txt file from within their applications. When I open them as is they work perfectly. I have a master program that I use in Java to open the applications by clicking a button that executes:

    final String filePath = appList.get(1).get(3);//a string of the file directory
    try {
         Runtime.getRuntime().exec(filePath);
    } catch (IOException e) {
         System.err.println("Caught IOException: " + e.getMessage());
    }

When I launch the programs from this master program, it can no longer locate these files even though the directory remains the same. The only thing that seems to change is that I am launching the applications from a java program instead of just clicking on the application itself. Anyone know what to do?

EDIT: I am trying:

    String fileDir = appList.get(2).get(3).substring(0,appList.get(2).get(3).lastIndexOf("\\"))+"\\";
    Process p = null;
    ProcessBuilder pb = new ProcessBuilder(lastWord);
    pb.directory(new File(fileDir));
    p = pb.start();

But the system cannot find the file specified.

gsamaras
  • 71,951
  • 46
  • 188
  • 305
Cole
  • 349
  • 5
  • 16

1 Answers1

3

Use a ProcessBuilder for each of these apps you launch via your third java app. For each application, set the working directory appropriately (The root directory for the target application). I suspect the working directory for the two apps you launch from the third, are using the third programs working directory as their own. Heres an example of how to use the ProcessBuilder.

How to set working directory with ProcessBuilder

You can check out this post to find what the current working directory of the application is: Getting the Current Working Directory in Java.

Community
  • 1
  • 1
Mark W
  • 2,791
  • 1
  • 21
  • 44
  • 1
    @Cole, I still think its a working directory issue. I dont write in python, so I dont know what the typical deployment directory structure looks like. In Java, the working directory can vary depending on how the application was built and launched. If your python applications printed out the absolute path to the file they attempt to open, you could figure out pretty quickly what they expect the working directory to be. – Mark W May 16 '14 at 15:26
  • Yea, thanks for your help. I'll post if i figure it out. – Cole May 16 '14 at 15:28
  • @Cole, if you wrote the python apps, you could use this post: http://stackoverflow.com/questions/5137497/find-current-directory-and-files-directory to print out the working directory. Compare the output when you launch it via the java app vs by itself. Also, that substring bit sucks. You can use File.getParent() to return the parent directory. – Mark W May 16 '14 at 15:32
  • I'll look into it. And thanks for your honesty haha, i just started using java. – Cole May 16 '14 at 15:45