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
Related
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.
Let's say I have a java program and I write this to the command line:
java javafile.jar
Is there anyway to start the java file minimized (somthing similar to /min command)? I need to do it through the commands line.
try start /min java javafile.jar
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);
I currently have the following batch script I want to run from my Java program:
"C:\Program
Files\Java\jdk1.6.0_25\bin\java.exe"
-classpath "D:..."
Main >
"...\result.out"
Now, I've done a simple
Runtime.getRuntime().exec(command);
where command is that string I have shown above. The problem is that it is simply calling java.exe with the shown arguments, instead of calling the console with the given arguments. The difference is subtle, for if it is calling directly java.exe it will ignore the redirect of the output stream!
Is there a easy way to do this? I've tried prefixing command with "cmd " but that didn't seem to help.
I'd like to stay away from having to read the output stream and then having to manually save this to a file.
Thanks
To solve the issue,
cmd /c "command"
is enough.
Process proc = Runtime.getRuntime().exec("acpi -b");
Now you can use proc.getInputStream() and proc.getOutputStream() like any normal input and output streams.
You can then write the contents to your output file.
This is the method I mostly use.
I know how to start the batch file using java code.
When i run the batch file command prompt is opened.
To close the command prompt i am using the taskill /im cmd.exe.
but the problem is that the command prompt that is used to start the jboss is also closed.
i want to kill the cmd with a particular process id.
How do i get the process id of a particular cmd promt and kill that using java
Run the batch file with cmd.exe /c job.bat. The /c switch carries out the command and then terminates the command interpreter.
can't you add exit to your batch file to exit it's own command prompt. Uisng taskill seems overkill for just closing one command prompt, don't you think?
PS: I've never worked on batch files just the command prompt so I'm assuming it accepts the same commands.
Here is the solution that works: to close command window after executing the commands from the batch (.bat) file you need to add "exit" (without the quotes) in a new line of your batch file. If you want to delay the execution this is the way and it works:
public class TestSleep
{
public static void main ( String [ ] args )
{
System.out.println("Do this stuff");
try
{
Thread.currentThread().sleep(3000);
}
catch ( Exception e ) { }
System.out.println("Now do everything after this");
}
}
Cheers
If you start the batch file with Runtime.exec(), it returns you a Process object. Calling the destroy() method will kill that process.
Although this seems to be an old question and probably resolved.. I struggled with the same thing for a long time.. Finally this works
String command = "cmd.exe /c build.bat";
Runtime rt = Runtime().getRuntime();
Process pr = rt.exec(command);
I too had the same problem. I first used as
Runtime.getRuntime().exec("cmd.exe start /c test.bat");
Then I tried as below. It works fine.
Runtime.getRuntime().exec("cmd.exe /c start test.bat");
Try this.