Results 1 to 13 of 13
Thread: reflection invoke method
- 11-16-2010, 08:52 AM #1
Member
- Join Date
- Nov 2010
- Posts
- 7
- Rep Power
- 0
reflection invoke method
Hi,
I'm invoking an external .class program using java reflection as follows:
My question is: How can I get the input and output streams for that external program?Java Code:Class<?> program = Class.forName("name"); Method m= program.getMethod("main",new Class[]{String[].class} ); ..... m.invoke(null, new Object[] { input});
Thanks!
- 11-16-2010, 09:16 AM #2
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
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().
- 11-16-2010, 01:38 PM #3
Member
- Join Date
- Nov 2010
- Posts
- 7
- Rep Power
- 0
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.
- 11-16-2010, 01:55 PM #4
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
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:
Then calling it using similar code to yours above will result in "Hello" being printed out to the console.Java Code:public class Someting { public static void main(String[] args) { System.out.println("Hello"); } }
- 11-16-2010, 02:15 PM #5
Member
- Join Date
- Nov 2010
- Posts
- 7
- Rep Power
- 0
if the external class looks like the following:
I want to run it several times but with different inputs....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); } }
so how can i assign the input each time?
- 11-16-2010, 02:20 PM #6
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
System.in reads from the keyboard (normally)...so just give it different input from the keyboard?
What is it you are attempting to do?
- 11-16-2010, 02:25 PM #7
Member
- Join Date
- Nov 2010
- Posts
- 7
- Rep Power
- 0
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.
- 11-16-2010, 03:02 PM #8
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
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?
- 11-16-2010, 03:22 PM #9
Member
- Join Date
- Nov 2010
- Posts
- 7
- Rep Power
- 0
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.
- 11-16-2010, 03:40 PM #10
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
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-16-2010, 05:04 PM #11
Member
- Join Date
- Nov 2010
- Posts
- 7
- Rep Power
- 0
Many thanks Tolls!
Decided to go with Runtime exec instead of reflection.
- 11-16-2010, 05:11 PM #12
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
How are you planning on reading the output?
Or supplying input for that matter?
- 11-16-2010, 05:43 PM #13
Member
- Join Date
- Nov 2010
- Posts
- 7
- Rep Power
- 0
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
-
onchange invoke java object method and submit form
By marckamga in forum JavaServer Pages (JSP) and JSTLReplies: 0Last Post: 10-14-2010, 06:26 AM -
Display part of matching method using Reflection
By finder255 in forum Advanced JavaReplies: 0Last Post: 09-23-2010, 08:04 PM -
Reflection - How to determine the method parameter class
By bazong in forum Advanced JavaReplies: 4Last Post: 06-18-2010, 10:08 AM -
[SOLVED] I want to Invoke an Instance Method as a Parameter is it Possible?
By Laythe in forum New To JavaReplies: 10Last Post: 06-12-2009, 06:41 PM -
Getting method names using Reflection
By Java Tip in forum Java TipReplies: 0Last Post: 01-24-2008, 03:18 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks