Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

setting environment

Posted on 1998-11-19
13
199 Views
Last Modified: 2010-03-30
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?  

0
Comment
Question by:mbunkows
  • 7
  • 6
13 Comments
 
LVL 5

Expert Comment

by:fontaine
ID: 1227806
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?
0
 

Author Comment

by:mbunkows
ID: 1227807
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.

0
 

Author Comment

by:mbunkows
ID: 1227808
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);
                                   process.waitFor();
                                   int status= process.exitValue();
                                   System.out.println("Status is: " + status);
                              }
                              catch(IOException ex1)  {
                                   System.out.println("IOEXCEPTION");
                              }
                              catch(InterruptedException ex2)  {
                                   System.out.println("InterruptedException");
                              }
                         }
                    }
               }
               System.exit(0);        //invalid page fault error if this exit(0) is here
          }
     }

0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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.

 

Author Comment

by:mbunkows
ID: 1227809
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
0
 
LVL 5

Expert Comment

by:fontaine
ID: 1227810
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...
0
 

Author Comment

by:mbunkows
ID: 1227811
"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)


fontaine:
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

0
 
LVL 5

Expert Comment

by:fontaine
ID: 1227812
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) {
            System.out.print((char)read);
        }

    }

    public static void main(String args[]) throws Exception {
        Test test = new Test();
        return;
    }
}
0
 
LVL 5

Expert Comment

by:fontaine
ID: 1227813
runnint.bat contains the following:

echo %mymsg% Experts!
0
 

Author Comment

by:mbunkows
ID: 1227814
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)

 
0
 
LVL 5

Expert Comment

by:fontaine
ID: 1227815
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).
0
 

Author Comment

by:mbunkows
ID: 1227816
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
Thanks
0
 
LVL 5

Accepted Solution

by:
fontaine earned 100 total points
ID: 1227817
OK, like this, the question is locked.
0
 

Author Comment

by:mbunkows
ID: 1227818
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
Thanks
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

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.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
servlet example 11 49
ejb stateless example 2 19
jsp error 6 25
jsp CRUD operations with and without prepared statement also hibernatge 1 9
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question