We help IT Professionals succeed at work.

unable to set environment variable through batch file or through RunTime.exec

bjr1
bjr1 asked
on
7,373 Views
Last Modified: 2013-12-29
Cannot set environment variable PATH and TEST by passing the value through .bat file or through Runtime.exec(command, String[] env). I have java 1.4

I have :
String PATH = "\""+ "C:\\Program Files\\foo\"";
String TEST="\""+ "C:\\Program Files\\foo\"";
In my data.bat, I have
set PATH=%PRODUCT_HOME%\foo;%PATH%
set TEST=%PRODUCT_HOME%\foo

First I tried just passing RunTime.exec("c:\data.bat");
I got JNI exception.
 so I tried  String [] env = new String [] {PATH,TEST}
passing Runtime.exec("C:\data.bat",env) but I am still unable to set the environment that I want.
I cannot figure out the problem.
Also how do I see if the environment is set or not?
thanks
bjr
               
Comment
Watch Question

Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
try:

Runtime.exec("cmd /c c:\data.bat");

and specify the path in the bat file
Java Developer
CERTIFIED EXPERT
Top Expert 2010
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
actually should be

Runtime.exec("cmd /c c:\\data.bat");
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
CERTIFIED EXPERT
Top Expert 2016
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:
Thanks objects and anokun7 I tried all the options but now when I give:
RunTime.exec(command, env, dir) where  i set dir   is same as TEST environment variable, i could get it work but after trying some changes in the application and then running the app, I got no JNI in java.library.path. I set dir to TEST thinking that current working directory should be same location as the dlls.Please correct me if I am wrong.

Now I cannot run any  app successfuly.
My command has :"cmd \c C;\data.bat -cp "c:\pg files\foo" - result "c:\pg files\resultsFolder" .
CEHJ, I dont know how to write to the registry in my code and also I have to use the PATH environment variable to set the dlls location otherwise I see exceptions.

CERTIFIED EXPERT
Top Expert 2016

Commented:
>>My command has :"cmd \c C;\data.bat -cp "c:\pg files\foo" - result "c:\pg files\resultsFolder" .

That's full of errors. Is that the actual command?


cmd /c C:\data.bat -cp "c:\pg files\foo" -result "c:\pg files\resultsFolder"

would be more like it

Commented:
My suggestion is to put your command into a single batch file - do whatever you want in the batch file - like writing to the registry etc...

All you java program has to do is to call the batch file like in my example.

So for this to work code your batch file first and test it alone without bringing java or anything else into the equation.. then all you need to do is to use something like my example to call the batch file and you are done.

Author

Commented:
thanks, actually the command I wrote was not with proper quotes, sorry I posted that just for example purposes. I will try to put everything in one batch file and try to run and see what happens.
thanks

Author

Commented:
Sorry took this long to figure out what was going on in my code. Finally I relaized that  In windows: runtime.exec(command, env,"current working dir") was still not working because the System root :C:\Windows or C:\WINNT was not to be found when I send just PATH and TEST env variables to runtime.exec. so when I added String env = {PATH, TEST,sysroot) everything works fine. Now my question is I cannot hard code the SystemRoot value in my code. I need to know how to pass this SystemRoot value . In System.getproperty(), I dont see any property that sets system root. Any ideas on how to pass this value without hardcoding in my code.
thanks
bjr1
CERTIFIED EXPERT
Top Expert 2016

Commented:
Try

String sysRoot = System.getenv("WINDIR") ;

Author

Commented:
Hi CEHJ, I am using java 1.4 , is there a way to get SYSTEMROOT in java 1.4
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
why do u need to pass it, and instead just access it from environment in your batch file.

Author

Commented:
I cannot modify the batch file as that is comming from an other application.So the only way is to change in my java code that supports only java 1.4.
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
use another batch that writes the value to stdout, read the output of that and pass it to the batch.

Or use a 2nd batch that calls the batch from the other application.

Author

Commented:
thanks for helping. finally I could get my pg to run without complaints. RunTime.exec does behave very mysterously.
thanks genius for helping me to pass the environment variables in RunTime.exec and also to write another batch file
thanks anokun7: to help me debug my pg
and CEHJ for helping me.

Commented:
You are welcome! Thanks for the points.
CERTIFIED EXPERT
Top Expert 2016

Commented:
:-)
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.