Results 1 to 4 of 4
  1. #1
    JavaBeast is offline Member
    Join Date
    Mar 2013
    Posts
    3
    Rep Power
    0

    Default Please help me with Exit Codes.

    Hi all,

    I am very new on this and I am trying to figure it out how to add Exit Codes to programs.

    I created this code that prints all the arguments you type in..

    Example:

    C:\Users\JC\Documents>java -jar Echo.jar 1 2 3 4 5 6 7 7 8 9
    1
    2
    3
    4
    5
    6
    7
    7
    8
    9

    Here is the code:

    public class Echo {
    public static void main (String[] args) {
    for (String s: args) {
    System.out.println(s);
    }
    }
    }

    NOW, If no arguments are supplied to this program, it should return an exit code of 2, otherwise
    it should print the command line arguments and return an exit code of 0

    I think I should use something like if(arg.lengh!=0)....but I cannot make it work....

    It is possible for you to give me ideas on how to make it happen? the program works as you can see but I need to use the system.exit function.

    Thanks a lot for your time.

  2. #2
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    758
    Rep Power
    8

    Default Re: Please help me with Exit Codes.

    Hello,

    To check if the arguments is available you can do:

    Java Code:
    if (args.length == 0) {
        // No argument was supplied.
    }
    To exit with status code you can do something like:

    Java Code:
    // Exit with status code 2.
    System.exit(2);

  3. #3
    JavaBeast is offline Member
    Join Date
    Mar 2013
    Posts
    3
    Rep Power
    0

    Default Re: Please help me with Exit Codes.

    Quote Originally Posted by wsaryada View Post
    Hello,

    To check if the arguments is available you can do:

    Java Code:
    if (args.length == 0) {
        // No argument was supplied.
    }
    To exit with status code you can do something like:

    Java Code:
    // Exit with status code 2.
    System.exit(2);
    Thank you Sir and thank you....

    One more silly question......to actually check the return code at exit I believe I need to execute echo $?

  4. #4
    JavaBeast is offline Member
    Join Date
    Mar 2013
    Posts
    3
    Rep Power
    0

    Default Re: Please help me with Exit Codes.

    Quote Originally Posted by JavaBeast View Post
    Thank you Sir and thank you....

    One more silly question......to actually check the return code at exit I believe I need to execute echo $?
    I found it with %ERRORLEVELS%

Similar Threads

  1. Replies: 7
    Last Post: 06-27-2012, 05:30 PM
  2. Help with these two codes
    By blackdiz in forum New To Java
    Replies: 5
    Last Post: 06-09-2012, 12:03 PM
  3. Need help with my codes
    By vesperia in forum NetBeans
    Replies: 3
    Last Post: 06-29-2011, 12:23 PM
  4. What do the following codes do?
    By javaguy2 in forum New To Java
    Replies: 2
    Last Post: 01-23-2011, 10:23 PM
  5. How to exit the program by typing "exit"?
    By Laythe in forum New To Java
    Replies: 6
    Last Post: 08-19-2009, 08:32 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •