Reputation: 1925
Here's the code.
import java.lang.reflect.*;
class Invoke {
public static void main(String[] args) {
int ret;
if (args.length<2) {
System.out.println("Usage: Invoke <class> <method>");
return;
}
if (args.length == 2) {
ret = 2
} else {
System.out.println("Additional parameters not yet supported.");
return;
}
System.out.println("Results: " + ret);
}
}
The problem is, even if I run the program with something like...
java -cp Invoke;HelloJava4 Invoke HelloJava4 param1 param2 param3
... it still recognizes "param1 param2 param3" as one argument. Note: my system's classpath is set to C:\JavaSource
, so -cp Invoke;HelloJava4
makes it search the Invoke and HelloJava4 directories for Invoke.class and HelloJava4.class
If I do System.out.println(args.length);
, it will output the correct number of arguments given, but when I check it with the following if
statement, it runs the if
code block, not the else
code block.
if (args.length == 2) {
ret = 2
} else {
System.out.println("Additional parameters not supported yet.");
return;
}
What gives? :confused:
Here is the unedited code, in full:
import java.lang.reflect.*;
class Invoke {
public static void main(String[] args) {
Object ret;
for (String arg : args)
System.out.println(arg);
System.out.println("Count: " + args.length + " \n");
if (args.length<2) {
System.out.println("Usage: Invoke <class> <method>");
return;
}
try {
Class theClass = Class.forName(args[0]);
Method theMethod = theClass.getMethod(args[1]);
if (args.length == 2) {
System.out.println("Invoking method " + args[1] + " within class " + args[0]);
ret = theMethod.invoke(null);
} else {
// pass parameters to .invoke() if more than two args are given
// for now, just exit...
System.out.println("Parameter passing not yet supported.");
return;
}
System.out.println("Invoked static method: " + args[1]
+ " of class: " + args[0]
+ " with no args\nResults: " + ret);
} catch (ClassNotFoundException e) {
System.out.println("Class (" + args[0] + ") not found.");
} catch (NoSuchMethodException e2) {
System.out.println("Class (" + args[0] + ") found, but method does not exist.");
} catch (IllegalAccessException e3) {
System.out.println("Class (" + args[0] + ") and method found, but method is not accessible.");
} catch (InvocationTargetException e4) {
System.out.println("Method threw exception: " + e4.getTargetException() );
}
}
}
And here is the exact output it gives:
C:\JavaSource>cd invoke
C:\JavaSource>javac invoke.java
Note: invoke.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
C:\JavaSource>cd ..
C:\JavaSource>java -cp Invoke;HelloJava4 Invoke HelloJava4 param1 param2 p
aram3
HelloJava4
param1
param2
param3
Count: 4
Class (HelloJava4) found, but method does not exist.
Upvotes: 0
Views: 761
Reputation: 3059
java -cp Invoke;HelloJava4 Invoke HelloJava4 param1 param2 param3
That is not right. I don't know what OS nor what shell you use to execute that line.
I don't think the:
-cp Invoke;HelloJava4
is correct. (The reason I'm saying that is because you are experiencing none-reproducable problems.)
To verify, execute the snippet you gave us without any -cp or -classpath.
edit: If we assume the -cp argument is correct. Meaning that the jvm is started with the directories Invoke and HelloJava4 in the classpath (-cp/-classpath overrides the environmental variable specifying a classpath) the jvm will look in the directories Invoke and HelloJava4 for the Invoke class. This means that you are probably executing another class than you think you are.
You example code is in the default package. Just go to the directory where the sourcefile is and execute:
javac Invoke.java
java -cp . Invoke param1 param2 param3 [...]
(the point says the current directory is in the classpath ...)
And you should see a different result.
Upvotes: 2
Reputation: 3116
In your unedited code, the exception is thrown from this line.
Method theMethod = theClass.getMethod(args[1]);
It doesn't even reach the if/else condition for number of arguments.
Upvotes: 4