user3735005
user3735005

Reputation:

Run Program from IntelliJ with Command Line File Input

I am running OSX 10.11 with IntelliJ 14.1.15.

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

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.

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

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 ".

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. :-(

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

Upvotes: 8

Views: 47071

Answers (6)

CodeFarmer
CodeFarmer

Reputation: 11

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

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();
    }

Upvotes: 1

N&#237;colas Schirmer
N&#237;colas Schirmer

Reputation: 1444

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 Arguments field with the file format at the end.

enter image description here

Upvotes: 2

user3735005
user3735005

Reputation:

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.

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

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

I hope that this will help someone else.

Upvotes: 5

RockAndRoll
RockAndRoll

Reputation: 2287

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

Upvotes: 1

Davide Lorenzo MARINO
Davide Lorenzo MARINO

Reputation: 26946

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

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

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

java MyMainClass ProgramArgument1 ProgramArgument2 ProgramArgument3

in your example

java SearchCmd test.txt

the program argument is test.txt

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

Upvotes: 0

YoungHobbit
YoungHobbit

Reputation: 13402

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

enter image description here

Upvotes: 12

Related Questions