Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Regarding accessing environment variables

 
babu sharath
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Sanjeev Singh
Ranch Hand
Posts: 381
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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");
 
babu sharath
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1249
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Swami Vidhyananda
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
babu sharath
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 381
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use the following command
For UNIX use : env
For Windows/nt : cmd.exe
For Windows9x : command.com
 
Naveen Zed
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I woder...! were these are all in SCJP....?If there, There is alot I have to get.
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, the right place for this is Java In General (Intermediate). Moving...
 
Rahul Bhattacharjee
Ranch Hand
Posts: 2308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic