<< JLink`;
Clear[serverSocket, socket];
serverSocket = JavaNew["java.net.ServerSocket", 12345];
socket = serverSocket@accept[];

If I enter Alt+.,the program can not stop.

However in Eclipse,I can press enter image description here("Terminate" button) to stop the running java program.

I want to know how to stop this program in Mathematica.

  • $\begingroup$ Could you reduce your problem to a much smaller minimal working example? As it stands, I am afraid that few people will have the time and patience to wade through pages of code to understand your question. $\endgroup$ – MarcoB Feb 14 '16 at 6:09

I don't know much Java, so I am only going to post the link to the documentation.

Check page 104 here.


If you are writing a method that may take a while to complete, you should consider making it interruptible from Mathematica. In C MathLink programs, a global variable named MLAbort is provided for this purpose. In J/Link programs, you call the wasInterrupted() method in the KernelLink interface:

public boolean wasInterrupted();

You need to write your java programs specifically to be interruptable. The wasInterrupted method will return true if Alt+. was pressed. You must check for this condition yourself in your Java program and stop the program when appropriate. Mathematica won't be able to automatically kill the Java program without that program explicitly supporting this.

| improve this answer | |
  • $\begingroup$ Note: I do not use J/Link, so unfortunately I cannot go into details on this. I found this information by searching for MLAbort, which is the way to do the same from the C MathLink interface (J/Link is just the Java interface for MathLink). $\endgroup$ – Szabolcs Feb 14 '16 at 9:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.