1

I am newbie to groovy. In command prompt (I am not using any IDE), I typed following : (No Problem with environment variables settings please)

groovy -n -e "println line.toLong()" data.txt

Error :

Caught: java.io.IOException: Invalid argument
java.io.IOException: Invalid argument

data.txt is there in that directory (verified using TAB key)


Not sure why its throwing Error ????

[data.txt contains raw data in following format]

1

2

3

4

Prashant2329
  • 327
  • 3
  • 7
  • 21
  • Do the file have those line breaks? – Will Aug 13 '14 at 12:09
  • Yes............ I am using Java 7 version and groovy 2.2.2 version – Prashant2329 Aug 13 '14 at 12:22
  • Even if I use line.reverse() , its throwing the same error .... – Prashant2329 Aug 13 '14 at 12:26
  • Can you try reading the file manually? `groovy -e ' println new File("data.txt").text '` – Will Aug 13 '14 at 13:53
  • Caught: groovy.lang.MissingPropertyException: No such property: data for class: script_from_command_line – Prashant2329 Aug 13 '14 at 14:16
  • There is something wrong with your script. How did you wrote the quotes? – Will Aug 13 '14 at 14:51
  • While executing groovy -e " println new File("data.txt").text " [see double quotes around execute script] M getting this Error : Caught: groovy.lang.MissingPropertyException: No such property: data for class: script_from_command_line – Prashant2329 Aug 14 '14 at 03:13
  • I wrote "[double quotes] that enclosed the println statement [Starting double quote is before println and Ending double quote is at the end of the command] and I have also enclosed data.txt in double quotes same as I mentioned in above comment.... – Prashant2329 Aug 14 '14 at 03:22
  • I am using groovy 2.2.2 version ...and that wont be problem surely or Will it ???? – Prashant2329 Aug 14 '14 at 04:52
  • Are there empty lines between the lines containing the numbers? – Robby Cornelissen Aug 14 '14 at 05:31
  • Run it in debug mode and post the full stacktrace: `groovy -d -n -e "println line.toLong()" data.txt` – Robby Cornelissen Aug 14 '14 at 05:39
  • If I use println System, command gets succed.. If I use println line , throws an error.... – Prashant2329 Aug 14 '14 at 05:40
  • FULL STACKTRACE IS AS FOLLOWS : Caught: java.io.IOException: Invalid argument java.io.IOException: Invalid argument at java.io.WinNTFileSystem.canonicalize0(Native Method) at java.io.Win32FileSystem.canonicalize(Win32FileSystem.java:414) ... etc etc – Prashant2329 Aug 14 '14 at 05:42
  • @Robby Do you know whats this : Invalid argument at java.io.WinNTFileSystem.canonicalize0(Native Method) at java.io.Win32FileSystem.canonicalize(Win32FileSystem.java ... ???? – Prashant2329 Aug 14 '14 at 05:46
  • It's a native Windows method that's throwing the exception. No idea why, I'm not a Windows user myself. – Robby Cornelissen Aug 14 '14 at 05:51
  • Sorry Robby. I might hoped much from you. Sorry 4 that. and BTW, thanks a lot for a Quick response and an immediate help. and Thanks to JamesA and Will too for finding out some time to help me. – Prashant2329 Aug 14 '14 at 08:06
  • Those double quotes, at least in Linux, are wrong. You need to wrap the whole script in single quotes and the println string in double quotes, or the whole script in double quotes and the println string in single quotes. Trying changing one of then or executing the command line exactly as i wrote in my comment – Will Aug 14 '14 at 11:55

1 Answers1

1

I tried to duplicate the problem with Groovy 2.3.6 and Java 1.7.0_60 on Linux and had no issue:

$ echo -e "1\n2\n\3\n\4" > data.txt
$ groovy -n -e "println line.toLong()" data.txt
1
2
3
4

With a non-existing file:

$ groovy -n -e "println line.toLong()" bogus.txt
Caught: java.io.FileNotFoundException: bogus.txt
java.io.FileNotFoundException: bogus.txt

With non-numeric data:

$ echo -e "a\nb\nc\nd" > data.txt
$ groovy -n -e "println line.toLong()" data.txt
Caught: java.lang.NumberFormatException: For input string: "a"
java.lang.NumberFormatException: For input string: "a"
        at script_from_command_line.run(script_from_command_line:1)

And it even worked with CR/LF EOL:

$ echo -e "1\r\n2\r\n3\r\n4\r" > data.txt
$ groovy -n -e "println line.toLong()" data.txt
1
2
3
4
James Allman
  • 40,573
  • 11
  • 57
  • 70
  • Mine is throwing IOException with Invalid argument.... I dont understand what the problem with this .... – Prashant2329 Aug 13 '14 at 12:28
  • Prashant2329, try using the `echo` command as JamesA proposes. Your file might have some problem. – Will Aug 13 '14 at 12:36
  • Even if I pass wrong file name (i.e. Non-existing file) ,Unlike your's, it is throwing the same Invalid argument (IOException) ... Instead it should throw FileNotFoundException ........ Whats wrong ???? – Prashant2329 Aug 13 '14 at 12:38
  • When I run echo -e "1\n2\n\3\n\4" > data.txt this command, My data.txt overwritten with following data: -e "1\n2\n3\n4\n" – Prashant2329 Aug 13 '14 at 12:42