我正在尝试使用 Desktop.getDesktop().open(file) 从 .jar 文件启动文本文件
String fileName = "file.txt";
URL url = getClass().getResource(fileName);
File fileToRead = new File(url.toURI());
Desktop.getDesktop().open(fileToRead);
为了简单起见,我省略了 try-catch block 。 从 eclipse 运行时它能够打开我的文件。但是,一旦导出到 .jar 文件,我就会在 File fileToRead = new File(url.toURI());
中收到 NullPointerException最佳答案
当您将类打包到 .jar 文件中时,通常会使其嵌套更深一层。
因此,您可以尝试将第一行更改为:
String filename = "../file.txt";
关于java - 从 .jar 文件启动文本文件(文件路径?),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33544344/