• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 231
  • Last Modified:

setting environment

I was wondering if there is any way to set environment
variables (DOS system) in the same command.com instance that spawned the Java program.
Runtime.exec() spawns another process (another DOS window) so unless there are options that I dont know about I dont think I can use that.
After setting an environment I also need to run a program from the same process that the environment variable is in

Any ideas?  

  • 7
  • 6
1 Solution
There is a Runtime.exec(String command, String envp[]) method that allows you to set
up the environment for the external process. envp is an array of String containing
the environment in name=value pairs (ex: String envp[] =  {"a=1", "b=2"}). Is this what
you are searching for?
mbunkowsAuthor Commented:
Doesnt Runtime.exec() spawn a separate process? (or window)

I'll give you a brief synopsis of my problem:

We have a DOS program that we need to set an environment variable for and then run the program... The Java program will select the value of the environment variable (from the Java GUI) and set the variable, start the program and return control to the calling DOS window (which now runs the DOS program)

The Java application cannot create a new DOS console (there would be two open then)
it needs to modify the old console, and run the program in the old console.

mbunkowsAuthor Commented:
Heres the code fragment Im testing it with:
I was hoping this would give me a HI Experts and then shut down the interpreter
I instead get an IOException (I get that exception without setting the environment also)

     class ButtonListener implements ActionListener  {
          public void actionPerformed(ActionEvent e)  {
               if (e.getActionCommand().equals("ok"))  {  
                    for (int i=0;i<checkbox.length;i++)  {
                         if (checkbox[i].getState())  {
                              try {
                                   Runtime r=Runtime.getRuntime();
                                   String command="echo %mymsg% experts!";
                                   String setenv={"mymsg=HI"};
                                   Process process= r.exec(command,setenv);
                                   int status= process.exitValue();
                                   System.out.println("Status is: " + status);
                              catch(IOException ex1)  {
                              catch(InterruptedException ex2)  {
               System.exit(0);        //invalid page fault error if this exit(0) is here

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

mbunkowsAuthor Commented:
The answer of No .. this cannot be done is an acceptable answer....
But I would need some reasons why...
with a possible workaround.. if any
The problem, as far as I see it, is that "echo" can not be run as an external process. This is
simply one of the commands that is understood by DOS. If you want this to work, you
will have to put the command in a .bat file, and launch the BAT file via Runtime.exec().
This is for the sample code you posted.

I don't have the feeling it is possible to run the program in the same console as the
one that is running your Java application because they are two separate process...
mbunkowsAuthor Commented:
"echo" isnt really the program i was trying to run.. that was just a test

so the way I understand it even if I put the commands in a batch file to run and call the batch
file via Runtime.exec() I cannot run it in the console that is already there... i need to run it in another DOS window.

The plan was to call a Java app from the DOS program and then return to the DOS program when finished. (running a batch file that was created in the Java App)

If you are saying that this is impossible I am inclined to belive you (I havent found a way and noone else has responded)
Post your answer and ill give you the points

What about the following code. Is that what you was expecting?

import java.io.*;

public class Test {

    public Test() throws Exception {
        Runtime runtime = Runtime.getRuntime();
        String  command = "runnit.bat";
        String setenv[] ={"mymsg=HI"};

        Process process = runtime.exec(command, setenv);
        InputStream in = process.getInputStream();

        int read = -1;

        while((read = in.read()) > -1) {


    public static void main(String args[]) throws Exception {
        Test test = new Test();
runnint.bat contains the following:

echo %mymsg% Experts!
mbunkowsAuthor Commented:
Your solution worked great for the test case!

However, Im a little confused on why....
I thought that Runtime.exec() spawned a new process (therefore a new window would appear to run it)

I also tried to run the actual DOS program and i couldnt get it to take any input.  The JVM was still running (since the batch file hasnt returned yet) and I probably have some memory issues to deal with there (which are hardly your or Java's fault)

If you could briefly explain why in some cases exec() spawns another window and in other cases it doesnt that would be great (also "answer" the question)

Have you a new window spawning when you are using ".bat" file. I would think that no,
because these are actually batch files. Hence, no window should be associated to them.
I remember that we also tried to remove the spawning of the DOS window when we were
running a FORTRAN executable compiled for DOS from a C program (so the problem is not
specific to Java). We had no clue. So, I would say that the problem is not with exec():

- for DOS batch files (.bat), no window will pop up;
- for DOS applications, you will always have a window (if you have access to the source
  code, you could maybe take a look at the eventual compiler options, or re-compile for
  Windows and see if a window still pops up).
mbunkowsAuthor Commented:
ahh i see ...
so when run from a batch file -- no window
run the .exe directly and you get a window

when i ran it from a batch file (which is an acceptable solution)
i can see the main menu of the dos program but it wont accept any commands
which obviously isnt Java's or your responsibility
it must be a memory issue since java is still running

leave an "answer" and ill grade it
OK, like this, the question is locked.
mbunkowsAuthor Commented:
The question, as stated, was answered and answered well.
The problem Im having now really doesnt have much to do with Java
I just have to figure out if its a memory issue or something else
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 7
  • 6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now