?
Solved

setting environment

Posted on 1998-11-19
13
Medium Priority
?
221 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
[X]
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
  • 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
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 

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 400 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

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
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 will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month13 days, 19 hours left to enroll

800 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