0

I'm using RedHat Linux bash command prompt. My requirement is to run a command using following code snippet where command could be anything which is allowd on command prompt eg: javac etc.

Process p = Runtime.getRuntime().exec(command);

Now i have set my commands bin directory in PATH variable by editing ~/.bash_profile file

export PATH=$PATH:<my commands bin directory>

it runs perfectly when i manually run the command by opening a new command prompt, but when i'm trying to run it using process command it fails as the PATH variable does not have my commands bin directory.

It seems when "Runtime.getRuntime().exec(command);" invokes a new bash shell it does not include or read ~/.bash_profile file. I have also verified that the user is same when running manually and using java code.

Could anyone point out whats issue here?

Vishal
  • 107
  • 1
  • 4
  • 12

1 Answers1

-1

Runtime.getRuntime().exec() doesn't search PATH by default. You will either need to find a different method that does this for you or implement it yourself by loading PATH, possibly parsing it, and then iterating through it to find the executable.

Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268
  • what do you mean by "loading PATH"? is it manually exporting PATH first? But the problem in this case is different, my commands bin directory could be different on different machine. – Vishal Sep 09 '16 at 01:01
  • By "loading path" I mean retrieving the value of the system variable. – Code-Apprentice Sep 09 '16 at 18:59