• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

Java does not recognize the content file passed as a parameter

 
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 2236
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Saloon Keeper
Posts: 14497
325
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 2236
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Or Files.lines() if you find a Stream useful in your case.
 
Destroy anything that stands in your way. Except this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic