Using Windows, I entered a command in my cmd window to run an executable file that has your basic "Hello, world!" line printed by the program. When I enter the "start out.exe" command though, a window opens and closes in a quick second. I'm guessing that this window is the window where the hello world message is being printed, but it closes so quickly (maybe because the program is finished). Is there some sort of setting I have that is causing the window to close immediately? Is that just the default? And if so, is there a setting or something that I can change to prevent the window from closing immediately (without changing the nature of the program)? Thanks in advance for your help. I've seen some similar questions, but the ones I have seen are specifically for Linux/Unix or suggested to change the program itself.
1 Answers
If you possess .PDB files, generated at build time (symbols of .EXE file) and required .DLL libraries for your program (In your case those might not be required due to the simple nature of a "Hello World" program), you could try to open the executable as project in Visual Studio and attach a debugger to it, using breakpoints to stop program executions before main returns. This article shows how to do it as a proof of concept.
Open .EXE file by opening VS and navigating to File-Open-Project/Solution and selecting the executable file, then right-click on your "solution" and press Debug-Start new instance. PDB and DLL files should be in the same folder as EXE file.
If you also don't have source code available of the program, follow this advice in order to try to debug it:
To effectively debug you’ll also need the source code that was used to build the EXE, even for just a few files that you care about. You’ll need to locate those files and open them in Visual Studio. If the source code isn’t the exact same as the source code that was built the EXE Visual Studio will warn you when you try to insert a breakpoint and the breakpoint won’t bind. That behavior can be overridden from the Breakpoint Settings peek window. In the settings peek window click on the Must match source link text and then check the box to allow mismatched source, as illustrated below. Of course, with mismatched source you never really know what’s going to happen, so use at your peril.
Disclaimer: I'm not really sure how this method is effective without source files, but I think it's the closest to your answer without changing the actual program.
As another option, you can try invoking your executable file by creating a custom program that invokes the program and redirects its stdout and stderr, like this, for example, or using pipes, check there. The custom program would eventually create a process using as executable your original .exe program and do something with stdout/stderr, for example showing them in your custom program console or saving the output to a file, thus allowing you to read your original program output without the window closing issue (Of course, the original window will still close itself, but I don't think it would matter for you too much).
Or if you want a quick'n'dirty way, you could try to capture the program output by making an invoker Java program. Replace commands array with your program executable name with arguments:
Runtime rt = Runtime.getRuntime();
String[] commands = {"system.exe", "-get t"};
Process proc = rt.exec(commands);
BufferedReader stdInput = new BufferedReader(new
InputStreamReader(proc.getInputStream()));
BufferedReader stdError = new BufferedReader(new
InputStreamReader(proc.getErrorStream()));
// Read the output from the command
System.out.println("Here is the standard output of the command:\n");
String s = null;
while ((s = stdInput.readLine()) != null) {
System.out.println(s);
}
// Read any errors from the attempted command
System.out.println("Here is the standard error of the command (if any):\n");
while ((s = stdError.readLine()) != null) {
System.out.println(s);
}

- 711
- 1
- 6
- 22