Reputation: 2042
I'm a Java beginner and I'm confused about testing args.length at the begining of many codes, and why it's never gets higher than 0 in any of my codes?
import java.net.Socket;
import java.net.UnknownHostException;
import java.io.IOException;
public class LowPortScanner {
public static void main(String[] args) {
String host = "localhost";
if (args.length > 0) {
host = args[0];
}
for (int i = 1; i < 1024; i++) {
try {
Socket s = new Socket(host, i);
System.out.println("There is a server on port " + i + " of "
+ host);
}
catch (UnknownHostException ex) {
System.err.println(ex);
break;
}
catch (IOException ex) {}
} // end for
} // end main
} // end PortScanner
Upvotes: 0
Views: 3637
Reputation: 4836
args
in public static void main(String[] args
is the String
array of arguments passed from command line.
java LowPortScanner argument1 argument2
if you try the above command args.length
will return 2
.
As far as question of checking the length it is done for java programs which can take command line arguments and if arguments are not passed then they prompt for input.
if(args.length >0 ){
//proceed using passed arguments
}else{
//proceed with some default value
}
You are running your program using java LowPortScanner
hence no arguments are passed and args.length
is always zero.
Moreover if you don't pass any argument and use host=args[0]
you will get ArrayIndexOutOfBoundsException
.
Upvotes: 0
Reputation: 6110
The variable String[] args hold all the parameter pass to the program thorough command line if you are not passing any argument then the length of args become 0. Now it's better to check it's length before accessing it other wise there is chance to get ArrayIndexOutOfBoundsException if it's size is 0
Upvotes: 0
Reputation: 5913
If you are calling your program from CMD or bash you can asign ARGuments it like
java LowPortScanner google.com
Then "google.com" is your args[0]. When your program supports commandline attributes it is recommended to test if the given arguments are corret.
Upvotes: 0
Reputation: 5506
You have to inputs to main
mathod from Command prompt.
like below
java LowPortScanner TEST1 TEST2
Upvotes: 1
Reputation: 25725
It depends on how you invoke the Java class file. In command prompt or bash shell:
java LowPortScanner Argument1
typing the above line in the command prompt/bash will cause the argument count to increase to 1. (because Argument1
is one argument, after the class file LowPortScanner
)
java LowPortScanner Argument1 Argument2
the above line will make argument count to increase to 2.
hence args.length
will be 2
in the second case and 1
in the first case.
Upvotes: 0
Reputation: 17029
If there are no command arguments the args[0]
will fail. This is why it must be protected.
Upvotes: 0
Reputation: 45058
Because if it is not tested then an exception would be thrown because host = args[0];
would illegal.
However, this doesn't look like it's going to help much, an empty or null
host
looks like it would cause further problems.
If the length of args
is always 0
, then be sure you're actually passing in parameter arguments.
Upvotes: 0