• 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

Get environment variables

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 45
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As per javadocs System.getEnv method has been deprecated and it recommend using System.getProperty. Hope this should resolve the environment variable issue.

 
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Scarlet Li
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic