Groups | Blog | Home
all groups > vj# > november 2003 >

vj# : Unable to pass environmental variables to executable.


anonymous
11/5/2003 3:20:21 AM
java.lang.Runtime.exec(String, String[]) and
java.lang.Runtime.exec(String[], String[]) does not pass
environmental variables to executable command. Works in
Java, but not in J# Just in case it makes a difference it
is being used in Console Application. The executable
executes, but no environmental variables are found by the
mikegreonline NO[at]SPAM microsoft.com
11/6/2003 12:03:36 AM
How are you attempting to access the environment variables in the process
you are calling exec on?

Thanks,

Michael Green
Microsoft Developer Support

This posting is provided "AS IS" with no warranties, and confers no rights.
Please reply to newsgroups only.
anonymous NO[at]SPAM discussions.microsoft.com
11/6/2003 8:08:00 PM
Michael,

First let me start by saying thank you for your quick
response. I greatly
appreciate your assistance in this. I have been working on
getting out a
product that I have been portting from Java to J# .NET and
have run into
this problem.

Re:
[quoted text, click to view]

I am trying to access the environmental variables from the
process that
java.lang.Runtime.exec(String, String[]) and
java.lang.Runtime.exec(String[], String[]) spawns.

I have attached jcgi.jsl, a little program that attempts
to duplicate the
problem. It calls t.bat and t.bat dumps all environmental
variables and then
MYVAR which was passed in by jcgi.jsl when it called
java.lang.Runtime.exec(String[], String[]). InputReader is
a little class
that asynchronously handles STDOUT AND STDERR.

Also, from what I recalled the program worked on J++ and
JVIEW.

If you would like to work on this together feel free to
contact me anytime
at 201.998.1048.

Thanks in advance,

Manny


t.bat:
set
set MYVAR
echo %MYVAR%


InputReader.jsl:
import java.io.InputStream;
import java.io.PrintStream;

public final class InputReader extends Thread
{
boolean done;
InputStream is;
PrintStream os;

protected InputReader(InputStream is, PrintStream
os)
{
this.is = is;
this.os = os;
done = false;
}

protected synchronized final void waitFor()
{
try
{
while(!done)
{
wait();
}
}
catch(InterruptedException ex)
{
// [mjg] ignore
}
}

public synchronized final void run()
{
try
{
byte buffer[] = new byte[1024];

int i;

int length = buffer.length;

while((i = is.read(buffer, 0,
length)) != -1)
{
os.write(buffer, 0, i);
}
}
catch(Exception ex)
{
}
finally
{
done = true;

notify();
}
}
}


jcgi.jsl:
import java.io.InputStream;
import java.io.OutputStream;

public class jcgi
{
public static void main(String[] args) throws
Exception
{
InputStream is = null;
OutputStream os = null;
InputStream es = null;

try
{
String[] cmd = new String[3];
// xp
cmd[0] = "c:\\windows\\system32
\\cmd.exe";
// nt/2k
//cmd[0] = "c:\\winnt\\system32
\\cmd.exe";
// 9x
//cmd[0]
= "c:\\windows\\command.com";
cmd[1] = "/c";
cmd[2] = "t.bat";

String[] envp = new String[1];
envp[0] = "MYVAR=myvalue";

Process p = Runtime.getRuntime
().exec(cmd, envp);

is = p.getInputStream();
os = p.getOutputStream();
es = p.getErrorStream();

InputReader ir = new InputReader
(is, System.out);
InputReader er = new InputReader
(es, System.err);

ir.start();
er.start();

int rc = -1;
try
{
rc = p.waitFor();
}
catch(InterruptedException ex)
{
// ignore
}

ir.waitFor();
er.waitFor();
}
finally
{
if(is != null)
{
try
{
is.close();
}
catch(Exception ex)
{
// ignore
}
}

if(os != null)
{
try
{
os.close();
}
catch(Exception ex)
{
// ignore
}
}

if(es != null)
{
try
{
es.close();
}
catch(Exception ex)
{
// ignore
}
}
}
}
}
mikegreonline NO[at]SPAM microsoft.com
11/10/2003 7:28:32 PM
Hi Manny,

I did some digging around and found a bug already reported for this issue.
Unfortunately I can't say when or if this will be fixed, but I can
recommend a workaround. There is a Win32 API called SetEnvironmentVariable
that you can call from J# code via PInvoke to set any enviroment variables
you may need. I know this is not the cleanest solution, but it may help
you get past this bug.

Thanks,

Michael Green
Microsoft Developer Support

This posting is provided "AS IS" with no warranties, and confers no rights.
Please reply to newsgroups only.
AddThis Social Bookmark Button