Results 1 to 13 of 13
  1. #1
    pprl is offline Member
    Join Date
    Nov 2010
    Posts
    7
    Rep Power
    0

    Default reflection invoke method

    Hi,

    I'm invoking an external .class program using java reflection as follows:

    Java Code:
    Class<?> program = Class.forName("name");
    Method m= program.getMethod("main",new Class[]{String[].class} );
    .....
    m.invoke(null, new Object[] { input});
    My question is: How can I get the input and output streams for that external program?

    Thanks!

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,044
    Rep Power
    20

    Default

    That's not an external program.
    That's simply executing a method on a class.
    OK, it's a main() method, but in this case it could be foo().
    There are no "input and output streams", anymore than there would be with a call to foo().

  3. #3
    pprl is offline Member
    Join Date
    Nov 2010
    Posts
    7
    Rep Power
    0

    Default

    is it possible to capture the output?:confused:

    ok I think I got the output part...


    but how to assign values to system.in of the called class?

    help please...
    Last edited by pprl; 11-16-2010 at 01:51 PM.

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,044
    Rep Power
    20

    Default

    System.in is not something special to the called class.
    System.in will still work.

    The output will still go wherever it went before (System.out presumably).

    If main() looked like this:
    Java Code:
    public class Someting {
        public static void main(String[] args) {
            System.out.println("Hello");
        }
    }
    Then calling it using similar code to yours above will result in "Hello" being printed out to the console.

  5. #5
    pprl is offline Member
    Join Date
    Nov 2010
    Posts
    7
    Rep Power
    0

    Default

    if the external class looks like the following:

    Java Code:
    public class Someting {
        public static void main(String[] args) {
            BufferedReader b = new BufferedReader(new InputStreamReader(System.in));
          try{
            String name = b.readLine();
            }catch(Exception e){}
    
            System.out.println(name);
        }
    }
    I want to run it several times but with different inputs....

    so how can i assign the input each time?

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,044
    Rep Power
    20

    Default

    System.in reads from the keyboard (normally)...so just give it different input from the keyboard?

    What is it you are attempting to do?

  7. #7
    pprl is offline Member
    Join Date
    Nov 2010
    Posts
    7
    Rep Power
    0

    Default

    I have several text files with the input, I read them in the first class and I need to assign the data to the external class called with the invoke method.

    So for each input file I read, I need to call the external class once.

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,044
    Rep Power
    20

    Default

    I suspect you're confusing yourself.
    From Java's point of view that method is not in an external class. It is simply another method it has been asked to called. No different to calling Something.main() directly.

    Are you testing the code in main() or something?

  9. #9
    pprl is offline Member
    Join Date
    Nov 2010
    Posts
    7
    Rep Power
    0

    Default

    what I need is to write to the System.in from the calling program...


    is there a way to write to System.in, like for example with outputstreamwriter or some other method?

    I'm trying to create an automated input writer for the external program, so I think I need to have a writer of some kind and write to System.in or the standard input of the operating system...
    Last edited by pprl; 11-16-2010 at 03:29 PM.

  10. #10
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,044
    Rep Power
    20

    Default

    Then you'll need two threads, and to change System.in to point to another source that you can then write to from the original thread. The second thread will be the one that fires off the call to main().

    Exactly how you do that I'll have to leave up to you, but you can't do this in a single thread.

  11. #11
    pprl is offline Member
    Join Date
    Nov 2010
    Posts
    7
    Rep Power
    0

    Default

    Many thanks Tolls!

    Decided to go with Runtime exec instead of reflection.

  12. #12
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,044
    Rep Power
    20

    Default

    How are you planning on reading the output?
    Or supplying input for that matter?

  13. #13
    pprl is offline Member
    Join Date
    Nov 2010
    Posts
    7
    Rep Power
    0

    Default

    I found this example:

    Java Runtime Environment (JRE) - Runtime.getRuntime().exec(java test)

    on the last post...

    I tested it and it works, now I'm trying to adapt the idea to my program..

Similar Threads

  1. onchange invoke java object method and submit form
    By marckamga in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 10-14-2010, 06:26 AM
  2. Display part of matching method using Reflection
    By finder255 in forum Advanced Java
    Replies: 0
    Last Post: 09-23-2010, 08:04 PM
  3. Replies: 4
    Last Post: 06-18-2010, 10:08 AM
  4. Replies: 10
    Last Post: 06-12-2009, 06:41 PM
  5. Getting method names using Reflection
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-24-2008, 03:18 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •