Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9142849
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T09:52:45+00:00 2026-06-17T09:52:45+00:00

Possible Duplicate: How do I set environment variables from Java? I’m working on Java.

  • 0

Possible Duplicate:
How do I set environment variables from Java?

I’m working on Java. I have to add an environment variable in java code programmatic such that it will be available when i get list using process builder as follows:

import java.util.Map;
import java.util.Set;

class helloworld  {
    public static void main(String[] args) {

        ProcessBuilder pb = new ProcessBuilder("export MY_ENV_VAR=1");

        Map<String, String> envMap = pb.environment();

        Set<String> keys = envMap.keySet();
        for(String key:keys){
            System.out.println(key+" ==> "+envMap.get(key));
        }

    }
}

But with above trial i cant get environment variable properly.
so How to set the environment variable ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T09:52:46+00:00Added an answer on June 17, 2026 at 9:52 am
     Map<String, String> env = pb.environment();
     env.put("MV_ENV_VAR", "1");
    

    would set MY_ENV_VAR=1. Before you invoke the Process by

    Process p = pb.start();
    

    export would only be interpreted by a shell.

    See also ProcessBuilder

    A full example:

    public static void main(String[] args) throws IOException {
    
        ProcessBuilder pb = new ProcessBuilder("CMD", "/C", "SET");
        Map<String, String> env = pb.environment();
        env.put("MYVAR", "myValue");
        Process p = pb.start();
        InputStreamReader isr = new InputStreamReader(p.getInputStream());
        char[] buf = new char[1024];
        while (!isr.ready()) {
            ;
        }
        while (isr.read(buf) != -1) {
            System.out.println(buf);
        }
    }
    

    prints among other environment values:

    MYVAR=myValue
    

    This should prove that the created process uses the manipulated environment.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: How do I set environment variables from Java? I'm trying to set
Possible Duplicate: Exporting an Environment Variable in Ruby I need to set several environment
Possible Duplicate: SmartGWT Dialog title not set correctly I have a problem, is that
Possible Duplicate: set the tabbar bottom on android all activities I have an application
Possible Duplicate: Android: Set interval in SeekBar I have a seekbar with max value
Possible Duplicate: What’s the difference between a System property and environment variable What is
Possible Duplicate: Set NA to 0 in R I have a data.frame with a
Possible Duplicate: Set Additional Data to highcharts series So I have this graph: http://jsfiddle.net/Gg3ZL/
Possible Duplicate: How to set cellpadding & cellspacing in CSS? i have a little
Possible Duplicate: Can I set a breakpoint on 'memory access' in GDB? I have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.