• 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

Reading environment variables using System.getProperty

 
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,
I have to read the environment variable set in the system.
So I am using System.getProperty("APPLICATION_HOME");
But it is returning null. I have set the APPLICATION_HOME varible in environment variable on my system. I am not understanding why it is returning null even after setting the in environment variables.

My Operating system is : windows xp.
My JDK version is : JDK1.3.1_08.

sometime back I used the same to read the environment properties in some other project, but I not getting why it is returning null. Please help me in this.

Many Thanks
Vishnu
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You cannot read environment variables using System.getProperty(). In fact, in Java 1.3.1 there is nothing in the standard API to read environment variables.

In Java 1.5, there's System.getenv(), but that won't be of much use to you if you're stuck with Java 1.3.1.

You can set Java system properties (that you can read using System.getProperty()) on the command line by using the "-Dname=value" switch on the command line, for example:

java -DAPPLICATION_HOME=C:\apphome com.mypackage.MyMainClass
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
System properties are not environment variables -- they're values in a Properties object held by the System class. On the command line, you can set system properties with

java -DFOO=foo

which sets the system property FOO equal to "foo". But the system properties don't contain environment variables unless a script that starts your program uses -D to set the values of some system properties to the values of some environment variables.

Perhaps you are thinking of the once-deprecated, now no-longer-so "System.getenv()" method.
 
Ranch Hand
Posts: 1140
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ernest Friedman-Hill:
Perhaps you are thinking of the once-deprecated, now no-longer-so "System.getenv()" method.



Actually System.getenv() is back now with JDK 5.0
 
Ernest Friedman-Hill
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Mani Ram:


Actually System.getenv() is back now with JDK 5.0



Ummmm... yes. That's what I said, right? It was once deprecated, and now it is no longer deprecated.
 
Vishnu Murthy
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi thanks all,
in the follwing article
http://java.sun.com/developer/JDCTechTips/2001/tt1204.html#tip1
it is mentioned that

public class EnvDemo2 {
public static void main(String args[]) {
String s =
System.getProperty("localuser");
System.out.println("localuser = " + s);
}
}



If you issue the commands:

javac EnvDemo2.java

java -Dlocaluser=$USERNAME EnvDemo2
the result is:

localuser = username
where "username" is replaced with the value of the USERNAME environment variable on your system. Of course, this assumes that your system includes the USERNAME environment variable.


but even if I define the variable in the environment variable I am getting the output as what I passed. i.e for the above program
my output is
localuser = null, it is not getting the value that is set for environment variable USERNAME, but it is displays null. why it is behaving differently from the behaviour as explained in the article.


Thanks
Vishnu
 
Jesper de Jong
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are running on Windows XP, aren't you? The syntax $USERNAME is for Unix. For Windows, try:

java -Dlocaluser=%USERNAME% EnvDemo2
 
Vishnu Murthy
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Jesper,
Thanks it is working now
 
Ranch Hand
Posts: 46
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Vishnu Murthy wrote:Hi Jesper,
Thanks it is working now



This thread for System.getenv was really helpful. it helped me to access environmental variables set in UNIX from java.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic