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] > How are you attempting to access the environment
variables in the process
> you are calling exec on?
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
}
}
}
}
}