Results 1 to 5 of 5
  1. #1
    Join Date
    Feb 2010
    Posts
    11
    Rep Power
    0

    Default running external program in directory different from working dir

    Hi,

    My java code calls an external program. This program produces a file that is created in the working directory where my java code was called from.
    I call the external program like this:

    Runtime r = Runtime.getRuntime();
    Process p = r.exec("C:/my_program.exe");
    p.destroy();


    This is what I want: my external program needs to be executed in a subdirectory of my current working directory.

    Apparently, if I add the following before I call the external program

    System.setProperty(user.dir,"C:/subdir/);

    it has no effect on the location of the file created by the external program.

    Any suggestions, ideas?

    thanks in advance,

    Nick

  2. #2
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,842
    Rep Power
    19

    Default

    Use java to move the file once it's been created?

  3. #3
    Join Date
    Feb 2010
    Posts
    11
    Rep Power
    0

    Default

    That's not an option I think. Here is why:

    I am using multi-threading to start multiple external programs at once. Since they all produce files with the same filename, they need to be executed in different directories. otherwise I would have files with equal filenames.

    Note that I have no possibility to set the filename as an argument of the external program.

  4. #4
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,842
    Rep Power
    19

    Default

    Ah, OK.
    A quick peek at the API and I see that exec() has a version that'll take a working directory.

  5. #5
    Join Date
    Feb 2010
    Posts
    11
    Rep Power
    0

    Default

    Tolls,

    your idea solved my problem. Specifying the working directory inside the runtime.exec() method was indeed the thing to do. It 's a shame i didn't notice this in the API before.

    Thanks for the suggestion!

    nick

Similar Threads

  1. Problem with running an external program via java
    By g123456 in forum New To Java
    Replies: 4
    Last Post: 12-30-2009, 05:06 PM
  2. working directory in web application
    By Juuno in forum Advanced Java
    Replies: 1
    Last Post: 05-04-2009, 07:45 PM
  3. Static function to get current working directory
    By Java Tip in forum Java Tip
    Replies: 1
    Last Post: 06-23-2008, 02:13 AM
  4. Replies: 0
    Last Post: 10-04-2007, 09:33 PM
  5. How to get the current working directory in EJB?
    By sathish_2111 in forum Enterprise JavaBeans (EJB)
    Replies: 1
    Last Post: 07-19-2007, 04:24 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •