Java 使用命令行文件输入从 IntelliJ 运行程序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32951846/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 14:04:51  来源:igfitidea点击:

Run Program from IntelliJ with Command Line File Input

javaintellij-idea

提问by

I am running OSX 10.11 with IntelliJ 14.1.15.

我正在使用 IntelliJ 14.1.15 运行 OSX 10.11。

I have a programme which takes a txt file as an argument. I can run it from the terminal through java SearchCmd test.txtand then it allows me to enter a search term and searches that list.

我有一个以 txt 文件为参数的程序。我可以通过java SearchCmd test.txt从终端运行它,然后它允许我输入搜索词并搜索该列表。

How do I do this from within IntelliJ, so that I can click the run button and it reads the file and I am able to enter a search term in the IntelliJ console.

我如何在 IntelliJ 中执行此操作,以便我可以单击运行按钮并读取文件,并且我可以在 IntelliJ 控制台中输入搜索词。

The main class 'SearchCmd' contains the main method, as such:

主类“SearchCmd”包含主要方法,如下所示:

public class SearchCmd {

public static void main (String[] args) throws IOException {
    String name;

    // Check that a filename has been given as argument
    if (args.length != 1) {
        System.out.println ("Usage: java SearchCmd <datafile>");
        System.exit (1);
    }

    // Read the file and create the linked list
    HTMLlist l = Searcher.readHtmlList (args[0]);
}

However, when I try and run this it says: "Usage: java SearchCmd ".

但是,当我尝试运行它时,它说:“用法:java SearchCmd”。

In order to pass the test.txt file to IntelliJ, I entered the file path in the 'Run/Debug Configurations'. Sadly I can't attach the picture. :-(

为了将 test.txt 文件传递​​给 IntelliJ,我在“运行/调试配置”中输入了文件路径。 可惜我不能附上图片。:-(

Any help on fixing this and helping me run it from IntelliJ will be greatly appreciated.

任何有关解决此问题并帮助我从 IntelliJ 运行它的帮助将不胜感激。

采纳答案by Prince

I just figured it out.

我只是想通了。

So instead of pasting in an absolute path, you need to paste a relative path from the root directory of your IntelliJ project. And most importantly you have to ommit the initial forward slash.

因此,您需要从 IntelliJ 项目的根目录粘贴相对路径,而不是粘贴绝对路径。最重要的是,您必须省略初始正斜杠。

So my absolute path to the file might be this: Computer/project/TestInput/itcwww-small.txt

所以我的文件绝对路径可能是这样的:Computer/project/TestInput/itcwww-small.txt

But the path that I need to put into Programme Arguments is: TestInput/itcwww-small.txt

但是我需要放入 Program Arguments 的路径是:TestInput/itcwww-small.txt

I hope that this will help someone else.

我希望这会帮助别人。

回答by Davide Lorenzo MARINO

You need to go in the menu Run -> Edit configurations.

您需要进入菜单Run -> Edit settings

Select your configuration and add the parameters in the field Program arguments

选择您的配置并在程序参数字段中添加参数

The field Program argumentsis what appears after the class name from command line. For example:

字段Program arguments是命令行中出现在类名之后的内容。例如:

java MyMainClass ProgramArgument1 ProgramArgument2 ProgramArgument3

in your example

在你的例子中

java SearchCmd test.txt

the program argument is test.txt

程序参数是test.txt

Note:Use an absolute path or check that the working directory is the directory containing your file

注意:使用绝对路径或检查工作目录是否是包含您的文件的目录

回答by YoungHobbit

Go to Run-> Edit Configurations, Select Application, then give the main classname and program arguments. Then Run.

转到Run-> Edit Configurations,选择Application,然后给出main class名称和program arguments。然后Run

enter image description here

在此处输入图片说明

回答by Prince

Steps to follow-
1. Run->Edit Configurations.
enter image description here

2. Select Application.

enter image description here

3. Provide main class name and command line arguments and apply.

4. Run

要遵循的步骤-
1. 运行->编辑配置。 2. 选择应用程序。 3. 提供主类名和命令行参数并应用。 4.运行
在此处输入图片说明



在此处输入图片说明



回答by Nícolas Schirmer

I had the same issue and was very confused with the previous answers of this question, so here is my explanation to anyone that is lost like I was.

我遇到了同样的问题并且对这个问题的先前答案感到非常困惑,所以这是我对像我一样迷路的人的解释。

With the project open.

随着项目的打开。

Run > Edit Configurations....

运行 > 编辑配置....

edit configuration

编辑配置

Add the whole directory that the file is in it on the Program Argumentsfield with the file format at the end.

将文件所在的整个目录添加到Program Arguments字段中,并在末尾添加文件格式。

enter image description here

在此处输入图片说明

回答by CodeFarmer

Adding the input file name's relative path in the "Program Arguments" will allow the "main" method reading the argument in as a "String"

在“程序参数”中添加输入文件名的相对路径将允许“main”方法将参数作为“字符串”读取

Config

配置

However, in order to actually let the System to understand using data from the file as standard input, it seems we have to specifically read the file and set the input stream as the system input :

但是,为了真正让系统理解使用文件中的数据作为标准输入,似乎我们必须专门读取文件并将输入流设置为系统输入:

    try {
        FileInputStream inputStream = new FileInputStream(new File(args[0]));
        System.setIn(inputStream);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }