hi
I have 2 files, a Java ".class" file and a JSP file
I need to invoke the Java ".class" file from the JSP file.
from the JSP file I have to pass command line arguments to the Java
".class" file ( by command line arguments i mean the String args[] of
the "public static void main(string args[])" ).
and i have to display the output of the Java ".class" file in a web
page using JSP.
can anybody help me out.
gaurav v bagga - 30 Jan 2007 15:10 GMT
hi,
public static <T> void invokeMethodDynamically(Class klassType,
Class<T> argumentsType, String methodName, Object object,T
valueSentForMethod) throws SecurityException,
IllegalArgumentException, NoSuchMethodException,
IllegalAccessException, InvocationTargetException {
Method method;
method = klassType.getMethod(methodName, argumentsType);
Object returnValue = method.invoke(object,valueSentForMethod);
}
you can do something like this.
alternatively
you can use
<Object> o = Class.forName("class_name").newInstance();
then use o as per you needs
hope this helps
regards
gaurav
Alex Hunsley - 30 Jan 2007 19:57 GMT
> hi,
>
[quoted text clipped - 17 lines]
> <Object> o = Class.forName("class_name").newInstance();
> then use o as per you needs
Reflection should be avoided unless you can't do without it.
What is wrong with accessing the class directly, if he knows the class's
name?
E.g. if class is called MyThing:
MyThing.main(new String[] {"arg1", "arg2", "etc"});
Alex Hunsley - 31 Jan 2007 11:15 GMT
> hi,
>
[quoted text clipped - 17 lines]
> <Object> o = Class.forName("class_name").newInstance();
> then use o as per you needs
Reflection should be avoided unless you can't do without it.
What is wrong with accessing the class directly, if he knows the class's
name?
E.g. if class is called MyThing:
MyThing.main(new String[] {"arg1", "arg2", "etc"});
Alex Hunsley - 30 Jan 2007 20:08 GMT
> hi
> I have 2 files, a Java ".class" file and a JSP file
[quoted text clipped - 7 lines]
>
> can anybody help me out.
Should have asked already - why exactly do you want to do this? What is
the end aim?
Andy Dingley - 30 Jan 2007 20:36 GMT
> I have to pass command line arguments to the Java ".class" file
> ( by command line arguments i mean the String args[] of the "public static void main(string args[])" ).
You've answered your own question. Although this class has a method
signature that _allows_ it to be used as a command line app, it's
still just a plain old method that takes parameters. It doesn't
_have_to_ be used as a command line app. Give it some parameters and
you can call it quite happily from within JSP.
Alex Hunsley - 31 Jan 2007 11:13 GMT
> hi
> I have 2 files, a Java ".class" file and a JSP file
[quoted text clipped - 7 lines]
>
> can anybody help me out.
Should have asked already - why exactly do you want to do this? What is
the end aim?
Alex Hunsley - 31 Jan 2007 11:14 GMT
> hi
> I have 2 files, a Java ".class" file and a JSP file
[quoted text clipped - 7 lines]
>
> can anybody help me out.
Should have asked already - why exactly do you want to do this? What is
the end aim?