Results 1 to 5 of 5

Thread: Java - bash environment variables

  1. #1
    Join Date
    Aug 2009
    Beans
    8

    Question Java - bash environment variables

    Hi folks,

    there are certain bash environment variables defined in my ~/.bashrc and accessing them through bash terminal works indeed. But these environment variables are not accessible when I try to execute any command in Java.

    For example, when I run following code:

    Code:
        try {
            //run command "printenv"
            Process proc = Runtime.getRuntime().exec("printenv");
            BufferedReader read = new BufferedReader(new InputStreamReader(proc.getInputStream()));
    
            //print output
            while (read.ready())
                System.out.println(read.readLine());
            
        } catch (IOException e) {
                System.out.println(e.getMessage());
        }

    it does not print all environment variables (excludes those defines in ~/.bashrc). But running command printenv directly in bash terminal prints all environment variables.

    One of the options is pharsing ~/.bashrc in Java to read environment variables. Are there any other options?


    Thank you for your help.

  2. #2
    Join Date
    Nov 2012
    Location
    Halloween Town
    Beans
    Hidden!
    Distro
    Xubuntu Development Release

    Re: Java - bash environment variables

    IMHO, parsing is a very good option. Just output the environment variables and in your Java parser simply read one line at a time and split on the first equals sign on each line. You could store them in a Map, where the bit before the equals sign would be the key, and the bit after it would be the value, i.e. VAR_NAME=VAR_VALUE would become <VAR_NAME, VAR_VALUE> in a Map<String, String>.

  3. #3
    Join Date
    May 2007
    Location
    Leeds, UK
    Beans
    1,664
    Distro
    Ubuntu 13.10 Saucy Salamander

    Re: Java - bash environment variables

    It's not a good option if someone wants to run it who doesn't have a .bashrc file though. Could you define the values you need in a .properties file? No need to parse that, just load up with the load() method of Java.util.Properties. You could also load it from the classpath with getResourceAsStream() from java.lang.Class.

    Edit: Alternatively, have you tried System.getenv() to get environment variables?
    Last edited by r-senior; March 1st, 2013 at 07:29 PM.
    Please create new threads for new questions.
    Please wrap code in code tags using the '#' button or enter it in your post like this: [code]...[/code].

  4. #4
    Join Date
    Nov 2012
    Location
    Halloween Town
    Beans
    Hidden!
    Distro
    Xubuntu Development Release

    Re: Java - bash environment variables

    Quote Originally Posted by r-senior View Post
    It's not a good option if someone wants to run it who doesn't have a .bashrc file though.
    To be honest that scenario didn't cross my mind.
    Quote Originally Posted by r-senior View Post
    Edit: Alternatively, have you tried System.getenv() to get environment variables?
    Yes, good idea. It's really much simpler than parsing, plus it's still possible to store them the some way I previously posted:
    Code:
    Map<String, String> env = System.getenv();
    for (String envName : env.keySet()) {
         System.out.format("%s=%s%n", envName, env.get(envName));
    }

  5. #5
    Join Date
    Aug 2011
    Location
    47°9′S 126°43W
    Beans
    1,913
    Distro
    Kubuntu 12.10 Quantal Quetzal

    Re: Java - bash environment variables

    IMHO, Bash is Bash and Java is Java. Your Java code should really use System.getProperty(), and the bash script that starts the Java app could define theses properties from environment variables:
    Code:
    java -Dsome.property=$SOME_BASH_VARIABLE YourApp

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •