Reputation:
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
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"
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
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....
Add the whole directory that the file is in it on the Program Arguments field with the file format at the end.
Upvotes: 2
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
Reputation: 2287
Steps to follow-
1. Run->Edit Configurations.
2. Select Application.
3. Provide main class name and command line arguments and apply.
4. Run
Upvotes: 1
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
Reputation: 13402
Go to Run
-> Edit Configurations
, Select Application
, then give the main class
name and program arguments
. Then Run
.
Upvotes: 12