• Post Reply Bookmark Topic Watch Topic
  • New Topic

export commands before exec() in linux  RSS feed

 
Stephen Huey
Ranch Hand
Posts: 618
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey there, I've used exec() a lot, but for this particular program that I need to shell out from my Java program, I need to call export with a few temporary environment variables on Red Hat Linux 8. What's the best way to do this? Can I call something like exec("export MYVAR=value") for each one before exec'ing the program (will those export commands be associated with the "same shell" as the final exec that calls the program), or should I just create a shell script on the fly that contains all those lines?
 
Tom Hill
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can set your environment methods before you start the process - check the API for that -
I cheat and start the exec with some usless command - then use the OutputStream to feed in the line:
export MYVAR=value \n
in a buffered reader
Once the environment is started you can feed strings in to do processes - its not fool proof, but its much easier than anything else ive seen
 
Stephen Huey
Ranch Hand
Posts: 618
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When I tried using the OutputStream I ran into an IOException reporting a Broken Pipe, so I'm trying your other suggestion of using exec(String command, String [] env), so it looks like this:



However, that's not setting up my environment, so what am I doing wrong? I'm trying to find an example of what that environment variable array should look like. Thanks...

 
Julian Kennedy
Ranch Hand
Posts: 823
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Stephen,

The Javadoc for this method (never a bad place to look ) states that the environment parameter should be a String[] of name=value pairs, so I'm guessing you don't need the export. That syntax is only valid in the korn shell anyway (well mainly?) and you're doing nothing to ensure you get a ksh.

I can't promise it'll work but there be dragons in Runtime.getRuntime().exec()...

Jules
 
Stefan Wagner
Ranch Hand
Posts: 1923
Linux Postgres Database Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Perhaps this would work:

exec("/usr/mydir/myprogram", new String { "export VAR1=val1;export VAR2=val2"});

but I would suggest the API.docs as Tom Hill did too.
 
Stephen Huey
Ranch Hand
Posts: 618
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

The Javadoc for this method (never a bad place to look ) states that the environment parameter should be a String[] of name=value pairs, so I'm


I read the docs, but I only read the method description, not down below where it explicitly tells me the format of envp!!! :roll:

Anyway, I haven't tried it that way yet (it would be bash, I guess, since it's Red Hat). I finally resorted to reading in a template shell script file, and replacing a keyword with the file path every time I needed to run the script (and writing it out to a new script file that I then called).
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!