Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1319
  • Last Modified:

How to kill a Java program with an infinite loop?

I have a Java class called TestThread.java. Below is the code. I have a Windows batch file that will run the class file. When I run the batch file, "Hello world!" will be printed out every 30 seconds.

I then modified the java file to print out "Goodbye everyone!" instead of "Hello world!", compiled it, and then replaced the original class file with the new one. Now, in addition to "Hello world!" being printed out every 30 seconds, "Goodbye everyone!" is also printed out every 30 seconds.

How do I kill the Java program that keeps printing out "Hello world!"?

This is a simplified explanation of my actual program that doesn't really print out anything and is called from the Windows Scheduled Task, so "CTRL+C" is not an option.
public class TestThread {
	public static void main(String[] args) {
		try {
			while (true) {
				System.out.println("Hello world!");
				Thread.sleep(30000);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

Open in new window

0
killdurst
Asked:
killdurst
  • 3
  • 3
1 Solution
 
wwnosalCommented:
Alt + Ctrl + Del

In task Manager  select this process and press End process. (It might be listed as java.exe)
0
 
killdurstAuthor Commented:
Thanks. That works on my PC, but on the application server, I can't find java.exe in the process list. Is there another way to see the list of running process and then kill the ones I don't want?
0
 
wwnosalCommented:
On which platform this application server is running. Is it windows? Unix? Linux?

0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
killdurstAuthor Commented:
Windows.
0
 
wwnosalCommented:
tasklist.exe to list processes

taskkill.exe /PID pid_read_from_previous_command
0
 
killdurstAuthor Commented:
Thanks man. Going back to work in around 9 hours time. Gonna try that out. Will get back to you soon if everything works out alright.
0
 
mbodewesCommented:
If you see more java or javaw commands, you might want to try and use the new jmonitor (GUI) or jps (command line) tools included in the latest JDK's. This will actually tell you what the java or javaw is doing, and you can kill the correct process using the process ID (PID) using the task manager. Always try <ctrl><break> first for command line applications, because sending a signal to an application allows it to exit more gracefully.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now