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**/
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**/
>
>
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**/
>>
>>
>
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**/
>>>
>>>
>>
>
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**/
> >>>
> >>>
> >>
> >