如果我在 NetBeans 中运行 Java 程序并按照输出窗口中给出的信息从命令行运行:
To run this application from the command line without Ant, try:
java -jar "C:\Users\erdik\OneDrive\Documents\Computing Science Degree\Course Folder\Year 1\Programming 1\Assignment 2 - Year 2 Edit\assignment2\dist\assignment2.jar"
程序开始运行,但是当需要运行以下代码来打开 .txt 文件(我的“数据库”)时:
System.out.println("Loading database of stored transactions...");
try
{
file = new File("TransactionDetails.txt");
inFile = new Scanner(file);
}
// if the log couldn't be found in the default program location
catch (FileNotFoundException ex)
{
System.out.println(CustomMessages.FileNotFound() +
System.getProperty("user.dir")); // display default directory
System.out.println(CustomMessages.systemExit());
System.exit(1); // the program needs the log to function as intended
}
它找不到 .txt 文件,并将默认目录打印为 Windows System32 文件夹。如何按预期指定项目文件夹的位置?
最佳答案
您可以使用文件的绝对路径而不是相对路径。例如
file = new File("C:\Users\erdik\OneDrive\Documents\Computing Science Degree\Course Folder\Year 1\Programming 1\Assignment 2 - Year 2 Edit\assignment2\dist\TransactionDetails.txt");
inFile = new Scanner(file);
关于java - 来自命令行的 NetBeans Java 项目 : Working directory is System32,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40326250/