• Post Reply Bookmark Topic Watch Topic
  • New Topic

[SOLVED] Accessing Windows Environment Variables  RSS feed

 
Jose Kreif
Greenhorn
Posts: 22
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can java access the environmental variables?

Like:

%appdata%
%temp%
%USERPROFILE%

http://www.askvg.com/list-of-environment-variables-in-windows-xp-vista-and-7/#comment-2444930

I want to save a specific file that the program uses at run time. To ensure this works on all my computers, I need to use the variable path so it's not looking for my directory on the other computer.

 
Jose Kreif
Greenhorn
Posts: 22
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I found the answer.





So, here is how I assume it implements.

 
Knute Snortum
Sheriff
Posts: 4270
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use System.getenv().
 
Fred Kleinschmidt
Bartender
Posts: 571
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What if the user has not set that environment variable ("TMP")? You need to check that the value returned by the env.get("TMP") is not null before you use it.
 
Jose Kreif
Greenhorn
Posts: 22
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Fred Kleinschmidt wrote:What if the user has not set that environment variable ("TMP")? You need to check that the value returned by the env.get("TMP") is not null before you use it.


On every windows computer I have used, TMP was set correctly.

Correct me if I'm wrong but, TMP is a path setup by default by Microsoft.

All these should always be ready to use: http://www.askvg.com/list-of-environment-variables-in-windows-xp-vista-and-7/#comment-2444930
 
Fred Kleinschmidt
Bartender
Posts: 571
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You did not specify what platforms "all of my computers" were -as far as I knew, some could have been MAC, UNIX, or LINUX. anyway, the user can always remove an environment variable.
 
Jose Kreif
Greenhorn
Posts: 22
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Fred Kleinschmidt wrote:You did not specify what platforms "all of my computers" were -as far as I knew, some could have been MAC, UNIX, or LINUX.


Right, sorry about that. I was referring to my Windows computers. Which is why I was asking about "Accessing Windows Environment Variables"

Fred Kleinschmidt wrote: anyway, the user can always remove an environment variable.


This is a possibility to keep in mind for when launching software towards a large range of people. I'm privately releasing for friends and family, most of which don't have administrator rights to mess with environmental variables.
 
Fred Kleinschmidt
Bartender
Posts: 571
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
On Windows, TMP is a User variable, not a system variable. the user can always change it.
 
Knute Snortum
Sheriff
Posts: 4270
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Checking whether an environment variable is there or not is called defensive programming, and it's a good habit to get into. you should programming for every possibility, even if you're 99% sure it won't happen.
 
Jose Kreif
Greenhorn
Posts: 22
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Fred Kleinschmidt wrote:On Windows, TMP is a User variable, not a system variable. the user can always change it.


So then %USERPROFILE%\AppData\Local\Temp would be a better choice, right?
 
Fred Kleinschmidt
Bartender
Posts: 571
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If the file in question is one that is a fixed input file for the program, the file should be included with the compiled code in a jar file that the user executes. Then there is no need to fool around with environment variables.
 
Jose Kreif
Greenhorn
Posts: 22
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Fred Kleinschmidt wrote:If the file in question is one that is a fixed input file for the program, the file should be included with the compiled code in a jar file that the user executes. Then there is no need to fool around with environment variables.


I read and write to this 1 specific file. If I package it with the jar, could I still write to it?
 
Knute Snortum
Sheriff
Posts: 4270
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you're reading and writing to the file, and the contents doesn't need to be saved, I would use a temp file. If you have Java 8, look into File.createTempFile().
 
Fred Kleinschmidt
Bartender
Posts: 571
9
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, your program cannot write to a text file that is in a jar.
An alternative is to execute the program via a batch script that passes the file path to the program as a command line argument; This allows the user to place the file anywhere (such as wherever the program and batch file reside)
 
Paul Clapham
Sheriff
Posts: 22816
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If all of this discussion is in aid of your application being able to save a file, then you don't need any environment variables. Knute already mentioned how to deal with temporary files in Java, but if you want to save a file permanently (i.e. it will still be there the next time the application runs) then you can put it in the user's home directory. You can find a path to that directory like this:


 
Jose Kreif
Greenhorn
Posts: 22
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:If all of this discussion is in aid of your application being able to save a file, then you don't need any environment variables. Knute already mentioned how to deal with temporary files in Java, but if you want to save a file permanently (i.e. it will still be there the next time the application runs) then you can put it in the user's home directory. You can find a path to that directory like this:



Thanks. I'm looking into using the File.createTempFile() method
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!