有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

异常处理Java显示错误NoSuchFileException

我正在尝试为巡航控制系统开发一个java应用程序。但我有一个问题,没有这样的文件例外。因为我对java编码有点陌生。我不知道为什么会出现这个问题。我的代码是-

 public static void main(String[] commandLineArgs) throws IOException {
    Path input_path = Paths.get(commandLineArgs[0]);
    List<InputState> input_states = StateInput.input_states_from_file(input_path);
    Timer timer = new Timer(new CruiseControlSystem());
    List<OutputState> output_states = timer.pulse_from_input(input_states);
    for (OutputState s : output_states){
        System.out.println(s.format());
    }
}

我发现的错误是-

Exception in thread "main" java.nio.file.NoSuchFileException: commandLineArgs[0]
at sun.nio.fs.WindowsException.translateToIOException(Unknown Source)
at sun.nio.fs.WindowsException.rethrowAsIOException(Unknown Source)
at sun.nio.fs.WindowsException.rethrowAsIOException(Unknown Source)
at sun.nio.fs.WindowsFileSystemProvider.newByteChannel(Unknown Source)
at java.nio.file.Files.newByteChannel(Unknown Source)
at java.nio.file.Files.newByteChannel(Unknown Source)
at java.nio.file.spi.FileSystemProvider.newInputStream(Unknown Source)
at java.nio.file.Files.newInputStream(Unknown Source)
at java.nio.file.Files.newBufferedReader(Unknown Source)
at java.nio.file.Files.readAllLines(Unknown Source)
at StateInput.input_states_from_file(StateInput.java:31)
at CommandLine.main(CommandLine.java:23)

我的主要方法是-

public static void main(String[] commandLineArgs) throws IOException {
    Path input_path = Paths.get(commandLineArgs[0]);
    List<InputState> input_states = StateInput.input_states_from_file(input_path);
    Timer timer = new Timer(new CruiseControlSystem());
    List<OutputState> output_states = timer.pulse_from_input(input_states);
    for (OutputState s : output_states){
        System.out.println(s.format());
     }
 }

image of run configuration


共 (3) 个答案

  1. # 1 楼答案

    My main method is:

    Path input_path = Paths.get(commandLineArgs[0]);
    

    不,不是。您正在传递"commandLineArgs[0]"作为参数。你的意思是commandLineArgs[0],没有引号

  2. # 2 楼答案

    程序参数文本框中,您当前有“命令行参数[0]”。您需要将“命令行args[0]”更改为包含输入状态的文件路径。类似于“/data/input states”的内容

    enter image description here

  3. # 3 楼答案

    运行程序时,您没有向main方法传递任何参数

    您还可以添加如何运行此功能