• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to use Environment Variables inside of application?  RSS feed

 
Jahed Hossain
Greenhorn
Posts: 11
Firefox Browser Linux Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I need to be able to access Environment Variables to find files such as:



Plus, I want to make my application cross-platform on at least Windows and Linux OSes.

So, how do I do it?
 
Rajesh Vassey
Greenhorn
Posts: 8
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For accessing Environment variables use below :
processBuilder.environment().get("variablename");
http://docs.oracle.com/javase/tutorial/essential/environment/env.html

For making your app cross platform:
Whatever app you build using java is going to be a cross platform because of JRE, As different OS have different JREs. If your code uses environment variables then please make sure you follow the points given in the above same link.
 
Richard Tookey
Bartender
Posts: 1166
17
Java Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why do you need to access a jar file from your application?

As far as making things cross-platform is concerned environmental variables are bad news since event if they exist they may not have the same name on different OS. you should try to make sure any files are in a subdirectory relative to the user's home directory or the current working directory. The home directory is always available using System.getProperty("user.home") and the working directory using System.getProperty("user.dir") . When I want to create files I usually use the home directory as the base directory since that is the only directory that is pretty much guaranteed to be writeable on all OS. If you need to create temporary files then you can also use System.getProperty("java.io.tmpdir") but remember that on some OS files in this area get remove when the system is re-started.
 
Jahed Hossain
Greenhorn
Posts: 11
Firefox Browser Linux Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Richard Tookey wrote:Why do you need to access a jar file from your application?

As far as making things cross-platform is concerned environmental variables are bad news since event if they exist they may not have the same name on different OS. You should try to make sure any files are in a subdirectory relative to the user's home directory or the current working directory. The home directory is always available using System.getProperty("user.home") and the working directory using System.getProperty("user.dir") . When I want to create files I usually use the home directory as the base directory since that is the only directory that is pretty much guaranteed to be writeable on all OS. If you need to create temporary files then you can also use System.getProperty("java.io.tmpdir") but remember that on some OS files in this area get remove when the system is re-started.


Actually I'm creating a Minecraft Backup & Restore application and I don't want the users to go through the trouble searching for the minecraft location. Thanks for the info.
If I finish this, this will be my fully functional GUI app in java :|

 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can use System.getenv() to get a map containing the environment variables, or System.getenv(name) to get the value of a specific environment variable.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!