locked
how to keep command prompt open

    Question

  • I am running java with a jar file from a vb.net app. After it runs the command prompt window closes. Is there a way to keep it open so I can see what messages were generated?

    This is how I'm doing it now:
    Dim jprc As New Process
    jprc.StartInfo.WindowStyle = ProcessWindowStyle.Normal
    jprc.StartInfo.FileName = "java.exe"
    jprc.StartInfo.Arguments = JarAndArgs
    jprc.Start()
    
    Thursday, June 11, 2009 12:57 PM

Answers

  • Create a batch file which will invoke the java exe with required arguments.

    Invoke the batch file using Process.Start()
    Thanks,
    A.m.a.L
    .Net Goodies
    Remember to click "mark as answered" when you get a correct reply to your question
    • Marked as answer by rwbta2 Thursday, June 11, 2009 3:26 PM
    Thursday, June 11, 2009 1:31 PM
  • Thank you A.m.a.l; I tried that but the command prompt still disappears. I did find some info at: http://social.msdn.microsoft.com/Forums/en-US/vblanguage/thread/17d91cbe-319c-4cfb-9d2f-ab7c8e4a8188/ which was helpful

    I changed the above code as follows so I can view the results after the batch file runs.

     

    Dim jprc As Process = New Process
    jprc.StartInfo.FileName = BatchFilePath
    jprc.StartInfo.UseShellExecute = False
    jprc.StartInfo.RedirectStandardOutput = True
    jprc.Start()
    TextBox1.Text = jprc.StandardOutput.ReadToEnd
    
    • Marked as answer by rwbta2 Thursday, June 11, 2009 3:26 PM
    Thursday, June 11, 2009 3:26 PM

All replies

  • Create a batch file which will invoke the java exe with required arguments.

    Invoke the batch file using Process.Start()
    Thanks,
    A.m.a.L
    .Net Goodies
    Remember to click "mark as answered" when you get a correct reply to your question
    • Marked as answer by rwbta2 Thursday, June 11, 2009 3:26 PM
    Thursday, June 11, 2009 1:31 PM
  • You could use MyConsole.  Here's and example using notepad:

    Start a new Windows Forms application and replace the Form1 code with this code:

    Public Class Form1
      Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim Cnsl As New MyConsole
        Cnsl.Show()
        Cnsl.CmdTB.Text = "notepad.exe C:\Users\John\Desktop\Text.txt"
        Cnsl.CmdBtn.PerformClick()
      End Sub
    End Class
    Add a Form named MyConsole and replace the code with this code:

    Imports System.Text
    Public Class MyConsole
      Dim MS As New MenuStrip
      Friend WithEvents CmdBtn As ToolStripItem
      Friend CmdTB As New TextBox
      Friend ResultTB As New TextBox
      Dim P As New Process
      Private Sub MyConsole_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        SetControls()
        SetProcess()
        CmdBtn.PerformClick()
      End Sub
      Private Sub SetControls()
        CmdBtn = MS.Items.Add("Send Command")
        CmdTB.Parent = Me
        CmdTB.Dock = DockStyle.Top
        CmdTB.Text = "Dir"
        MS.Parent = Me
        MS.Dock = DockStyle.Top
        ResultTB.Parent = Me
        ResultTB.Multiline = True
        ResultTB.Dock = DockStyle.Fill
      End Sub
      Private Sub SetProcess()
        With P
          With .StartInfo
            .FileName = "cmd.exe"
            .Arguments = "/K"
            .RedirectStandardError = True
            .RedirectStandardInput = True
            .RedirectStandardOutput = True
            .CreateNoWindow = True
            .UseShellExecute = False
          End With
          .Start()
          .StandardInput.AutoFlush = True
        End With
      End Sub
      Private Sub CmdBtn_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles CmdBtn.Click
        With P
          With .StandardInput
            .WriteLine("prompt DELETEME:")
            .WriteLine(CmdTB.Text)
            .WriteLine("echo ---end of command---")
          End With
          Dim output As New StringBuilder()
          Dim currentLine As String = ""
          .StandardOutput.ReadLine()
          'discard line   
          While currentLine <> "---end of command---"
            If Not currentLine.StartsWith("DELETEME:") Then
              output.Append(currentLine)
              output.Append(Environment.NewLine)
            End If
            currentLine = .StandardOutput.ReadLine()
          End While
          ResultTB.Text = output.ToString
        End With
      End Sub
    End Class
    
    
    
    • Edited by JohnWein Thursday, June 11, 2009 2:56 PM
    Thursday, June 11, 2009 2:48 PM
  • Thank you A.m.a.l; I tried that but the command prompt still disappears. I did find some info at: http://social.msdn.microsoft.com/Forums/en-US/vblanguage/thread/17d91cbe-319c-4cfb-9d2f-ab7c8e4a8188/ which was helpful

    I changed the above code as follows so I can view the results after the batch file runs.

     

    Dim jprc As Process = New Process
    jprc.StartInfo.FileName = BatchFilePath
    jprc.StartInfo.UseShellExecute = False
    jprc.StartInfo.RedirectStandardOutput = True
    jprc.Start()
    TextBox1.Text = jprc.StandardOutput.ReadToEnd
    
    • Marked as answer by rwbta2 Thursday, June 11, 2009 3:26 PM
    Thursday, June 11, 2009 3:26 PM
  • Thanks John; you were certainly thorough. I gonna add this code to my treasure trove folder of useful code.
    Thursday, June 11, 2009 3:50 PM