This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes file argument to java program on the command line Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "file argument to java program on the command line" Watch "file argument to java program on the command line" New topic
Author

file argument to java program on the command line

Stephen Huey
Ranch Hand

Joined: Jul 15, 2003
Posts: 618
If I want to run the Java program MyProgram from the command line and give it a text file located on the C drive called MyFile.txt as an argument, I know I have to type this:




Is there any way to handle the args in my main method so that a user wouldn't have to double escape the location of the file? In other words, I want to make it so a user can just type this:





It's not the end of the world, but if there's a way, I definitely wouldn't mind avoiding that extra step of confusion because I've got a bunch of programs controlled by Windows batch files and I can comment those so that the user will know what to do, but I figure just making it standard in the first place would cut down on frustration! Thanks...
Rovas Kram
Ranch Hand

Joined: Aug 08, 2003
Posts: 135
I know this doesn't answer your question directly but you can ask them to use a forward slash '/' instead.
Stephen Huey
Ranch Hand

Joined: Jul 15, 2003
Posts: 618
Yeah, I'm just catching any errors and printing out usage instructions with the forward slash and double backslash both given as examples.
Julian Kennedy
Ranch Hand

Joined: Aug 02, 2004
Posts: 823
How about:

Then just use escapedArg where you would have used args[0].

You could achieve the same thing with a regular expression and String.replaceAll(), but thinking about it made my head hurt.

Jules
Rovas Kram
Ranch Hand

Joined: Aug 08, 2003
Posts: 135
I just did a test:


If I run the above code as such
java MyProgram c:\whatever
I get
c:\whatever

This tells me that I could use that args[0] to open a file with no problem. My conclusion is that you need the double \\ for string literals but not to pass arguments to main.
Julian Kennedy
Ranch Hand

Joined: Aug 02, 2004
Posts: 823
Yes, that just occurred to me too. The problem is: there is no problem! They're the best kind.
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
Perhaps a nice solution would be to prompt your users with a JFileChoose instead of requiring the location be specified by a command line arguement.


[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Stefan Wagner
Ranch Hand

Joined: Jun 02, 2003
Posts: 1923

The last solution would prevent to use the program in a script, when there is no user, to be prompted.
Of course it could be used, if (args.length == 0), to support both.

Note that if you put your app into a jar-File, and have set up your system, how to handle .jars, and pull some marked files with the mouse to your jar, the application may handle them as array of strings of filenames, i.e.
"file.a" "file containing a.blank" "file.b".


http://home.arcor.de/hirnstrom/bewerbung
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: file argument to java program on the command line