• Post Reply Bookmark Topic Watch Topic
  • New Topic

command line argument  RSS feed

 
Hendra Kurniawan
Ranch Hand
Posts: 239
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
lets say I have a apps.java file. What I want is that if I do this:


the program will output "Hello" in the console, but if I do this:



the program will spot the -f, and then it will attempt to read every line in messages.txt and output it to console.

How do I do this?
thanks
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Look in the java tool documentation for what the -f option means.
 
Hendra Kurniawan
Ranch Hand
Posts: 239
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There's no -f option there. Does this mean I have parse the -f as arguments in static main's (String[] args)? Also -D for property, will it still work if I use -d instead (is it case-sensitive)? thanks
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hendra Kurniawan wrote:There's no -f option there. Does this mean I have parse the -f as arguments in static main's (String[] args)?
Yes

Hendra Kurniawan wrote:Also -D for property, will it still work if I use -d instead (is it case-sensitive)? thanks
It's been half an hour since you posted this question. Plenty of time for you to have written a simple program to test this.
 
Hendra Kurniawan
Ranch Hand
Posts: 239
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, I just need some help because I'm currently testing an algorithm, so it would be nice if I can get help with syntax problem. Thanks
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hendra Kurniawan wrote:No, I just need some help because I'm currently testing an algorithm, so it would be nice if I can get help with syntax problem. Thanks


You'll stand a better chance of getting help if you provide details about what the problem is, what you've tried, and what specific difficulties you've encountered. People here are much more responsive when you ShowSomeEffort and TellTheDetails.
 
Hendra Kurniawan
Ranch Hand
Posts: 239
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I must create a program to find the path to go from point A to several destination using the least number of flights. Right now because I'm still testing the algo to achieve this, the input is hardcoded. I haven't done the input part yet because I'm still very occupied with finding the algo. In real condition, the input will be in the form of flight schedule files (thus the txt). That's why I asked about how to handle the input, so I can concentrate on the algo.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hendra Kurniawan wrote:I must create a program to find the path to go from point A to several destination using the least number of flights.


That's not the kind of detail we need here. The purpose of this program is totally irrelevant for the problem you're asking about. A tiny SSCCE that only shows you trying to read and process the command line args, without any regard for what the larger program is intended to do, along with details about what specific troubles you had with that code, is what would be useful.

Right now because I'm still testing the algo to achieve this, the input is hardcoded. I haven't done the input part yet because I'm still very occupied with finding the algo. In real condition, the input will be in the form of flight schedule files (thus the txt). That's why I asked about how to handle the input, so I can concentrate on the algo.


Right. So you're hardcoding your input now, but later you want to take it from a text file. That part we could have guessed, and is not really relevant.

What have you tried so far? What difficulties did you encounter? Show code please.
 
Hendra Kurniawan
Ranch Hand
Posts: 239
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the reply. As I've told you I haven't tried anything at all on the input handling part. I'm still struggling on how to work out the flight plans. I thought I could get some sample code here so I can modify it a bit to suit my needs. I need to know how to spot the -f for flight files. Will it just be a simple args array looping, if you find -f, then the string right after it must be the file? is that the only way? and if -d is not the same as -D, then I can use -d for destination file. At least if I can get the sample code, that one less thing for me to worry. Thanks
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hendra Kurniawan wrote:Thanks for the reply. As I've told you I haven't tried anything at all on the input handling part. I'm still struggling on how to work out the flight plans.


Well, when you're ready to try the command line stuff, post your attempt and a detailed question. And to make it easy on yourself, start with a separate program that only does command line parsing--totally separate from your flight plan program. Once you get it working, incorporate what you've learned into the overall program. You'll save yourself a lot of headaches that way.

I thought I could get some sample code here so I can modify it a bit to suit my needs.


This site is NotACodeMill. People here want you to ShowSomeEffort. If you want sample code, you should be able to find plenty by googling for something like java command line processing.

I need to know how to spot the -f for flight files. Will it just be a simple args array looping, if you find -f, then the string right after it must be the file? is that the only way?


That's pretty much it. There are command line processing libraries that abstract some of that grunt work away for you, or you could maybe use regex, but under the covers, that's what they all do--scan and compare piece by piece. It's nothing magic, and no different than what you could write yourself.

and if -d is not the same as -D, then I can use -d for destination file. At least if I can get the sample code, that one less thing for me to worry. Thanks

As already pointed out, you can test that for yourself very easily.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hendra Kurniawan wrote: if -d is not the same as -D, then I can use -d for destination file. At least if I can get the sample code, that one less thing for me to worry. Thanks

You're talking about arguments that are passed to your program here - it's up to you whether -D is the same as -d or not.

The -D that allows you to set system property values is a java command line option.
This explains the difference but it doesn't say whether command line options are case sensitive or not so you'll still need to test this.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joanne Neal wrote:
Hendra Kurniawan wrote: if -d is not the same as -D, then I can use -d for destination file. At least if I can get the sample code, that one less thing for me to worry. Thanks

You're talking about arguments that are passed to your program here - it's up to you whether -D is the same as -d or not.


Ah, good point. I was not paying attention at all.

@OP:

java -this -that -whatever MyClass my args which may or may not include a -whatever


Everything before the "MyClass" part is for the JVM. Your program never sees those. That's where the -D for defining system properties is used.

Everything after the "MyClass" part is for your program. As Joanne says, it's up to you whether to treat -D the same as -d. Regardless of what you decide for that, after the class name, both -D and -d are completely separate from anything that comes before the class name.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!