aspose file tools*
The moose likes Beginning Java and the fly likes passing in a file from the command line Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "passing in a file from the command line" Watch "passing in a file from the command line" New topic
Author

passing in a file from the command line

April Benzine
Greenhorn

Joined: Sep 23, 2004
Posts: 17
I have to pass in a text file from the command line to a class that reads it in, processes and counts its tokens then prints it out to a .dat file in my output directory. how do I connect what is entered in on the command line with the variable I have created in my class (it cannot be hard coded in my class)?

Here's my code:



[Edited to add UBB Code tags to make code reading easier - GB]

Please help
[ September 29, 2004: Message edited by: Gregg Bolinger ]
Jeff Bosch
Ranch Hand

Joined: Jul 30, 2003
Posts: 804
Hi, April -

My first question would have to be, where is your main method? The command-line arguments come into the program via main. Do you have another, driver program that invokes this class and its methods? Also, what error are you seeing? Compile-time, run-time, logical?

With more info, maybe I (or another visitor) can help.

Thanks,

Jeff
[ September 29, 2004: Message edited by: Jeff Bosch ]

Give a man a fish, he'll eat for one day. Teach a man to fish, he'll drink all your beer.
Cheers, Jeff (SCJP 1.4, SCJD in progress, if you can call that progress...)
Jeff Bosch
Ranch Hand

Joined: Jul 30, 2003
Posts: 804


Just out of curiosity, why are these variables public instead of private, like the other variables? This would expose your variables to outside modification.
April Benzine
Greenhorn

Joined: Sep 23, 2004
Posts: 17
Those were just the specs of the project's class diagram
Jeff Bosch
Ranch Hand

Joined: Jul 30, 2003
Posts: 804
May I ask what this project is for?
Jeff Bosch
Ranch Hand

Joined: Jul 30, 2003
Posts: 804
I couldn't get either file to compile as is. I had 15 compiler errors with the AnalyzeFile and 21 with the driver program.

Is some code missing? Perhaps your import statements? Custom packages or classes? (The classes TokenCount and PropertiesManager aren't part of the standard API's that I can see.)
[ September 29, 2004: Message edited by: Jeff Bosch ]
Stefan Wagner
Ranch Hand

Joined: Jun 02, 2003
Posts: 1923



In your other post you write:

But why == 2 ?
Coming from c/c++?
args[0] is the first parameter in java, not the program-name.
One file argument leads to (args.length == 1).
[ September 29, 2004: Message edited by: Stefan Wagner ]

http://home.arcor.de/hirnstrom/bewerbung
somkiat puisungnoen
Ranch Hand

Joined: Jul 04, 2003
Posts: 1312
get data from command.

Run java program
java Test

Output
nothing

Run java program
java Test test1 test2 test3

Output
test1
test2
test3



SCJA,SCJP,SCWCD,SCBCD,SCEA I
Java Developer, Thailand
somkiat puisungnoen
Ranch Hand

Joined: Jul 04, 2003
Posts: 1312
I think, you should use Property file , it more flexible than current solution.
Stefan Wagner
Ranch Hand

Joined: Jun 02, 2003
Posts: 1923

I don't think so.
A parameter is immediately typed at the commandline, and if you prefer mice, you may move a file from the explorer to your well-defined jar, if you want your program to process that filename in some way (open the file, for example).
A properties-file needs to be found, edited, saved ..., you need backups to process older commands, while the commandline can have a history of thousends of calls...
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: passing in a file from the command line