To invoke a Java ".class" file from JSP

V

Vivek

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.
 
G

gaurav v bagga

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
 
A

Alex Hunsley

gaurav said:
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

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"});
 
A

Alex Hunsley

Vivek said:
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.

Should have asked already - why exactly do you want to do this? What is
the end aim?
 
A

Andy Dingley

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.
 
A

Alex Hunsley

Vivek said:
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.

Should have asked already - why exactly do you want to do this? What is
the end aim?
 
A

Alex Hunsley

Vivek said:
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.

Should have asked already - why exactly do you want to do this? What is
the end aim?
 
A

Alex Hunsley

gaurav said:
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

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"});
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top