aspose file tools*
The moose likes Beginning Java and the fly likes Get environment variables Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Get environment variables" Watch "Get environment variables" New topic
Author

Get environment variables

Scarlet Li
Greenhorn

Joined: Dec 13, 2011
Posts: 4
Hi,

I am trying to get a program to run when a button is pressed
I did:
export EJBCA_HOME=/home/user/ejbca_4_0_6
and also:
export EJBCA_HOME="/home/user/ejbca_4_0_6"

I used this code to get the environment variable:
And this code to run the program:
However, I got "Cannot run program "bin/ejbca.sh": java.io.IOException: error=2, No such file or directory"

If I used:There is no error.
Can anyone help me with this?
Kuldip Shetty
Ranch Hand

Joined: Jan 07, 2010
Posts: 42

As per javadocs System.getEnv method has been deprecated and it recommend using System.getProperty. Hope this should resolve the environment variable issue.

Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19720
    
  20

Kuldip Shetty wrote:As per javadocs System.getEnv method has been deprecated and it recommend using System.getProperty.

That's no longer true. System.getenv was indeed deprecated, but only in Java 1.4. Since Java 5.0 it's no longer deprecated.

Scarlet, where did you export the variable? If you did that in a separate shell window, then you've exported it only for that shell window. If you create a new shell window it will not have that environment variable. The proper solution is to let the variable be set whenever you login. In Bash there is a .profile or .bash_profile file in your home folder in which you can export it. For other shells or how to set it for all users, please check the documentation for your particular shell. (For your information, Windows has the same limitation. Only if you set environment variables through the system properties window will they be persistent, but any existing window will still use the old variables.)

Keep in mind that your web container (Tomcat?) probably runs as a specific user that may not have a shell at all. You should check out how to set environment variables if that's the case.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Scarlet Li
Greenhorn

Joined: Dec 13, 2011
Posts: 4
Rob Spoor wrote:
Kuldip Shetty wrote:As per javadocs System.getEnv method has been deprecated and it recommend using System.getProperty.

That's no longer true. System.getenv was indeed deprecated, but only in Java 1.4. Since Java 5.0 it's no longer deprecated.

Scarlet, where did you export the variable? If you did that in a separate shell window, then you've exported it only for that shell window. If you create a new shell window it will not have that environment variable. The proper solution is to let the variable be set whenever you login. In Bash there is a .profile or .bash_profile file in your home folder in which you can export it. For other shells or how to set it for all users, please check the documentation for your particular shell. (For your information, Windows has the same limitation. Only if you set environment variables through the system properties window will they be persistent, but any existing window will still use the old variables.)

Keep in mind that your web container (Tomcat?) probably runs as a specific user that may not have a shell at all. You should check out how to set environment variables if that's the case.


I had run the command on a terminal of which the pwd was/home/user. The code I am trying to run is compiled in eclipse and deployed to jboss. How do I set the variable to be set whenever I login? Do I just add that command anywhere in .profile file?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19720
    
  20

In Tomcat, you can pass argument to the JVM itself. These can include -Dname=value pairs. These will show up when using System.getProperty and System.getProperties. I am positive you can do the same for JBoss as well. You'll move these variables from environment variables to JVM properties.
Scarlet Li
Greenhorn

Joined: Dec 13, 2011
Posts: 4
I am still not sure how to do this. For the environment variable, do I run the export command in the directory of the .profile file or do I "echo export EJBCA_HOME="/home/user/ejbca" " into the .profile file.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Scarlet Li wrote:I am still not sure how to do this. For the environment variable, do I run the export command in the directory of the .profile file or do I "echo export EJBCA_HOME="/home/user/ejbca" " into the .profile file.


At this point this is really not a Java issue at all. It's an OS/shell/environment issue.

If you want a given user to have a given environment variable set at all times, then you need to have a line like

in an appropriate file.

What that file is depends on the user's shell, how his login session starts, etc. If you read the man pages for whatever shell that user is using (zsh, bash, ksh, csh, tcsh, sh, ...) it will tell you which files are read when. Some shells are login shells and some are not. Some of these files are read only for login shells.

In short, it is complicated and highly system- and context-dependent.

You're better off doing as suggested and just passing -Dkey=value on the command line and using System.getProperty, OR putting all the key/value pairs in a file that your app knows about and that can be specified at startup time and then using the java.util.Properties class to load that file.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Get environment variables