• Post Reply Bookmark Topic Watch Topic
  • New Topic

Java does not recognize the content file passed as a parameter  RSS feed

 
Fernando Skackauskas
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Guys,

I have a program in java which must receive three files as parameter. The command line is:

java -Djava.library.path="C:\Nitgen" -jar C:\Nitgen\Gravar.jar C:\Nitgen\gravado.txt C:\Nitgen\codigo.txt C:\Nitgen\ nome.txt

the contents of the files are:
    C:\Nitgen\gravado.txt = "XXXXX";
    C:\Nitgen\codigo.txt = "YYYYY"
    C:\Nitgen\nome.txt = "ZZZZZ"


The java code that I get the parameters is as follows:


The output that is occurring =

    The content of first file = XXXXX
    The content of second file = C:\Nitgen\codigo.txt
    The content of third file = C:\Nitgen\nome.txt


So, only the first parameter is receiving the content of the file path. But the second and third parameters, (arg[1] and arg[2]), is not receivenig the file's contents, but the file path.

What I am doing wrong?

Thanks,

Fernando
 
Paweł Baczyński
Bartender
Posts: 2083
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, I am surprised the first parameter got output.

When I ran this:I got:
d:\temp>java Temp C:\Nitgen\gravado.txt C:\Nitgen\codigo.txt C:\Nitgen\nome.txt
The content of first file = C:\Nitgen\gravado.txt
The content of second file = C:\Nitgen\codigo.txt
The content of third file = C:\Nitgen\nome.txt


By the way, you have an extra space in your command:
java -Djava.library.path="C:\Nitgen" -jar C:\Nitgen\Gravar.jar C:\Nitgen\gravado.txt C:\Nitgen\codigo.txt C:\Nitgen\_nome.txt
 
Fernando Skackauskas
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mr. Pawel,

thank you.

But, how is the correct way for get the content of the file, but not the file path?

Where must I change? At the java program or in the command line?
 
Stephan van Hulst
Saloon Keeper
Posts: 7976
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to read these files manually. First translate each argument to a java.nio.file.Path object using java.nio.file.Paths.get(), then use java.nio.file.Files.readAllLines(), or Files.newBufferedReader() to read the contents. When you use a reader, make sure to use try-with-resources to close the file neatly:
 
Paweł Baczyński
Bartender
Posts: 2083
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or Files.lines() if you find a Stream useful in your case.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!