Jump to content


About Command Arguments


  • Please log in to reply
3 replies to this topic

#1 duguguhu

duguguhu

    Member

  • Members
  • PipPip
  • 10 posts

Posted 20 April 2005 - 04:20 PM

i 've  writed a  class with "Jcreater-IDE",but i need to pass arguments through the main(String []args) method,how can i do it ?
thank you in advance!

#2 E-E-R

E-E-R

    Advanced Member

  • Members
  • PipPipPip
  • 1,367 posts
  • Gender:Male
  • Location:The Netherlands
  • Interests:Programming, Gaming, DC

Posted 20 April 2005 - 08:08 PM

Configure
> Options
> JDK Tools

Select the tool type: "Run application"
Select <Default>
Open the tab 'parameters'
Select 'Prompt for main method Arguments'

click OK on all screens and you'll be all set :)
"Many are persistently in pursuit of the way they have chosen; few in pursuit of the goal"

#3 duguguhu

duguguhu

    Member

  • Members
  • PipPip
  • 10 posts

Posted 21 April 2005 - 03:09 AM

QUOTE(E-E-R @ Apr 21 2005, 03:08 AM)
Configure
> Options
> JDK Tools

Select the tool type: "Run application"
Select <Default>
Open the tab 'parameters'
Select 'Prompt for main method Arguments'

click OK on all screens and you'll be all set :)

View Post


thank you very much!
i've do as you said but there sill an Exception :
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
        at TestMainArgs.main(TestMainArgs.java:3)
my sample code is :
public class TestMainArgs{
public static void main(String []args){
System.out.println(args[0]);
}
}
can you give me more details about the "Configure". thank you in advance!

#4 Mr.Mofo

Mr.Mofo

    Newbie

  • Members
  • Pip
  • 1 posts

Posted 28 April 2005 - 04:24 PM

QUOTE(E-E-R @ Apr 20 2005, 03:08 PM)
Configure
> Options
> JDK Tools

Select the tool type: "Run application"
Select <Default>
Open the tab 'parameters'
Select 'Prompt for main method Arguments'

click OK on all screens and you'll be all set :)

View Post



If I may modify your instructions:
Select the tool type: "Run application"
Select <Default> (it is grayed out but you can still click it)
Click "Edit" button
Click the tab 'parameters'
Select 'Prompt for main method Arguments'
Hit "apply"
Hit "ok"
good to go!



QUOTE(duguguhu @ Apr 20 2005, 10:09 PM)
thank you very much!
i've do as you said but there sill an Exception :
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
        at TestMainArgs.main(TestMainArgs.java:3)
my sample code is :
public class TestMainArgs{
public static void main(String []args){
  System.out.println(args[0]);
}
}
can you give me more details about the "Configure". thank you in advance!

View Post




The exception you are getting is because your command line arguments are empty. When the little box shows up for "enter arguments" you put in whatever you want "java TestMainArgs ArgumentZero"


Here is an example from one of my programs that takes command line argument, converts it to an integer, and if the argument passed is anything but integer it exits the program:
CODE

try{
shift = Integer.parseInt(args[0]);//grab first arg, convert to int
}//end try
catch(Exception e){//catch ALL exceptions
System.out.println("Bad argument");//print to console
System.exit(0);//exit program
}//end catch



if you want to try your program without actually "using" cmd line, just do this right under your decleration for main method:
CODE

public class TestMainArgs{
public static void main(String []args){
               args[0] = "testtesttest";
 System.out.println("Args of 0 " + args[0]);
 System.out.println("Length of args 0: " + args[0].length());
}
}


hope that helps  ^_^