Why we use string args[]  in void main()

why we use string in void main in java?

Because that's roughly how C did it and Java is based on C.
You can get all the input arguments using JMX, but it's rather complicated and not useful except in obscure cases.
(I use it to determine if a unit test is running in debug and change the timeouts in my tests so I can step through a test without it stopping at a random point due to a time out)

Similar Messages

  • Why doyou pass string[] argv to main?

    I was just wondering why when you call the main method you pass a string array tto it? I just always did it without thinking about it and now im looking at the following code that uses argv-
    public static void main(String[] argv)
            try
                for(int i=0; i < argv.length; i++)
                    if(argv.endsWith(".class"))
    I can't figure out why its using argv and what it's doing.

    If that method were invoked by the VM while someone
    started it up on the command-line like this (let's
    pretend the class that main method belongs to is
    called Foo):
    java Foo one two three
    The strings "one", "two", and "three" are stuffed
    into a String array of size 3, and passed to
    main(String[] argv). argv is that array.Thanks, I understand what the code is doing now.

  • Need of String args[] in main()

    Hai all, i had a small doubt regarding the usage of String args[] in main function. When we are not passing any parameters to the program also, we have to give this String args[]. why should we do so. I mean when we pass some arguements to the program then there is the usage of String args[] but when we are not passing also, we have to give String args[] in the main function. why is it made mandatory?

    There is only one method signature that the JVM will call. This is part of the Java specification.
    The String[] args parameter represents the command line arguments. If there were none, the array will be empty.
    Think about it: imagine there were two versions, one with and one without the String[] args parameter, and your application doesn't expect any command line arguments, so it implements only the version with no parameter at all. What would happen if the user decides to provide some command line arguments just for fun?

  • Standard/common handling void main(String argv[])

    i would like to have a parent class to handle the void main(String argv[]), but I can't instantiate my child class because I dun know it's name.
    any way to get that? or any alternative way to handle the void main(String argv[]) in common? million thanks.
    eg:
    public abstract class parent {
    public static void main(String argv[]){
    // i need to know which child is been run by java childclass
    // so that I can instantiate a new object and call the run method
    public abstract void run();
    public class child1 extends parent{
    public void run(){}
    public class child2 extends parent{
    public void run(){}
    }

    Class.forName(name).newInstance();
    provided I know name=what, right?
    how do I know ppl calling using
    java child1?
    or
    java child2?
    both will go use same
    public static void main(String argv[])
    in parent class.

  • RFC FM using string parameter

    Hi all
       An RFC FM can using string parameter? why i using string pass value ,the destination reciev the value is wrong?
    thanks
    BR
    Chris

    Are you using any std func.. just wanted to chek that there is no internal conversion routine....
    Enjoy SAP.
    Pankaj Singh

  • Why can't created an object in the "public static void main(String args[])"

    Example:
    public Class Exp {
    public static void main(String args[])
    new Exp2();-------------> To Occur Error
    Class Exp2 {
    public Exp2()

    You can't create an inner class within main, because
    it is a static method. Inner classes can only be
    created by true class methods.This is not correct. You can create an inner class object in a static method. You just have to specify an outer class object that the inner class object should be created within:
    Exp2 exp2 = new Exp().new Exp2();

  • Why should i write main(String[] args)?

    Hello,
    I don't know why i am obliged in the main function of the class to send an array of string as parameters?
    Thanks

    Actually I think this is a great question!
    main()
    Any java application has to have a main method to do anything, but as you gain further into java and object orientated practices you will start writing lots of stuff that do NOT have a main method but are *.java and *.class files, neither will they do anything unless they are attached to a program with a main. Andthen as you get DEEPER still you might even have 'private synchronised void main(String[] args)' - though not too often.
    Why an array of strings? Hmmm ...pauses ...thinks, there is no obvious reason, other than the fact that if you're going to pass parameters from 'java ...' command line to the application. This is, logically, the most potentially useful data type to have. There may well be other reasons for this, so the answer to the second part of your question is a logical deduction - it may not be right.

  • How to call java with public static void main(String[] args) throws by jsp?

    how do i call this from jsp? <%spServicelnd temp = new spServicelnd();%> does not work because the program has a main. can i make another 2nd.java to call this spServiceInd.java then call 2nd.java by jsp? if yes, how??? The code is found below...
    import java.net.MalformedURLException;
    import java.io.IOException;
    import com.openwave.wappush.*;
    public class spServiceInd
         private final static String ppgAddress = "http://devgate2.openwave.com:9002/pap";
         private final static String[] clientAddress = {"1089478279-49372_devgate2.openwave.com/[email protected]"};
    //     private final static String[] clientAddress = {"+639209063665/[email protected]"};
         private final static String SvcIndURI = "http://devgate2.openwave.com/cgi-bin/mailbox.cgi";
         private static void printResults(PushResponse pushResponse) throws WapPushException, MalformedURLException, IOException
              System.out.println("hello cze, I'm inside printResult");
              //Read the response to find out if the Push Submission succeded.
              //1001 = "Accepted for processing"
              if (pushResponse.getResultCode() == 1001)
                   try
                        String pushID = pushResponse.getPushID();
                        SimplePush sp = new SimplePush(new java.net.URL(ppgAddress), "SampleApp", "/sampleapp");
                        StatusQueryResponse queryResponse = sp.queryStatus(pushID, null);
                        StatusQueryResult queryResult = queryResponse.getResult(0);
                        System.out.println("Message status: " + queryResult.getMessageState());
                   catch (WapPushException exception)
                        System.out.println("*** ERROR - WapPushException (" + exception.getMessage() + ")");
                   catch (MalformedURLException exception)
                        System.out.println("*** ERROR - MalformedURLException (" + exception.getMessage() + ")");
                   catch (IOException exception)
                        System.out.println("*** ERROR - IOException (" + exception.getMessage() + ")");
              else
                   System.out.println("Message failed");
                   System.out.println(pushResponse.getResultCode());
         }//printResults
         public void SubmitMsg() throws WapPushException, IOException
              System.out.println("hello cze, I'm inside SubmitMsg");          
              try
                   System.out.println("hello cze, I'm inside SubmitMsg (inside Try)");                         
                   //Instantiate a SimplePush object passing in the PPG URL,
                   //product name, and PushID suffix, which ensures that the
                   //PushID is unique.
                   SimplePush sp = new SimplePush(new java.net.URL(ppgAddress), "SampleApp", "/sampleapp");
                   //Send the Service Indication.
                   PushResponse response = sp.pushServiceIndication(clientAddress, "You have a pending Report/Request. Please logIn to IRMS", SvcIndURI, ServiceIndicationAction.signalHigh);
                   //Print the response from the PPG.
                   printResults(response);
              }//try
              catch (WapPushException exception)
                   System.out.println("*** ERROR - WapPushException (" + exception.getMessage() + ")");
              catch (IOException exception)
                   System.out.println("*** ERROR - IOException (" + exception.getMessage() + ")");
         }//SubmitMsg()
         public static void main(String[] args) throws WapPushException, IOException
              System.out.println("hello cze, I'm inside main");
              spServiceInd spsi = new spServiceInd();
              spsi.SubmitMsg();
         }//main
    }//class spServiceInd

    In general, classes with main method should be called from command prompt (that's the reason for main method). Remove the main method, put the class in a package and import the apckage in your jsp (java classes should not be in the location as jsps).
    When you import the package in jsp, then you can instantiate the class and use any of it's methods or call the statis methods directly:
    <%
    spServiceInd spsi = new spServiceInd();
    spsi.SubmitMsg();
    %>

  • Can't find file when use the main(String[] args) method

    I put a file under the DefaultDomain directory of Weblogic 11.1.1.5 on my local machine. Below is how I look up this file
    FileInputStream fis = new FileInputStream("input.xml");
    I started up the IntegratedWeblogicServer instance and I tested using the below 2 approaches.
    1) When I test a web service program using the below approach, it CAN'T find the input.xml file
    public static void main(String[] args)
    2) When I package it into an EAR file and deploy it onto the server then log into the admin console and use the test client approach then it's able to find the file
    How do I make it so it can find the file using #1 approach?
    Thanks

    That's how relative paths work, and it's nothing particular to Java.
    When you type ls input.xml or dir input.xml does it list the file, or give an error? If it lists the file, then it's in your current directory, and it will work if you run Java from that directory. If it gives an error, then that file is not in your current working directory, and Java won't be able to find it either.

  • Private static void main(String args[]) ... ?????

    even if the main method is declared
    private static void main(String args[])
    //Code Here
    it works fine..can somebody explain the reason behind?

    hi rkippen
    so u mean, since the JVM is calling the main it can
    break all kinds of access specifiers to any class..am
    i right?
    This could be the case.
    However, I tried using 1.4.1 with no development environment (just java.exe) and it gave the following error:
    C:\>java MainTest
    Main method not public.
    So, your development environment might be the cause (e.g. what you use to program your code (e.x. JBuilder)), or the Java version.
    Your development environment might be using reflection to get around Java accessibility checks. For debugging purposes, it is possible to use reflection to obtain refereneces to private methods/fields and invoke those methods and access those fields.
    E.g.
    Vector v = new Vector();
    Field f = v.getClass().getDeclaredField("elementCount");
    f.setAccessible(true); // this is the trick
    f.set(v, new Integer(100));In the presence of a security manager, the setAccessible method will fire a "suppressAccessChecks" permission check.

  • What is the use of passing String[] args in main() method?

    what is the use of passing String[] args in main() method?
    Is there any specific use for this ?

    actually my sir asked me the same question & I gave
    the same reply as given by you........but he further
    asked what is the use of this also??
    ie accepting cmd line args at runtime??is there any
    specific purpose ??Apart from the one you just mentioned? No

  • When does the Java main(String args[]) method exit?

    Hi all,
    I and my colleagues want to know, when does the main() method exit?
    we wrote a small code as follows:
    import java.awt.*;
    public class Test {
    public static void main(String[] args) {
    Frame f = new Frame("hi");
    f.show();
    System.out.println("after show");
    and ran the program, in which case I could see the printed message "after show", but although there is no code after the System.out.println(), the Java virtual machine does not exit and waits for the Frame to close.
    My question is has the main method exited in this case? since it clearly shows that it does not have any more code to execute. Does java create the main method as a thread or as a process?
    regards,
    Harshad

    To make your application terminate you need to add code to cause the AWT thread to handle the termination. In your code, before f.show(), try:
    f.addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent e) {
        dispose();
      public void windowClosed(WindowEvent e) {
        System.exit(0);
    });Now, at least you can click on the little "X" on the upper right-hand corner of the Frame (at least in Win95/98/NT) environments) to cause your application to receive a window closing event. It is also possible to add a menu with an option to exit; in that case I'd use an EventQueue to cause a window closing event to be sent.

  • What happens when main(String[] args)

    Hi techies,
    I am trying to understand, "how the JVM invokes public static void main(String[] args)".
    I had gone through JVM tutorial, and found that there are 2 different types of classloaders 1. bootstrap loader 2. user defined class loader.
    I had gone through java api for class loader class. i did not find the method main(String[] args).
    I am having confusion whether bootstrap loader calls main(String[] args)(or) userdefined classloader calles main(String[] args).
    Why do we have to give String[] args as argument to main??
    can you guys guide to understand this .
    regards,
    Krishna

    Hi techies,
    I am trying to understand, "how the JVM
    invokes public static void main(String[] args)".
    I had gone through JVM tutorial, and
    found that there are 2 different types of
    classloaders 1. bootstrap loader 2. user defined
    class loader.
    I had gone through java api for class
    loader class. i did not find the method
    main(String[] args).
    I am having confusion whether bootstrap
    loader calls main(String[] args)(or) userdefined
    classloader calles main(String[] args).
    Why do we have to give String[] args as
    argument to main??
    can you guys guide to understand this
    regards,
    Krishnawell, you won't find main(String[] args) in the docs for ClassLoader, since it's not a method on ClassLoader!
    the bootstrap loader loads, verifies, prepares and initializes the class for which you pass the fully.qualified.name into the JVM, and looks for the static method on that class called 'main' which has a single argument, an array of Strings, and no return type ( I'm not 100% sure of that last part actually - I'll try it ) and begins a thread of execution on that method. from there, your program is run
    we have to give an array of Strings in order to pass arguments into the program
    it can't be a user-defined classloader that does this, because most of the time there won't be a user-defined classloader!

  • Please what does 'String[] args' in main is set to in a normal no parameter

    Please,
    what does 'String[] args' in main is set to in a normal no parameters static main function??
    I have a frame with a static main function in it, in the main i want to do one thing if the function got parameters and another if it didnt.
    if i check for (if (args[0] == "Load") i get an exception
    and args is not null, i know, how do i know if the array has parameters??
    p.s
    I new to java

    Yes, like jsalonen said.
    And args[0]=="Load" won't work, you have to use
    args[0].equals("Load");

  • Using a the string args to input command line numerical input

    Hello,
    I am trying to allow the user to input a number from the command line to set up the number of elements in an array. I am having trouble converting the string info to int. Could someone post an example of how this is possible? Or a way to read a string for a numerical value?
    Example:
    nameofprogram 105 (so this means they want a 105 element array create when the program starts)
    Message was edited by:
    2disbetter

    try this
    public class Test {
         public static void main(String[] args)
              final int i = Integer.parseInt( args[0]);
              int[] a = new int;
              System.out.println(a.length);
    if you have to have variable in size part of the array object creator it needs to be final.

Maybe you are looking for