wood burning stoves 2.0*
The moose likes Java in General and the fly likes Regarding  accessing environment variables Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Regarding  accessing environment variables" Watch "Regarding  accessing environment variables" New topic
Author

Regarding accessing environment variables

babu sharath
Ranch Hand

Joined: Jul 06, 2006
Posts: 42
HI ,

I created a environment variable using:
setenv file_name /home/test/a.txt
How to access that environment variable from java code.
I tried
String s = System.getenv("file_name");
I am getting following error when runnig :

Exception in thread "main" java.lang.Error: getenv no longer supported, use properties and -D instead: input
at java.lang.System.getenv(System.java:691)
at Li.main(Li.java:6)

Than I tried
String s = System.getProperty("file_name"); It is giving null as output

How to access the environment vatiable ?
babu sharath
Ranch Hand

Joined: Jul 06, 2006
Posts: 42
Sanjeev Singh
Ranch Hand

Joined: Nov 01, 2006
Posts: 381
Why dont you are running the jvm with -D option.
like
java -Dmy.class.path = .......

you can get access to the variable my.class.path using System.getProperty("my.class.path");


~Sanjeev Singh<br />SCJP 1.5
babu sharath
Ranch Hand

Joined: Jul 06, 2006
Posts: 42
due to some reasons I dont want to use -D option
If I have 10 enviromnet variables , than everytime -D with 1- those variables is not a good option i think
Shaan Shar
Ranch Hand

Joined: Dec 27, 2005
Posts: 1249

Originally posted by babu sharath:
due to some reasons I dont want to use -D option
If I have 10 enviromnet variables , than everytime -D with 1- those variables is not a good option i think


Well you can use properties file to access environment properties.

Here I can show you one example. but rest about your application you need to do yourself.



Hope it may be usefull for you.


The Best way to predict your future is to create it - Every great individual common man
Swami Vidhyananda
Greenhorn

Joined: Apr 06, 2006
Posts: 6
Hi,

First we have to decide whether to use Environment Variables or System Properties. While programming in Java Environment, it is best to use System Properties instead of Env Variables.

Here is the reason,

System properties and environment variables are both conceptually mappings between names and values. Both mechanisms can be used to pass user-defined information to a Java process. Environment variables have a more global effect, because they are visible to all descendants of the process which defines them, not just the immediate Java subprocess. They can have subtly different semantics, such as case insensitivity, on different operating systems. For these reasons, environment variables are more likely to have unintended side effects. It is best to use system properties where possible. Environment variables should be used when a global effect is desired, or when an external system interface requires an environment variable (such as PATH).


~ THE TOUGHEST THING IS TO REMAIN FOCUSED ~
babu sharath
Ranch Hand

Joined: Jul 06, 2006
Posts: 42
What is cmd.exe in the code
I am running in UNIX environment
When I run the code I am getting following error :

java.io.IOException: cmd.exe: not found
at java.lang.UNIXProcess.forkAndExec(Native Method)
at java.lang.UNIXProcess.<init>(UNIXProcess.java:52)
at java.lang.Runtime.execInternal(Native Method)
at java.lang.Runtime.exec(Runtime.java:566)
at java.lang.Runtime.exec(Runtime.java:428)
at java.lang.Runtime.exec(Runtime.java:364)
at java.lang.Runtime.exec(Runtime.java:326)
at Sample.getEnv(Sample.java:19)
at Sample.main(Sample.java:10)
Sanjeev Singh
Ranch Hand

Joined: Nov 01, 2006
Posts: 381
Use the following command
For UNIX use : env
For Windows/nt : cmd.exe
For Windows9x : command.com
Naveen Zed
Ranch Hand

Joined: Aug 11, 2006
Posts: 61
I woder...! were these are all in SCJP....?If there, There is alot I have to get.
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Yes, the right place for this is Java In General (Intermediate). Moving...


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Rahul Bhattacharjee
Ranch Hand

Joined: Nov 29, 2005
Posts: 2308
Why do you want to pass arguments using system variables , why do not consider a property file for you application containing all the required properties for your application.


Rahul Bhattacharjee
LinkedIn - Blog
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Regarding accessing environment variables