Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: command line arguments

  1. #1
    Junior Member
    Join Date
    Dec 2010
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default command line arguments

    Hi Everybody

    I had a question regarding command line input.
    What i'm trying to do is run a program based on arguments from the command line.

    The entire line to be read in is:
    //java -jar sort1.jar -fres/p -tsorttype=v -ssort
    This is what I have so far:
    public static void main(String[] args)
    	{
    		//if substring equals t then do sorttype
    		//pick out the arguments from each item
    		//if -f then pick out file
    		//if -
     
    		//java -jar sort1.jar -fres/p -tsorttype=v -ssort
    		Testing t = new Testing();
     
    		String file = args[0];
    		for (String s: args)
    		{
    			if (args[0].equals(arg0))
                            {
                               //perform file operation
                           }
                             	if (args[1].equals(arg0))   
                                   {
                                        perform sort type
                                  }
    		}
     
     
    		//String path = args[1];
    		String compare = args[1];
    		String sort = args[2];
    		char method2;
    		//System.out.println(path);
    		if (compare.contains("-h"))
    		{
    			method = 'h';
    			//Testing.readIn(path);
     
    		}
    		else if (compare.contains("-a"))
    		{
    			method = 'a';
    		}
     
    		else if (compare.contains("-v"))
    		{
    			method = 'v';
    		}
     
    		if (compare.contains(sort))
    		{
    			method2 = 't';
    		}

    I've been trying to tear this apart for days but have no idea. Can someone please help. I tried looking in the javadoc but there are no examples.

    Thanks
    Riz


  2. #2
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: command line arguments

    Not sure how relevant it is, but I touched on doing a command line argument in a post of mine awhile ago.

    See if any of this is useful: http://www.javaprogrammingforums.com...resources.html
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  3. #3
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: command line arguments

    What exactly is the problem?

  4. #4
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: command line arguments

    I believe he's asking about passing command line arguments to his program rather than using his program to call a command-line function.

    As copeg stated, what problem are you running into?

    Right away I can tell you have a few grammatical errors with your code. You're trying to access arg0 which doesn't exist.
    if (args[0].equals(arg0))
                            {
                               //perform file operation
                           }
                             	if (args[1].equals(arg0))   
                                   {
                                        perform sort type
                                  }

    Perhaps you meant an actual string, such as -fres?

    Try this instead:

    if (args[0].equals("-fres"))
                            {
                               //perform file operation
                           }
                             	if (args[1].equals("-fres"))   
                                   {
                                        perform sort type
                                  }

Similar Threads

  1. How do i encrypt my password at command line
    By AHOT in forum Java Theory & Questions
    Replies: 5
    Last Post: October 13th, 2010, 03:13 PM
  2. A problem with command line compilation
    By goodguy in forum Java Theory & Questions
    Replies: 4
    Last Post: August 2nd, 2010, 10:58 AM
  3. [SOLVED] Command Line Argument Help
    By EmSaint in forum Loops & Control Statements
    Replies: 2
    Last Post: January 28th, 2010, 10:55 AM
  4. Multi-Valued Command Line Arguments
    By joey86 in forum Java Theory & Questions
    Replies: 1
    Last Post: December 29th, 2009, 11:19 AM
  5. How to Send command line arguments in Eclipse?
    By JavaPF in forum Java JDK & IDE Tutorials
    Replies: 0
    Last Post: April 23rd, 2009, 11:37 AM

Tags for this Thread