有 Java 编程相关的问题?

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

java在Eclipse中运行代码并提供额外输入

在我的CS类中,我们使用终端来运行代码,使用javac来运行cimpile,使用java来运行。 在我当前的任务中,我从一个文件接收输入。在CMD(终端)中输入命令时,会给出文件名,如下所示: “java-cp mypath\classtext.txt” 使用此代码:

if (args.length != 1) {
        final String msg = "Usage: EmployeePay name_of_input file";
        System.err.println(msg);
        throw new IllegalArgumentException(msg);
    }
    final String inputFileName = args[0];
    final File input = new File (inputFileName); 
    final Scanner in = new Scanner (input);

在Eclipse中运行时,它会抛出异常。eclipse中有没有一种方法可以在不更改或添加任何代码的情况下执行这个命令“java-cp mypath\classtext.txt


共 (1) 个答案

  1. # 1 楼答案

    转到Run/Run configurations...,然后选择左侧的“运行实例”,然后选择Arguments选项卡,最后输入文本。txt进入Program arguments字段。单击Apply,并对其他“运行实例”(如果有)重复该操作

    如果你想运行你的项目,你可以点击Run,但是不要忘记提前点击Apply