File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes export commands before exec() in linux Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "export commands before exec() in linux" Watch "export commands before exec() in linux" New topic
Author

export commands before exec() in linux

Stephen Huey
Ranch Hand

Joined: Jul 15, 2003
Posts: 618
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

Joined: Aug 24, 2003
Posts: 115
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

Joined: Jul 15, 2003
Posts: 618
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

Joined: Aug 02, 2004
Posts: 823
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

Joined: Jun 02, 2003
Posts: 1923

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.


http://home.arcor.de/hirnstrom/bewerbung
Stephen Huey
Ranch Hand

Joined: Jul 15, 2003
Posts: 618

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).
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: export commands before exec() in linux
 
Similar Threads
Calling Linux shell in JSP
How to export the Env Variables in java
Linux+ Certification Question
XWindows error
Can i Run Dos Command?