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);
Related
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.
When we click on .jar files which do not contain any GUI the CMD Prompt runs the code and exits it immediately.
How to make it remain open when running a .jar by double clicking on it?
Is there any java code for it? Just like the pause command in batch files or any? I prefer only by java code but not an OS way? and also not by running command java - jar *.jar in cmd prompt.
I'd usually just open a command line first, as suggested in the other answer.
If you need a pure Java code solution though, just read from system in at the end of your code. The window will stay open while waiting for input.
Run the jar file from the command prompt
java -jar yourjarfilename.jar
Something along the lines read a character from std-in along with a print statement telling people to "press any key" should do the trick, provided you have access to the java code that is.
if not well, best option is to drop to CMD and run the command manually.
I'm trying to do something very simple here, but it isn't working.
Basically I have a program and an input file sitting at a certain directory, let's call it "programDir".
I'm writing a plugin in Eclipse that will call this program and run it on the input file.
Essentially two steps must be done: 1) cd to programDir 2) run the program by calling "idp input.txt"
I have done this manually in the cmd and it works as expected. However in Java I can't get it to work. I tried 2 approaches:
Runtime rt = Runtime.getRuntime();
Process proc = rt.exec("cmd.exe /c cd \""+programDir+"\" & \"idp input.txt\"");
Here I get the following from the errorStream: "idp output.txt" is not recognized as an intern or extern command, program or batch file
I'm not sure why. I have left out the second part of the command to ensure that I am in the correct location. When i add "start", a console window pops up and it is in the programDir folder. If I then manually type "idp input.txt" I get the expected behavior.
Second approach:
I also used ProcessBuilder after some googling on the subject. I tried this piece of simple code:
String[] command = {"CMD", "/C", "idp input.txt"};
ProcessBuilder pb = new ProcessBuilder(command);
pb.directory(new File(programDir));
Process proc = pb.start();
Now nothing happens and the program does not terminate.
Again, if I add "start" as one of the parameters in "command", a console window pops up in the right location and if I then manually type "idp input.txt", it works. So I have no idea why the code doesn't work.
One interesting thing: the idp.bat file calls a kbs.exe process. When I run the second piece of code, no kbs.exe appears in my task manager. However, as soon as I terminate the program, it briefly makes an appearance. Does that mean my code gets stuck in a loop somewhere or something?
Any help appreciated!
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.
I have opened a commnad prompt using java program
and another command prompt manually. Now my requirement is that i need to close the command prompt that i have opened by java program using the program.
I tried to close this by giving rt.exec("taskkill /IM cmd.exe");
But the problem is that the command prompt that i have opened manually is aslo closed which i dont want.
Help needed.
Thanks in advance
if you're using Runtime.getRuntime().exec(), it returns a Process object. You should hold on to that and call Process.destroy() when you're done.
It's simple -
Step 1 - Create a batch file (say closeCMD.bat)
Step 2 - write exit in closeCMD.bat
Step 3 - call above batch file in your java code as below -
Runtime.getRuntime().exec("cmd /c start closeCMD.bat");
That's it !! Cheers!!
you can try to program "exit" into the command prompt after you are done with your tasks in command prompt