Passing parameters to the "main" method


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 5 of 5

Thread: Passing parameters to the "main" method

  1. #1
    Andrew McLellan Guest

    Passing parameters to the "main" method


    I have a basic program that deletes a file. The problem I'm having is passing
    the file name to the class. I've tried putting the file in the same directory
    as the program, adding the full path to the file and putting it in the root
    directory but neither worked. The class is able to read in the file name
    but when it gets to the Delete method an exception is thrown when it tries
    to create a new file object. I assume I'm not passing the parameter correctly.
    Any help would be greatly appreciated.
    Regards
    Andrew

    import java.io.*;

    public class Delete {
    public static void main(String[] args){
    if(args.length != 1){
    System.err.print("Usage: java Delete <file or directory>");
    System.exit(0);
    }

    try{ Delete(args[0]); }
    catch(IllegalArgumentException e){
    System.err.print(e.getMessage());
    }
    }//end main

    public static void Delete(String filename){
    //create a file object to represent the file name
    File f = new File(filename); /**ERROR HERE**/



  2. #2
    markn Guest

    Re: Passing parameters to the "main" method


    What are you puting on the command line?

    "Andrew McLellan" <amclellan@nospamYahoo.com> wrote:
    >
    >I have a basic program that deletes a file. The problem I'm having is passing
    >the file name to the class. I've tried putting the file in the same directory
    >as the program, adding the full path to the file and putting it in the root
    >directory but neither worked. The class is able to read in the file name
    >but when it gets to the Delete method an exception is thrown when it tries
    >to create a new file object. I assume I'm not passing the parameter correctly.
    > Any help would be greatly appreciated.
    >Regards
    >Andrew
    >
    >import java.io.*;
    >
    >public class Delete {
    > public static void main(String[] args){
    > if(args.length != 1){
    > System.err.print("Usage: java Delete <file or directory>");
    > System.exit(0);
    > }
    >
    > try{ Delete(args[0]); }
    > catch(IllegalArgumentException e){
    > System.err.print(e.getMessage());
    > }
    > }//end main
    >
    >public static void Delete(String filename){
    > //create a file object to represent the file name
    > File f = new File(filename); /**ERROR HERE**/
    >
    >



  3. #3
    Andrew McLellan Guest

    Re: Passing parameters to the "main" method


    I'm using IntelliJ as my IDE and it provides the user with a form to enter
    the parameters. I stepped through the code and arg[0] was the value I entered.
    I assume what I'm doing wrong is real simple but I can't figure it out.
    Thanks for the help.

    "markn" <mnuttall@nospam.com> wrote:
    >
    >What are you puting on the command line?
    >
    >"Andrew McLellan" <amclellan@nospamYahoo.com> wrote:
    >>
    >>I have a basic program that deletes a file. The problem I'm having is

    passing
    >>the file name to the class. I've tried putting the file in the same directory
    >>as the program, adding the full path to the file and putting it in the

    root
    >>directory but neither worked. The class is able to read in the file name
    >>but when it gets to the Delete method an exception is thrown when it tries
    >>to create a new file object. I assume I'm not passing the parameter correctly.
    >> Any help would be greatly appreciated.
    >>Regards
    >>Andrew
    >>
    >>import java.io.*;
    >>
    >>public class Delete {
    >> public static void main(String[] args){
    >> if(args.length != 1){
    >> System.err.print("Usage: java Delete <file or directory>");
    >> System.exit(0);
    >> }
    >>
    >> try{ Delete(args[0]); }
    >> catch(IllegalArgumentException e){
    >> System.err.print(e.getMessage());
    >> }
    >> }//end main
    >>
    >>public static void Delete(String filename){
    >> //create a file object to represent the file name
    >> File f = new File(filename); /**ERROR HERE**/
    >>
    >>

    >



  4. #4
    markn Guest

    Re: Passing parameters to the "main" method


    So, what are you typing in? Because that value seems to be the problem.

    Mark

    "Andrew McLellan" <amclellan@nospamYahoo.com> wrote:
    >
    >I'm using IntelliJ as my IDE and it provides the user with a form to enter
    >the parameters. I stepped through the code and arg[0] was the value I entered.
    > I assume what I'm doing wrong is real simple but I can't figure it out.
    > Thanks for the help.
    >
    >"markn" <mnuttall@nospam.com> wrote:
    >>
    >>What are you puting on the command line?
    >>
    >>"Andrew McLellan" <amclellan@nospamYahoo.com> wrote:
    >>>
    >>>I have a basic program that deletes a file. The problem I'm having is

    >passing
    >>>the file name to the class. I've tried putting the file in the same directory
    >>>as the program, adding the full path to the file and putting it in the

    >root
    >>>directory but neither worked. The class is able to read in the file name
    >>>but when it gets to the Delete method an exception is thrown when it tries
    >>>to create a new file object. I assume I'm not passing the parameter correctly.
    >>> Any help would be greatly appreciated.
    >>>Regards
    >>>Andrew
    >>>
    >>>import java.io.*;
    >>>
    >>>public class Delete {
    >>> public static void main(String[] args){
    >>> if(args.length != 1){
    >>> System.err.print("Usage: java Delete <file or directory>");
    >>> System.exit(0);
    >>> }
    >>>
    >>> try{ Delete(args[0]); }
    >>> catch(IllegalArgumentException e){
    >>> System.err.print(e.getMessage());
    >>> }
    >>> }//end main
    >>>
    >>>public static void Delete(String filename){
    >>> //create a file object to represent the file name
    >>> File f = new File(filename); /**ERROR HERE**/
    >>>
    >>>

    >>

    >



  5. #5
    Jawahar Rajan Guest

    Re: Passing parameters to the "main" method

    What is the error you get ?
    The standard way passing a argument to the main from a command line is:
    java Delete filename

    where file name is the name of the file you are trying to delete in this case.
    Trying printing out the parse file name to make sure you are passing it correctly from
    the IDE you are using.
    HTH

    Thanks
    Jawahar


    markn wrote:

    > So, what are you typing in? Because that value seems to be the problem.
    >
    > Mark
    >
    > "Andrew McLellan" <amclellan@nospamYahoo.com> wrote:
    > >
    > >I'm using IntelliJ as my IDE and it provides the user with a form to enter
    > >the parameters. I stepped through the code and arg[0] was the value I entered.
    > > I assume what I'm doing wrong is real simple but I can't figure it out.
    > > Thanks for the help.
    > >
    > >"markn" <mnuttall@nospam.com> wrote:
    > >>
    > >>What are you puting on the command line?
    > >>
    > >>"Andrew McLellan" <amclellan@nospamYahoo.com> wrote:
    > >>>
    > >>>I have a basic program that deletes a file. The problem I'm having is

    > >passing
    > >>>the file name to the class. I've tried putting the file in the same directory
    > >>>as the program, adding the full path to the file and putting it in the

    > >root
    > >>>directory but neither worked. The class is able to read in the file name
    > >>>but when it gets to the Delete method an exception is thrown when it tries
    > >>>to create a new file object. I assume I'm not passing the parameter correctly.
    > >>> Any help would be greatly appreciated.
    > >>>Regards
    > >>>Andrew
    > >>>
    > >>>import java.io.*;
    > >>>
    > >>>public class Delete {
    > >>> public static void main(String[] args){
    > >>> if(args.length != 1){
    > >>> System.err.print("Usage: java Delete <file or directory>");
    > >>> System.exit(0);
    > >>> }
    > >>>
    > >>> try{ Delete(args[0]); }
    > >>> catch(IllegalArgumentException e){
    > >>> System.err.print(e.getMessage());
    > >>> }
    > >>> }//end main
    > >>>
    > >>>public static void Delete(String filename){
    > >>> //create a file object to represent the file name
    > >>> File f = new File(filename); /**ERROR HERE**/
    > >>>
    > >>>
    > >>

    > >



Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center