views:

29

answers:

2

I have this in a VB.NET console application:

Dim p As ProcessStartInfo
p = New ProcessStartInfo(Environment.CurrentDirectory & "\bin\javac.exe",ClassName & ".java")
Dim ps As Process = Process.Start(p)

This does run the java compiler on the file, but it does so in a new window. I want the output from javac to appear in the same console that is running my application. How can I do this? Perhaps there is another method for running commands in the current console? Or maybe I can supress the second console window from opening and redirect its output to the current console?

+2  A: 

I don't think that you can run in same console, because it is occupied by your application. If it is just about showing output you can use stream redirection. If you do javac [here go params] >out.txt 2>err.txt you can later load outputs from them when javac finished.

You can even redirect streams to your application by ProcessStartInfo.RedirectStandardOutput and Process.StandardOutput

Andrey
+1 for the file idea, but it doesn't look like Process.StandardOutput can be used to set a different output stream; it's a readonly property.
Exp HP
@Exp HP but you can read from it
Andrey
Oh, I see what you mean now. I thought you were saying to set StandardOutput to Console.OpenStandardOutput or something like that. =P
Exp HP
@Exp HP no, but you can read it line by line using `ReadLine` and then output using `Console.WriteLine`
Andrey
+2  A: 

I don't think you can run in the same console but you can get the output by redirecting the standard out:

Dim si = New ProcessStartInfo(Environment.CurrentDirectory & "\bin\javac.exe",ClassName & ".java")

si.RedirectStandardOutput = True
si.UseShellExecute = False
Dim proc = New Process() 
proc.StartInfo = si
proc.Start()
proc.StandardOutput.ReadToEnd()
proc.WaitForExit()
brendan
Tried this and it works exactly as desired. +1 for now, and I'll probably accept this answer later. ;)
Exp HP