To invoke a Java ".class" file from JSP

Discussion in 'Java' started by Vivek, Jan 30, 2007.

  1. Vivek

    Vivek Guest

    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.
     
    Vivek, Jan 30, 2007
    #1
    1. Advertising

  2. Re: To invoke a Java ".class" file from JSP

    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
     
    gaurav v bagga, Jan 30, 2007
    #2
    1. Advertising

  3. Vivek

    Alex Hunsley Guest

    Re: To invoke a Java ".class" file from JSP

    gaurav v bagga wrote:
    > 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"});
     
    Alex Hunsley, Jan 30, 2007
    #3
  4. Vivek

    Alex Hunsley Guest

    Vivek wrote:
    > 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?
     
    Alex Hunsley, Jan 30, 2007
    #4
  5. Vivek

    Andy Dingley Guest

    Re: To invoke a Java ".class" file from JSP

    On 30 Jan, 14:41, "Vivek" <> wrote:

    > 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.
     
    Andy Dingley, Jan 30, 2007
    #5
  6. Vivek

    Alex Hunsley Guest

    Vivek wrote:
    > 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?
     
    Alex Hunsley, Jan 31, 2007
    #6
  7. Vivek

    Alex Hunsley Guest

    Vivek wrote:
    > 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?
     
    Alex Hunsley, Jan 31, 2007
    #7
  8. Vivek

    Alex Hunsley Guest

    Re: To invoke a Java ".class" file from JSP

    gaurav v bagga wrote:
    > 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"});
     
    Alex Hunsley, Jan 31, 2007
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    1
    Views:
    565
    Andrea Desole
    Dec 14, 2005
  2. Replies:
    0
    Views:
    4,442
  3. Replies:
    8
    Views:
    3,563
    Manish Pandit
    Oct 11, 2006
  4. Vivek
    Replies:
    3
    Views:
    350
    Eric Sosman
    Feb 22, 2007
  5. thiagoalgo

    JSP invoke Servlet

    thiagoalgo, Apr 9, 2007, in forum: Java
    Replies:
    2
    Views:
    467
Loading...

Share This Page