• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How do i set environment variable?

 
Ranch Hand
Posts: 692
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 692
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 692
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 692
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 :)
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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:
 
naved momin
Ranch Hand
Posts: 692
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic