Title: JAVA command line: "file not found exception" when I enter two-words-long command? (when I enter one-word-command, it works fine.)
Tags: java;terminal;system
Question: my code aims to provide 1) simple, and 2) detailed type answers to a command that I type on Mac command line, depending on the command that I enter (I want to distinguish by the number of words I enter, however, the command line only executes for one-word-long command, and throws the exception for two-words-long command).
When I enter java <filename&gt; <input&gt;, I want it to produce a simple version, which I programmed, and when I enter java <filename&gt; --verbose <input&gt;, I want it to produce a detailed version, which I have also programmed.
The simple version works fine, but the detailed version throws an error, indicating the error involves scanner. Here is the snippet of the code (just for the reference. Here is the command line output.
public class Test {
public static Scanner scan;
public static void main(String args[]) {
//To check the length of args --&gt; I take "check" array as an input for my method "eachCycleFCFS".
for (int a = 0; a < args.length; a++) {
check.add(args[a]);
try {
String fileAddress = args[0];
File fileInput  = new File(fileAddress); //Read
scan = new Scanner(fileInput);
int numProcesses  = scan.nextInt();
...
for (int m = 0; m < numProcesses; m++) {
int a = scan.nextInt();
int b = scan.nextInt();
int c = scan.nextInt();
int io = scan.nextInt();
...
}
}
catch (Exception e){
e.printStackTrace();
System.out.printf(" Error: File not foundd. \n");
public static void eachCycleFCFS (Queue<Process&gt; processes, int numProcesses, Process[] allProcesses, Process[] original, Process[] realOriginal, ArrayList<String&gt; check) {
File fileInput = new File("random-numbers.txt");
Scanner randomInput = null;
try {
randomInput = new Scanner(fileInput);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
if (check.size() == 2) {
if (check.get(0).contains("verbose")) {
//Produce the detailed output
}
else {
//Produce the simple output
while (terminatedProcesses != numProcesses) {
if (check.size() == 2) {
if (check.get(0).equals("--verbose")) {
//Produce the detailed output
}
}
For the one-word-long command, the code should produce the simple output that I programmed. For the two-word-long command, the code should produce one more block of information on top of the simple output.
The simple version is fine.
The detailed version says this:
blahblahblah$ java Scheduling2 --verbose input-1.txt
java.io.FileNotFoundException: --verbose (No such file or directory)
at java.io.FileInputStream.open0(Native Method)
at java.io.FileInputStream.open(FileInputStream.java:195)
at java.io.FileInputStream.<init&gt;(FileInputStream.java:138)
at java.util.Scanner.<init&gt;(Scanner.java:611)
at Scheduling2.main(Scheduling2.java:18)
Error: File not found.
Thank you for your help and let me know if I can add more information to make it easier for you, please :)!
Comment: `check.get(0).contains("verbose")` first of all, according to your example the first argument (0) is always the filename and secondly what if the filename contains the word "verbose"? So this code is double bad. May I suggest you start by doing some proper validation of the program arguments so you from the start knows what you have and preferable assigning each argument to its own variable like `String filename` and `boolean isVerbose` and pass them as arguments to other methods when needed instead of being dependent of the order of items in an array which leads to very fragile code .

Comment: If your command line will contain a argument that happens to be a string which contains whitespaces then enclose that argument in Quotation Marks (`"My Command", myOtherCommand, "And Yet another Command"`).

Comment: Oh I get what you mean.. it is because I name every argument as "fileaddress". Do you know how I can modify my code? I have no idea how to fix this issue... and thank you for picking it out! really helps.


Here is another answer: Thing is you always take filename from your fist argument:
String fileAddress = args[0];
File fileInput  = new File(fileAddress); //Read
But in this case your first argument (e.g. args[0]) is "--verbose". So new File("--verbose") fails with FileNotFoundException, because indeed file "--verbose" doesn't exist in current directory.
What you probably want to do there is skip arguments that start with --, e.g.
int argNum = 0;
while(argNum<args.length &amp;&amp; args[argNum].startsWith("--")) {
argNum++;
if(!(argNum < args.length)) {
throw new IllegalArgumentException("Please pass file path in parameters");
String fileAddress = args[argNum];
File fileInput  = new File(fileAddress); //Read

{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 1, 'CommentCount': 3, 'ContentLicense': 'CC BY-SA 4.0', 'CreationDate': '2019:00:03/24/19', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<java><terminal><system>', 'Title': 'JAVA command line: "file not found exception" when I enter two-words-long command? (when I enter one-word-command, it works fine.)', 'ViewCount': 296, 'id': 55320295, 'user_AboutMe': None, 'user_AccountId': 15556746.0, 'user_DisplayName': 'Alex Kang', 'user_Reputation': 11.0, 'user_Views': 17.0, 'user_id': 11222953.0}}