Dinesh
Dinesh

Reputation: 2066

Java and .net interoperability

I have a c# program through which i am opening cmd window as a a process. in this command window i am running a batch file. i am redirecting the output of that batch file commands to a Text File. When i run my application everything seems to be ok.

But few times, Application is giving some error like "Can't access the file. it's being used by another application" at the same time cmd window is not getting closed. If we close the cmd process through the Task Manager, then it's writing the content to the file and getting closed. Even though i closed the cmd process, still file handle is not getting released. so that i am not able to run the application next time onwards.Always it's saying Can't access the file. Only after restarting the system, it's working.

Here is my code:

Process objProcess = new Process();
ProcessStartInfo objProInfo = new ProcessStartInfo();
objProInfo.WindowStyle = ProcessWindowStyle.Maximized;
objProInfo.UseShellExecute = true;
objProInfo.FileName = "Batch file path"                
objProInfo.Arguments = "Some Arguments";
if (Directory.Exists(strOutputPath) == false)
{
  Directory.CreateDirectory(strOutputPath);
}               
objProInfo.CreateNoWindow = false;
objProcess.StartInfo = objProInfo;                
objProcess.Start();                
objProcess.WaitForExit();

test.bat:

 java classname argument > output.txt

Here is my question:

  1. I am not able to trace where the problem is..

  2. How we can see the process which holding handle on ant file.

  3. Is there any suggestions for Java and .net interoperability

Upvotes: 0

Views: 258

Answers (3)

Artiom Chilaru
Artiom Chilaru

Reputation: 12221

In situations like this I start up the Process Explorer ( by Sysinternals, awesome tool btw ) click Ctrl+F, and enter the name of the file. It will search across all running processes and will list the file handles to this file by the applications that have it open.

You can then either drop the handle, or kill the app - whatever you think is better )

Upvotes: 1

Graviton
Graviton

Reputation: 83326

I think the problem is because the java program is accessing the text file when the C# program is writing something on it, and hence a "file cannot access" problem.

If I were you, I would do everything in C#-- I won't use Java to read the state of the C# program. And I would access the file only after I've completed whatever the C# needs to do.

As for to see what process is locking up your file, you can use Process Explorer for this purpose.

Upvotes: 0

arunmur
arunmur

Reputation: 650

You can try forking and attaching file descriptor from C# rather than launching a bat file.

Upvotes: 0

Related Questions