title: Windows - Using set and echo in Java program
body: <p>I want to set and echo a Windows variable in Java:</p>
  
  <pre><code>public static void main(String[] args) throws IOException
  {
  
      Runtime rt = Runtime.getRuntime();
      String[] cmd = { "cmd.exe", "/c", "set HOSTNAME=%COMPUTERNAME% "
              + "&amp;&amp; echo %HOSTNAME%" };
      Process proc = rt.exec(cmd);
  
      BufferedReader stdInput = new BufferedReader(
              new InputStreamReader(proc.getInputStream()));
  
      BufferedReader stdError = new BufferedReader(
              new InputStreamReader(proc.getErrorStream()));
  
      System.out.println("Output:\n");
      String s = null;
      while ((s = stdInput.readLine()) != null)
      {
          System.out.println(s);
      }
  
      System.out.println("Error (if any):\n");
      while ((s = stdError.readLine()) != null)
      {
          System.out.println(s);
      }
  }
  </code></pre>
  
  <p>I expect the program will print out my computer host name or I will use this value for another purpose. But the output is just like this:</p>
  
  <pre><code>Output:
  
  %HOSTNAME%
  Error (if any):
  </code></pre>
  
  <p>How could I get the value that I have set in the command <code>set HOSTNAME=%COMPUTERNAME%</code></p>