I have a java program which prints out the last statement of main program and hang. I would like to know what could cause this problem thanks.
public static void main(String[] args) {
...
...
JSch jsch=new JSch();
try {
jsch.addIdentity("xxx");
session = jsch.getSession("centos", this.ip, 22);
session.setConfig("StrictHostKeyChecking", "no");
session.connect();
} catch (JSchException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
...
...
System.out.println("end of test");
}
the program prints "end of test" and hang. what could cause this problem? I didnt use any framework. I compiled and run this in linux machine. after printing the "end of test", the linux prompt ">" doesnt show up
from ps command, I see the process is running.
i found the problem. the session conection doesnt get disconected. but why it blocks the exit? correct me if i am wrong. there is no join or wait inside main thread. can main thread get blocked?
Answer: with Stephen C 's help, finally, i found the reason. in c++, all threads are terminated after main thread exit, but in java, JVM wait for all non-Daemon thread to complete. I applied my c++ knowledge to Java but they are different in this case.
One possibility is that you have created another thread, and that thread hasn't terminated yet. The JVM won't shut down of its own volition until all (non-daemon) threads have terminated.
Another possibility is that you have implemented a shutdown hook, and the hook has blocked.
From ps command, I see the process is running and single thread.
That evidence is consistent with either of the above, I think.
Can we troubleshoot from outside (such as ps command) instead of source code.
Try using jstack
(I think it is called) to get a thread stack dump.
Try attaching a debugger to the JVM.