Solved

setting environment

Posted on 1998-11-19
13
186 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
 

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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
allStar challenge 1 41
mapAB Challlenge 35 85
bitbucket vs gitbucket 3 30
backtracking recursion  code 19 40
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

708 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now