8

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.

1
  • 1
    If it really helps, add a link to the picture and I'll edit your post. Commented Oct 5, 2015 at 15:09

6 Answers 6

12

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

enter image description here

Sign up to request clarification or add additional context in comments.

7 Comments

I am adding my file as a programme argument, however it doesn't work. :-(
@aevitas You need to add the full path.
I am using the same path that I would enter in the terminal. So, I'm dragging the file to the terminal and then copy that path to IntelliJ.
@aevitas are you able to run it from terminal? What do you mean by dragging into terminal?
Yes I am. Dragging the file from a finder window into the terminal in order to obtain the full file path. :-)
|
5

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.

1 Comment

from this OSX 10.11, I am guessing you are running on Mac OS. On the Mac the absolute starts with /Users/username/<rest_path>, so Computer/project/TestInput/itcwww-small.txt is not a absolute path.
2

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

Comments

1

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

Comments

1

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

1 Comment

enter image description here ?
0

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

3 Comments

I am adding my file as a programme argument, however it doesn't work. :-(
Use an absolute path.... check it. It should works. With a relative path your need to check the position of your file. It must be present in the working directory.
I am using the same path that I would enter in the terminal. So, I'm dragging the file to the terminal and then copy that path to IntelliJ.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.