0

I have the below code

public class prob01 {
    public static void main (String arg[]) {
        System.out.println(arg[0]);
    }
}

I have a text file prob01.txt in the same directory which contains:
StackOverflow StackExchange

How can I get the output as StackOverflow from the above code when I give the arguments as prob01.txt like below: (I dont want to modify the java code, the dos has to take care of parsing the file and sending the contents as arguments)

C:\> javac prob01.java
C:\> java -cp . prob01 < prob01.txt
Joshi
  • 573
  • 3
  • 11
  • 24
  • 3
    "I dont want to modify the java code" - well you have to. The command you've shown provides the content of the file as standard input, *not* as command-line arguments. I don't think there's anything in the Windows command shell to read a file and use its contents as command line arguments to another command. – Jon Skeet Nov 18 '14 at 13:46
  • Dont we have < to redirect? – Joshi Nov 18 '14 at 13:51
  • Again, that redirects the text of the file to standard input, *not* to command line arguments. (So if you read from `System.in`, you'll get the contents.) – Jon Skeet Nov 18 '14 at 13:53
  • You should be able to google the right dos batch file commands to read environment variables from a text file and then run something with those variables as command line arguments. – quamrana Nov 18 '14 at 14:06
  • Wild guess, as I'm running Linux, see http://stackoverflow.com/questions/5886334/windows-batch-set-variables-from-text-file to make a variable `input` from a file. And `java -cp . prob01 %input%` to run the Java with `%input%` as argument? I'm not sure if this works... – Charlie Nov 18 '14 at 14:49

1 Answers1

0

You'll need to use Reader in java code to read the content of the file.

peterremec
  • 488
  • 1
  • 15
  • 46