Reputation: 363
I have written a simple routine in .Net that I need to call from Java and check the exit value. For some reason when called from Java, waitFor never ends. This is in spite of the fact that when called from command prompt .Net routine ends quickly and when called from test.bat it properly returns -1. Anyone has any idea what the problem is?
Here is Java code:
public static int runOnceWait(String[] cmdarray) throws IOException, InterruptedException
{
Process p;
p = Runtime.getRuntime().exec(cmdarray);
int res = p.waitFor();
p.destroy();
return res;
}
/**
* @param args
* @throws InterruptedException
* @throws IOException
*/
public static void main(String[] args) throws IOException, InterruptedException
{
String [] cmd = new String[2];
cmd[0]=signer;
cmd[1]=fileLocation;
System.out.println ("\"" + cmd[0] + "\" \"" + cmd[1] + "\"");
System.out.println (runOnceWait(cmd));
}
Here's C# code:
static int Main(string[] args)
{
if (args.Length != 1 && args.Length != 2)
{
Console.WriteLine("Use: ");
Console.WriteLine("DocumentSigner.exe source");
Console.WriteLine(" or");
Console.WriteLine("DocumentSigner.exe source, destination");
return -100;
}
string destination = null;
try
{
if (args.Length == 1) destination = Sign(args[0]);
else destination = Sign(args[0], args[1]);
Console.WriteLine("Document signed and saved as " + destination);
}
catch (Exception e)
{
Console.WriteLine(e);
return -1;
}
return 0;
}
For testing purposes I even wrote a .bat file which behaves as expected, i.e. returns ERRORLEVEL of -1.
Here's .bat file:
@echo off
rem test.bat
"DocumentSigner.exe" "{5E3C1967-A26E-4FC5-A6A8-3F358F388A3D}.pdf"
@if "%ERRORLEVEL%" == "0" goto good
:fail
echo Execution Failed
echo return value = %ERRORLEVEL%
goto end
:good
echo Execution succeeded
echo Return value = %ERRORLEVEL%
goto end
:end
Upvotes: 1
Views: 261
Reputation: 25844
From the API reference of java.lang.Process (emphasis mine):
By default, the created subprocess does not have its own terminal or console. All its standard I/O (i.e. stdin, stdout, stderr) operations will be redirected to the parent process, where they can be accessed via the streams obtained using the methods getOutputStream(), getInputStream(), and getErrorStream(). The parent process uses these streams to feed input to and get output from the subprocess. Because some native platforms only provide limited buffer size for standard input and output streams, failure to promptly write the input stream or read the output stream of the subprocess may cause the subprocess to block, or even deadlock.
I would try to read from java what your c# application is writing with Console.WriteLine()
, e.g. doing something like this after your exec()
BufferedReader in = new BufferedReader(
new InputStreamReader(p.getInputStream()));
String line = null;
while ((line = in.readLine()) != null) {
System.out.println(line);
}
Upvotes: 5