我已将以下代码添加到我的程序中:
Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {
@Override
public void run() {
System.out.println("exit");
}
}){});
Run Code Online (Sandbox Code Playgroud)
但是,我没有看到这个消息.附加信息:我从Netbeans IDE内部运行程序.
编辑:我忘了添加有一个全局线程,使程序保持活着.我按下Netbeans右下角的[x]来关闭它.
import java.io.FileNotFoundException;
import java.util.Formatter;
import java.util.FormatterClosedException;
import java.util.NoSuchElementException;
import java.util.Scanner;
public class CreateTextFile
{
private Formatter formatter;
public void openFile()
{
try
{
formatter = new Formatter("clients.txt");
}
catch (SecurityException securityException)
{
System.err.println("You do not have permission to access this file");
System.exit(1);
}
catch (FileNotFoundException fileNotFoundException)
{
System.err.println("Error opening or creating the file");
System.exit(1);
}
}
public void addRecords()
{
AccountRecord accountRecord = new AccountRecord();
Scanner scanner = new Scanner(System.in);
System.out.printf("%s%n%s%n%s%n%s%n", "To terminate input, type the end-of-file indicator", "when you …
Run Code Online (Sandbox Code Playgroud) 我只是想知道.我需要System.exit(0);
在main
Java命令行应用程序的方法结束之前调用吗?如果是这样,为什么?如果我只是一直放在那里,让它自己退出有什么区别0
?有什么不清理的?
提前致谢.
我对system.exit有点困惑.我从这个链接创建了一些东西.
但我心里有些疑惑.如果我使用系统退出,创建的对象,变量和ect会发生什么.一旦我调用system.exit,一切都会被破坏吗?如果"是"那么为什么我们强制在system.exit()之前进行垃圾收集?如果"否"创建的对象存储在JVM(内存)中多长时间?如果在退出系统后再次运行程序,如果在我调用System.exit()之后它们没有被销毁,前一个对象会发生什么?
谢谢.
我对Stackoverflow和Java相对较新,但我对C有一点经验.在'exit()'函数出现故障后,我喜欢退出程序的非常干净的方式.
我在Java中找到了一个类似的函数System.exit(),它与C函数有什么不同,什么时候应该使用'System.exit()'而不是像在void main函数中那样在Java中使用简单的'return'?
我们正在使用Java开发Web应用程序。我想使用System.exit()
我的一种方法而不是返回。据我所知,如果我使用该应用程序,它将进入关闭模式。
有人可以给我建议吗?
我目前正在使用system.exit(1)维护一个在发生故障时关闭的系统.不幸的是,当发生这种情况时,系统的java进程仍然存在.
调用system.exit时jvm怎么可能没有关闭?
打电话的最佳做法是System.exit(0)
什么?
我是故意打电话的System.exit(0)
.什么是正确的打电话方式System.exit(0)
?
我应该创建一个调用的方法System.exit(0)
吗?
谢谢.
我希望在输入时终止我的程序5
.
按下5时我的程序没有关闭.
代码段:
System.out.println("What do you want to do?");
System.out.println("1. Go north");
System.out.println("2. Go east");
System.out.println("3. Go south");
System.out.println("4. Go west");
System.out.println("5. Quit");
System.out.print("Enter command (1-5): ");
// get and return the user's selection
System.exit(0); // exit the menu
return s.nextInt();
Run Code Online (Sandbox Code Playgroud)
如何更改任何建议?