[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

setting environment

Posted on 1998-11-19
13
Medium Priority
?
228 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

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

Tech or Treat!

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

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

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…
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
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:
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses

650 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