0

I have an external program that I'm running. for some reason, the code owner didn't give me the code or and good documentation, I know how to run this code but it was written originaly to be executed from command line and not from JAVA. the effect on me is that this application uses an ENV variable and relay on its value (a path on the computer for the output). I want to change that value, how can it be done without running it from a batch file?

Willi Mentzel
  • 27,862
  • 20
  • 113
  • 121
Hovav
  • 151
  • 2
  • 13
  • 1
    How do you really run that program? From command line or from some java wrapper code? Question's title and body are currently mutually exclusive on this point. – Vadzim Jun 07 '12 at 12:42
  • I'm calling it from another java application using regular java method call. – Hovav Jun 07 '12 at 12:53

4 Answers4

1

I assume you are executing this program using one of the Runtime.exec() methods in Java code to create a Process.

Note that some of those methods allows you to pass environment variables to the process you are creating, for example exec(String[] cmd, String[] envp).

Alternatively, the Map returned by ProcessBuilder.environment() can be manipulated for the same effect.

ewan.chalmers
  • 16,145
  • 43
  • 60
0

In your command prompt first set the required variable

set FILELOCATION=<PATH TO FILE>

java MyProgram

In this case the FILELOCATION will be available till you close the program.

Not setting variable will be dependent on OS.

For Linux or Solaris you can do :

export FILELOCATION=<PATH TO FILE>

In case you are looking for command line parameters then you can use something like this:

java MyProgram PathToFile

There is a better way of doing this java -DFILELOCATION=<PATH_TO_FILE> MyProgram

Edit: As per comment.

Just use ProcessBuilder to set ENV variable in Java code.

mprabhat
  • 20,107
  • 7
  • 46
  • 63
  • hi first of all, 10x for the fast responce. second: my program does not run from console, I'm invoking a static method in an object exist in the JAR file I got and inside this static method it relays on the env variable. my program also being called by another java program and I dont want to relay on external variable set - I want to ensure in my java code that the variable exist, if its not - add it, and set its value to my value, all using java code and without calling to outside tools if possible... – Hovav Jun 07 '12 at 12:18
0

how can it be done without running it from a batch file

Just set global environment variable. All new processes will see it (excluding those inheriting environment from old parent process).

See also How do I set environment variables from Java?. This answers the question's title. Which doesn't match the question's body, btw. ;)

Community
  • 1
  • 1
Vadzim
  • 24,954
  • 11
  • 143
  • 151
0

See this post. It usually helps to first start a search here before posting a question. If you already tried that solution, it really helps the Helpers to let them know that you tried it and what went wrong.

Community
  • 1
  • 1
brimborium
  • 9,362
  • 9
  • 48
  • 76