How to Stop Closing Command Prompt - java

Suppose If we create a bat file to run java program which prints "Hello World" , like this
javac MyProgram.java
java MyProgram
After when I Double click that bat file It opens Command prompt and displays "Hello World" result and automatically closes. Is there any solution to not to close Command prompt until and unless I type Exit in it.
Thanks.

If you want to make the window remain for a while, there are 3 ways:
Run that command javac MyProgram.java java MyProgram in a Command Promptwindow (fire up a new one, navigate to your working directory, execute it)
Get a char (Scanner a; ... ; a.nextLine()) at the end of your program, you can then enter something or simply press a to make the program complete.
Make the current thread sleep for some time so that you can see the output. Try: try { Thread.Sleep(1000); } catch (Exception e) {}

It happens because at the end of the day it is a program. And it terminates once it is done with its execution..in your case, printing stuff.
You can manually open a command prompt and then drag and drop the .bat file onto this command prompt and run it. This way, you would still have the window open.

Related

Java commands flash a cmd

I'm new to Java. I already installed Java, JDK and added it to Path, but when I try to run a command (like "java -version") it just flashes a CMD but doesn't show what I want to see. This happens with every command and doesn't even let me run Java code using an IDE. There is no error message, just a CMD flashing every time I try to run something (it also happens when double click the files like "java", "javac", etc).
How do you run these commands? Try running them from an interactive terminal window. Open it by typing windows key + R and then cmd. Then cd into your directory and type your commands there. Alternatively, if your commands are in a .bat file … you may add a pause command at the end.
This is entirely expected as java -version immediately exits after printing the version. This means that if you didn't start it from cmd, PowerShell, or another interactive shell, it opens a window which disappears immediately because the program ended in a fraction of a second.
Start cmd (Command Prompt) or PowerShell (either directly or through Windows Terminal), and run your commands in that interactive shell.

batch file pauses if command window not selected

I have a .bat-file that executes the following commands:
cls
#pushd %~dp0
#echo off
java -Dmypath=path -jar ./my_directory/myjava_program.jar arg1 arg2
#popd
#pause
In addition to that I have this restraining requirement: The batch file needs to be executed by clicking on it. No execution with additional commands in the command line is possible.
The problem:
The script runs fine if it is executed in the foreground. But as soon as I change to another program, the output in the command window will pause. If I click ENTER in the command window the script will continue again.
What is the reason the script pauses?
priority given for execution of other program?
something with the read line support (I read about this issue in a couple of related posts)?
...
And what could be a solution to keep the script running even if I don't have the command window selected? (It can still be visible though)
Try to right click on Command window, select properties, and uncheck "QuickEdit Mode" if it is checked. May have to close/reopen dos window to persist that setting.

Keeping terminal open when running a bash script from a desktop shortcut

I am running a bash script from my desktop off of a shortcut I made for it. When I click on it it says "Execute in Terminal" which is the option I want to do. The bash script compiles and runs a java program I made and the point of the script is to handle the errors that the program may through through the compiling prosses to someone who has very little programming experience (not really important to the question though). When I launch it, it will open the terminal as expected. When it encounters the error it will print out the error to the screen as one would expect it to do but then will immediately close the terminal it opened.
What I want is for the terminal to stay open until someone exits the terminal so they can read the errors.
I suggest you to add a line of code at the end of your bash script with a read operation as follows:
read -p 'Hit ENTER to exit'
This will keep the terminal visible until you hit ENTER.

Press any key to continue not working?

Okay, I'm running an executable .jar file, and something kinda funky is happening. The jar opens the command prompt (Windows 7, cmd) but that's supposed to happen. After everything finishes I get the:
Press any key to continue . . .
However, when I press a key, it just keeps the command prompt open, instead of closing it.
In my code, I have this to run the command prompt:
String fileName = "File Location";
Process p = Runtime.getRuntime().exec(new String[]{"cmd","/k","start",
"cmd","/k",fileName});
p.waitFor();
Is there something in this I could modify to fix it so that way the "Press any key to continue" actually allows me to exit without having to type "exit" next?
Edit: There's a pause at the end of my. Is this causing the problem?
You start a new cmd prompt and execute something in it.
How on earth do you expect the cmd to know that it should terminate after executing the command? You have to put an "exit" in the script you are running if you want the cmd to close.
Or try running the cmd with option /C.
Putting the following at the end of you code should exit the window
System.exit(1);

Executable Jar not Displaying Output

I have an executable jar that I know is executing, because I put two distinct beeps at the beginning of the code that I hear whenever I double click on it or when I run it through the cli. Even when I run it through the command line however, it does not display output nor prompt for input when I use System.out/System.in respectively. Everything is functional when I run it through eclipse.
How do I get the .jar to output/input to the same command line I executed it in?
Assuming java is in your PATH, you should lauch your program as follows :
java -jar myProgram.jar
If you want to run it without opening the command line, you could create a .cmd file in the same directory with this content :
#echo off
java -jar myProgram.jar
pause
then, double-click on the .cmd file.

Categories

Resources