wadda_wadda
wadda_wadda

Reputation: 1004

Command-Line Arguments Being Lost?

I'm writing a Java program to clean some data.

I'm passing it the files I need, but the first file is ignored!

Minimum code to reproduce the issue:

public class Classifier {
  public static void main(String[] args) throws IOException {
    System.out.println(args[0]);
    for (String s : args) {
      System.out.println(s);
    }
  }
}

I'm running it with the following Command-Line Argument:

java Classifier < March.csv February.csv

And the output I'm receiving is:

February.csv February.csv

Can someone explain why this is?

Upvotes: 0

Views: 91

Answers (2)

Jigar Joshi
Jigar Joshi

Reputation: 240898

< operator redirects this file to stdin to this Java process

so

if you just have

java Classifier < March.csv

and try to read arguments, you would see none and if you read stdin you would read the file content

Upvotes: 1

rgettman
rgettman

Reputation: 178263

The < March.csv is being interpreted by the shell as an input redirect. The contents of March.csv are sent to your program's standard input, which you are ignoring. This happens in the shell, before your Java program is even started. So, only February.csv is being sent as a command line argument to main.

Remove that <, so that all command-line arguments you intended to send to main are sent.

Upvotes: 2

Related Questions