Logo 
Search:

Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

command line parameters

  Asked By: Emily    Date: Jul 14    Category: Java    Views: 851
  

i am quiet new to java and ive been pondering why i need to mention
command line parameters in my program although i dont use it.

lets take an example program

class Temp

{

public static void main(String[] args)

{

System.out.println("hi its Temp class");
}

}


in this program i dont use any command line parameters although the
"jvm" requires me to use it in main.


so if i ignore the command line arguments and rewrite my program


class Temp

{

public static void main()

{

System.out.println("hi its Temp class");
}

}


the program compiles but i get a run time exception given by the jvm
when i try to run it the exception is

" There is no main method in class: Temp"

i would appericiate if some one can clarify this doubt.

Share: 

 

4 Answers Found

 
Answer #1    Answered By: Frederick Greene     Answered On: Jul 14

In java, the syntax of the main  method is with array of strings as parameter
though you may not use it always. It helps in passing information to an
application at runtime. It let users affect the operation of the application
without recompiling it. In your case, main method  is like any other method since
it does not comply with the java  syntax.

 
Answer #2    Answered By: Kelly Bell     Answered On: Jul 14

U r having an error in ur program...

Write :

public static  void main( String args[])
{
...
}

instead of
----------------------------
class Temp

{

-----> public static void  main(String[] args)

{

System.out.println("hi its Temp class");
}

}

 
Answer #3    Answered By: Angel Harris     Answered On: Jul 14

My guess would be that it crashes because there is no place to store
the null String of arguments. When it tries to set the arguments  to 0,
it cannot find a place to store the value and crashes out. Plus, it's
a good way of keeping the language regular.

 
Answer #4    Answered By: Cheri Garcia     Answered On: Jul 14

The java  interpreter is designed to run  Java "applications". Apart from
applications, which are usually executed outside of a browser
environment, you have applets, which are integrated into web pages, and
other special Java programs such as JavaBeans, servlets and handlers.
The java interpreter upon execution first looks for an executable main()
method, that provides the "point-of-entry" into your application.
In a C/C++ application, you would usually omit the argc, argv params if
your program  is not going to accept any command  line arguments. However
the PSVM(String[] args) line  is what tells it to expect a Java
stand-alone application. (You would be omitting this line in applets and
other non-application programs) Since the main  function has to be of the
form
public static  void main(String[] args)
any deviation from this syntax would cause Java to assume that you are
using a custom method  called main, which is obviously not a point of
entry into the program.
Your second program compiles because Java thinks you are creating your
own custom main method for use somewhere else. It is only when the java
interpreter tries to "run" your program, it looks for the main() method,
and not finding an exact match, triggers the exception.
In the end, all you have to remember is that Java is not C or C++,
although it may look syntactically the same, and what goes for one
language usually does not for another. Hope this helped to some extent.

 
Didn't find what you were looking for? Find more on command line parameters Or get search suggestion and latest updates.




Tagged: