7 Replies Latest reply: Sep 16, 2012 12:04 PM by rp0428 RSS

    Catching argument from command line

    GiulioSeverini
      Hi,

      I need to write a program that accepts arguments from command line and prints them. The excercise says I need the String array of command lines. But I do not know how to catch command line arguments. Do I need the method echo?

      Thank you and kind regards,

      Alberto.
        • 1. Re: Catching argument from command line
          DrClap
          public static void main(String[] args) {
            // your code goes here
          }
          You do have a "main" method like this one, don't you? Well, the "args" parameter is that String array of command-line parameters.

          (Don't forget to type the parameters on the command line when you actually run the class.)
          • 2. Re: Catching argument from command line
            962025
            U can catch the data from command line follow this code

            class IOExample
            {
            public static void main(String args[]) throws Exception // never forget to throw Exception in case u r using Input Output
            {
            String s=args[0];
            System.out.println(s);

            // you can take as many no. of String u want.


            String s1=args[1];
            System.out.println(s1);
            }

            run like this

            c:/>java IOExample vipin tiwari

            it will take two string first vipin(args[0]) and second ,tiwari(args[1]) . if u have any doubt post ur doubt.
            • 3. Re: Catching argument from command line
              Kayaman
              VTiwari wrote:
              public static void main(String args[]) throws Exception // never forget to throw Exception in case u r using Input Output
              Please ignore his bad advice about throwing Exception.
              it will take two string first vipin(args[0]) and second ,tiwari(args[1]) . if u have any doubt post ur doubt.
              I don't think you should be trying to help him. You'll do more harm than good.

              To the Original Poster:
              Remember to check the amount of arguments present (with args.length), so you don't try to access commandline arguments that don't exist.
              • 4. Re: Catching argument from command line
                aksarben
                Though you can read the command from the parameters passed to main(), you should learn to avoid using the term "catching arguments." Catch is a Java keyword & has a very specific meaning. It will confuse you & other developers if you use it when you mean "reading" or "processing" arguments.
                • 5. Re: Catching argument from command line
                  GiulioSeverini
                  Hi,

                  thank you all. The fact is: I do not think that after the 2nd Chapter of the book I am reading (Thinking in Java 2nd Edition) one could be able to do an excercise like that.
                  In fact there are still not enough information about how to do it, so I ignore why the author has put such an excercise like that immediately at the 2nd chapter.
                  Anyway, I have found the following code on the Internet:

                  package ReadString;

                  import java.io.*;

                  public class ReadString {

                  public static void main(String[] args) {
                  InputStreamReader reader = new InputStreamReader (System.in);
                  BufferedReader myInput = new BufferedReader (reader);
                  String str= new String();
                  try {
                  str = myInput.readLine(); } catch (IOException e){
                  System.out.println ("An error has occourred: " + e);
                  System.exit(-1);}

                  System.out.println ("You have written: " + str);
                  }
                  }

                  As far as I see it works fine, and it meets the scope of the excercise. But, as I can see, too much should be known that I still do not know :(

                  Kind regards,

                  Giulio.
                  • 6. Re: Catching argument from command line
                    GiulioSeverini
                    Hi,

                    I have found this code on the internet

                    package leggistringa2;

                    public class LeggiStringa2 {
                    public static void main(String[] args) {
                    for (int i=0; i < args.length; i++) {
                    System.out.println("Hello " + args);
                    }
                    }
                    }

                    but when I try to compile it from command line I obtain the following error:

                    C:\Users\Giulio\Documents\NetBeansProjects\LeggiStringa2\src\leggistringa2>java
                    leggiStringa2
                    Exception in thread "main" java.lang.NoClassDefFoundError: leggiStringa2 (wrong
                    name: leggistringa2/LeggiStringa2)
                    at java.lang.ClassLoader.defineClass1(Native Method)
                    at java.lang.ClassLoader.defineClass(Unknown Source)
                    at java.security.SecureClassLoader.defineClass(Unknown Source)
                    at java.net.URLClassLoader.defineClass(Unknown Source)
                    at java.net.URLClassLoader.access$100(Unknown Source)
                    at java.net.URLClassLoader$1.run(Unknown Source)
                    at java.net.URLClassLoader$1.run(Unknown Source)
                    at java.security.AccessController.doPrivileged(Native Method)
                    at java.net.URLClassLoader.findClass(Unknown Source)
                    at java.lang.ClassLoader.loadClass(Unknown Source)
                    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
                    at java.lang.ClassLoader.loadClass(Unknown Source)
                    at sun.launcher.LauncherHelper.checkAndLoadMain(Unknown Source)

                    C:\Users\Giulio\Documents\NetBeansProjects\LeggiStringa2\src\leggistringa2>

                    IT COMPILES IN NETBEANS, THOUGH...

                    What can be the mistake?

                    Kind regards,

                    Giulio
                    • 7. Re: Catching argument from command line
                      rp0428
                      >
                      but when I try to compile it from command line I obtain the following error:

                      C:\Users\Giulio\Documents\NetBeansProjects\LeggiStringa2\src\leggistringa2>java
                      leggiStringa2
                      >
                      'java' is not the compiler. You need to use 'javac' to compile.

                      Since the next error you will get is related to the classpath you should start with the Java tutorial
                      http://docs.oracle.com/javase/tutorial/getStarted/cupojava/index.html

                      The tutorial has examples and lessons that cover most of the Java functionality.