aspose file tools*
The moose likes Java in General and the fly likes How do i set environment variable? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "How do i set environment variable?" Watch "How do i set environment variable?" New topic
Author

How do i set environment variable?

naved momin
Ranch Hand

Joined: Jul 03, 2011
Posts: 692

How do i set environment variable under windows via Java program
I need to set the value for the path key ,I tried this
But it throws an


The Only way to learn is ...........do!
Visit my blog http://inaved-momin.blogspot.com/
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18651
    
    8

I'm pretty sure you can't set environment variables inside a process in any language at all. But as far as I can see there isn't any point in setting the PATH environment variable in your Java code, since the process where your application is running is never going to use it anyway. Or is it?
naved momin
Ranch Hand

Joined: Jul 03, 2011
Posts: 692

Paul Clapham wrote:I'm pretty sure you can't set environment variables inside a process in any language at all. But as far as I can see there isn't any point in setting the PATH environment variable in your Java code, since the process where your application is running is never going to use it anyway. Or is it?

But i have seen installer's setting environment variables ,how they do it ? and i have specific requirement where i need to set environment variable , are you dead sure no one can set environment variables programaticaly , because i have seen installer setting the environment varibles ...
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18651
    
    8

Those installers will be setting the system-level environment variables, not the copies of the variables which are provided to the process. I assume that to change the former, you would have to change some registry entries or call a low-level API or something like that. The latter can't be changed by the process itself.

And as I said, there's no need for your Java class to change its PATH environment variable because it can't possibly use it anyway. It might possibly create a new process which needs a modified version of the PATH variable, but the Process class allows you to do that in a straightforward way in any case. If your specific requirement was to change some other environment variable, then it wasn't very helpful to use PATH as your example, I don't think. So perhaps you could explain your actual requirement and why you think that changing an environment variable is the way to implement that requirement?
naved momin
Ranch Hand

Joined: Jul 03, 2011
Posts: 692

Paul Clapham wrote:Those installers will be setting the system-level environment variables, not the copies of the variables which are provided to the process. I assume that to change the former, you would have to change some registry entries or call a low-level API or something like that. The latter can't be changed by the process itself.

And as I said, there's no need for your Java class to change its PATH environment variable because it can't possibly use it anyway. It might possibly create a new process which needs a modified version of the PATH variable, but the Process class allows you to do that in a straightforward way in any case. If your specific requirement was to change some other environment variable, then it wasn't very helpful to use PATH as your example, I don't think. So perhaps you could explain your actual requirement and why you think that changing an environment variable is the way to implement that requirement?

My project want to exec certain ffmpeg commands , so i need to set the evironment variable for ffmpeg and ANT_HOME for ant , which is a best way to do that ?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18651
    
    8

That depends on how you're going to be running ANT and ffmpeg. My suggestion would be to set the environment variables as the first steps in the batch script which does those things.
naved momin
Ranch Hand

Joined: Jul 03, 2011
Posts: 692

Paul Clapham wrote:That depends on how you're going to be running ANT and ffmpeg. My suggestion would be to set the environment variables as the first steps in the batch script which does those things.

ohh! thanks I had just missed the batch thing ....thank you :)
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19718
    
  20

FYI, both ProcessBuilder and Runtime.exec allow you to pass additional environment variables. The problem with Runtime.exec is that you need to provide either null or all of the existing environment variables as well. If you only provide the new ones, most of the others will be dropped. ProcessBuilder is better for that. For example:


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
naved momin
Ranch Hand

Joined: Jul 03, 2011
Posts: 692

Rob Spoor wrote:FYI, both ProcessBuilder and Runtime.exec allow you to pass additional environment variables. The problem with Runtime.exec is that you need to provide either null or all of the existing environment variables as well. If you only provide the new ones, most of the others will be dropped. ProcessBuilder is better for that. For example:

but if i do this then any time i invoke ant or ffmpeg commands i have to rewrite this steps or it will set the environment variable till my java process killed ?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18651
    
    8

naved momin wrote:but if i do this then any time i invoke ant or ffmpeg commands i have to rewrite this steps or it will set the environment variable till my java process killed ?


No. As I already said, you cannot change the environment variables for your process. You can certainly create a second process which has different variables, and as we have already established, that's what you should do, but that won't have any effect on the original process. And in case the environment variables you were referring to were the system-wide ones which are by default assigned to every new process, and you thought you were changing those, let me repeat that again... you can't change those either.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How do i set environment variable?