Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #29976

Re: How does it know?

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.fsmpi.rwth-aachen.de!eternal-september.org!feeder.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From Knute Johnson <nospam@rabbitbrush.frazmtn.com>
Newsgroups comp.lang.java.programmer
Subject Re: How does it know?
Date Sun, 14 Sep 2014 07:48:45 -0700
Organization A noiseless patient Spider
Lines 40
Message-ID <lv49sd$kuv$1@dont-email.me> (permalink)
References <luv2fd$j58$1@dont-email.me> <main-20140912181753@ram.dialup.fu-berlin.de> <luvuvm$90e$1@dont-email.me> <c7lkrtF1khaU1@mid.individual.net>
Mime-Version 1.0
Content-Type text/plain; charset=windows-1252; format=flowed
Content-Transfer-Encoding 7bit
Injection-Date Sun, 14 Sep 2014 14:48:45 +0000 (UTC)
Injection-Info mx05.eternal-september.org; posting-host="9f88abf500e70723148465e543f647d1"; logging-data="21471"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18Z5ItSQp/dBCZGDcpEQNlj"
User-Agent Mozilla/5.0 (Windows NT 6.3; WOW64; rv:31.0) Gecko/20100101 Thunderbird/31.1.1
In-Reply-To <c7lkrtF1khaU1@mid.individual.net>
Cancel-Lock sha1:2Ldk9Tr3iqRjlI8WfxEBhXTTFgM=
Xref csiph.com comp.lang.java.programmer:29976

Show key headers only | View raw


On 9/14/2014 06:52, Mike Amling wrote:
> On 9/12/14 6:18 PM, Knute Johnson wrote:
>>   I'd like to figure out a way to store some sort of a reference to
>> Main.main() so that I could execute it.
>
> Any reason you don't want to use plain old reflection?
>
> import java.lang.reflect.Method;
> import java.lang.reflect.InvocationTargetException;
> import java.util.HashMap;
> ...
>
> Method callme;
> try {
>      callme=test1.class.getDeclaredMethod("main",
>       new Class[]{new String[0].getClass()});
> } catch (final NoSuchMethodException n) {
>      return;
> }
> HashMap<String, Method> store=new HashMap<String, Method>();
> store.put("main", callme);
>
>
> try {
>      store.get("main").invoke(null, new Object[]{new String[0]});
> } catch (final IllegalAccessException a) {
> } catch (final InvocationTargetException t) {
> }
>
> --Mike Amling
> Tm8gbmVlZCBmb3IgMS44LCBqdXN0IDEuMg==

That works and I guess that's what I'm going to end up with.  I just 
thought that since you can put test1.main() in a program that you could 
get there from a String without quite so much code.

Thanks everybody for all the answers!

knute...

Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

How does it know? Knute Johnson <nospam@rabbitbrush.frazmtn.com> - 2014-09-12 08:11 -0700
  Re: How does it know? Knute Johnson <nospam@rabbitbrush.frazmtn.com> - 2014-09-12 08:36 -0700
    Re: How does it know? Steven Simpson <ss@domain.invalid> - 2014-09-12 17:36 +0100
      Re: How does it know? Donkey Hottie <donkey@fredriksson.dy.fi> - 2014-09-12 21:40 +0300
        Re: How does it know? Joerg Meier <joergmmeier@arcor.de> - 2014-09-13 11:57 +0200
  Re: How does it know? Knute Johnson <nospam@rabbitbrush.frazmtn.com> - 2014-09-12 16:18 -0700
    Re: How does it know? Mike Amling <mamling@chaff.us> - 2014-09-14 08:52 -0500
      Re: How does it know? Knute Johnson <nospam@rabbitbrush.frazmtn.com> - 2014-09-14 07:48 -0700
  Re: How does it know? Roedy Green <see_website@mindprod.com.invalid> - 2014-09-12 18:11 -0700
    Re: How does it know? Joerg Meier <joergmmeier@arcor.de> - 2014-09-13 11:59 +0200
      Re: How does it know? Knute Johnson <nospam@rabbitbrush.frazmtn.com> - 2014-09-14 07:51 -0700
        Re: How does it know? Arne Vajhøj <arne@vajhoej.dk> - 2014-09-14 22:24 -0400
    Re: How does it know? Knute Johnson <nospam@rabbitbrush.frazmtn.com> - 2014-09-14 07:50 -0700
      Re: How does it know? Arne Vajhøj <arne@vajhoej.dk> - 2014-09-14 22:26 -0400
        Re: How does it know? Knute Johnson <nospam@rabbitbrush.frazmtn.com> - 2014-09-17 14:52 -0700
  Re: How does it know? Roedy Green <see_website@mindprod.com.invalid> - 2014-09-12 18:13 -0700
  Re: How does it know? Mike Amling <mamling@chaff.us> - 2014-09-14 08:29 -0500
    Re: How does it know? Knute Johnson <nospam@rabbitbrush.frazmtn.com> - 2014-09-14 07:53 -0700
      Re: How does it know? Roedy Green <see_website@mindprod.com.invalid> - 2014-09-17 00:40 -0700
        Re: How does it know? Joerg Meier <joergmmeier@arcor.de> - 2014-09-18 00:10 +0200

csiph-web