Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #29950 > unrolled thread
| Started by | Knute Johnson <nospam@rabbitbrush.frazmtn.com> |
|---|---|
| First post | 2014-09-12 08:11 -0700 |
| Last post | 2014-09-18 00:10 +0200 |
| Articles | 20 — 7 participants |
Back to article view | Back to comp.lang.java.programmer
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
| From | Knute Johnson <nospam@rabbitbrush.frazmtn.com> |
|---|---|
| Date | 2014-09-12 08:11 -0700 |
| Subject | How does it know? |
| Message-ID | <luv2fd$j58$1@dont-email.me> |
Give two classes:
public class test {
public static void main(String... args) {
System.out.println("calling test1.main()");
test1.main();
}
}
public class test1 {
public static void main(String... args) {
System.out.println("test1");
}
}
Compiling test.jar and running test produces the following output:
C:\Users\Knute Johnson>java test
calling test1.main()
test1
Adding the .class files to a .jar and running it produces the same:
C:\Users\Knute Johnson>java -jar test.jar
calling test1.main()
test1
I have two questions, the first is how does the test1 class get loaded
when test is run from the .jar? When all the .class files are in the
working directory they are in the CLASSPATH. Are all of the classes in
a .jar in the CLASSPATH too?
The second question is, what type is test.main()? I want to be able to
put it into a Map.
Thanks,
knute...
[toc] | [next] | [standalone]
| From | Knute Johnson <nospam@rabbitbrush.frazmtn.com> |
|---|---|
| Date | 2014-09-12 08:36 -0700 |
| Message-ID | <luv3up$te6$1@dont-email.me> |
| In reply to | #29950 |
On 9/12/2014 08:11, Knute Johnson wrote: > The second question is, what type is test.main()? I want to be able to > put it into a Map. Now that I think about it, I don't think that is possible. k...
[toc] | [prev] | [next] | [standalone]
| From | Steven Simpson <ss@domain.invalid> |
|---|---|
| Date | 2014-09-12 17:36 +0100 |
| Message-ID | <p1kbeb-rqd.ln1@s.simpson148.btinternet.com> |
| In reply to | #29951 |
On 12/09/14 16:36, Knute Johnson wrote:
> On 9/12/2014 08:11, Knute Johnson wrote:
>> The second question is, what type is test.main()? I want to be able to
>> put it into a Map.
>
> Now that I think about it, I don't think that is possible.
import java.util.*;
public class TestApplication {
public interface Application {
void main(String... args) throws Exception;
}
public static void main(String[] args) {
Map<String, Application> map = new HashMap<>();
map.put("self", TestApplication::main);
}
}
--
ss at comp dot lancs dot ac dot uk
[toc] | [prev] | [next] | [standalone]
| From | Donkey Hottie <donkey@fredriksson.dy.fi> |
|---|---|
| Date | 2014-09-12 21:40 +0300 |
| Message-ID | <6arbeb-7lt.ln1@tempest.fredriksson.dy.fi> |
| In reply to | #29953 |
12.09.2014, 19:36, Steven Simpson kirjoitti:
> On 12/09/14 16:36, Knute Johnson wrote:
>> On 9/12/2014 08:11, Knute Johnson wrote:
>>> The second question is, what type is test.main()? I want to be able to
>>> put it into a Map.
>>
>> Now that I think about it, I don't think that is possible.
>
> import java.util.*;
>
> public class TestApplication {
> public interface Application {
> void main(String... args) throws Exception;
> }
>
> public static void main(String[] args) {
> Map<String, Application> map = new HashMap<>();
> map.put("self", TestApplication::main);
> }
> }
>
>
>
That does not compute. Or is this some Java 8 new feature? I thought
methods are not "first class citizens" in Java...
I would say
map.put("something", new Application {
void main(String... args) throws Exception
{
throw new Exception("WTF?");
}
});
--
Avert misunderstanding by calm, poise, and balance.
[toc] | [prev] | [next] | [standalone]
| From | Joerg Meier <joergmmeier@arcor.de> |
|---|---|
| Date | 2014-09-13 11:57 +0200 |
| Message-ID | <1w9tll41jetk9$.1kjqmgkts927n$.dlg@40tude.net> |
| In reply to | #29954 |
On Fri, 12 Sep 2014 21:40:38 +0300, Donkey Hottie wrote:
> 12.09.2014, 19:36, Steven Simpson kirjoitti:
>> On 12/09/14 16:36, Knute Johnson wrote:
>>> On 9/12/2014 08:11, Knute Johnson wrote:
>>>> The second question is, what type is test.main()? I want to be able to
>>>> put it into a Map.
>>> Now that I think about it, I don't think that is possible.
>> import java.util.*;
>> public class TestApplication {
>> public interface Application {
>> void main(String... args) throws Exception;
>> }
>> public static void main(String[] args) {
>> Map<String, Application> map = new HashMap<>();
>> map.put("self", TestApplication::main);
>> }
>> }
> That does not compute. Or is this some Java 8 new feature? I thought
> methods are not "first class citizens" in Java...
Yes, that is part of the new Java 8 changes surrounding the Lambdas.
Liebe Gruesse,
Joerg
--
Ich lese meine Emails nicht, replies to Email bleiben also leider
ungelesen.
[toc] | [prev] | [next] | [standalone]
| From | Knute Johnson <nospam@rabbitbrush.frazmtn.com> |
|---|---|
| Date | 2014-09-12 16:18 -0700 |
| Message-ID | <luvuvm$90e$1@dont-email.me> |
| In reply to | #29950 |
On 9/12/2014 09:18, Stefan Ram wrote:
> Knute Johnson <nospam@rabbitbrush.frazmtn.com> writes:
>> Adding the .class files to a .jar and running it produces the same:
>
> To reproduce what you are doing, it might help, when you
> give /every/ command used in the sequence used in addition
> to the source code, if the problem should still persist.
>
>> The second question is, what type is test.main()?
>> I want to be able to put it into a Map.
>
> public final class Main
> { public static void main( final java.lang.String[] args )
> { final java.util.Map
> < java.util.function.Consumer< java.lang.String[] >,
> java.util.function.Consumer< java.lang.String[] >> map =
> new java.util.HashMap<>();
> map.put( Main::main, Main::main ); }}
>
What is Main::main, is it a Method? You can't call Main::main.invoke().
I'd like to figure out a way to store some sort of a reference to
Main.main() so that I could execute it.
Maybe I'm barking up a non-existent tree?
Thanks,
knute...
[toc] | [prev] | [next] | [standalone]
| From | Mike Amling <mamling@chaff.us> |
|---|---|
| Date | 2014-09-14 08:52 -0500 |
| Message-ID | <c7lkrtF1khaU1@mid.individual.net> |
| In reply to | #29959 |
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==
[toc] | [prev] | [next] | [standalone]
| From | Knute Johnson <nospam@rabbitbrush.frazmtn.com> |
|---|---|
| Date | 2014-09-14 07:48 -0700 |
| Message-ID | <lv49sd$kuv$1@dont-email.me> |
| In reply to | #29975 |
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...
[toc] | [prev] | [next] | [standalone]
| From | Roedy Green <see_website@mindprod.com.invalid> |
|---|---|
| Date | 2014-09-12 18:11 -0700 |
| Message-ID | <9b671als0s1psm43s3outq5culinesokr0@4ax.com> |
| In reply to | #29950 |
On Fri, 12 Sep 2014 08:11:39 -0700, Knute Johnson <nospam@rabbitbrush.frazmtn.com> wrote, quoted or indirectly quoted someone who said : >When all the .class files are in the >working directory they are in the CLASSPATH. Are all of the classes in >a .jar in the CLASSPATH too? When you have a jar, the classpath is ignored, unless you have one inside the jar. All the classes in the jar are potentially loadable. -- Roedy Green Canadian Mind Products http://mindprod.com There are only two industries that refer to their customers as users. ~ Edward Tufte (born: 1942 age: 72)
[toc] | [prev] | [next] | [standalone]
| From | Joerg Meier <joergmmeier@arcor.de> |
|---|---|
| Date | 2014-09-13 11:59 +0200 |
| Message-ID | <1pkeamzafzu27.1juux6y2rf8ai$.dlg@40tude.net> |
| In reply to | #29961 |
On Fri, 12 Sep 2014 18:11:05 -0700, Roedy Green wrote: > On Fri, 12 Sep 2014 08:11:39 -0700, Knute Johnson > <nospam@rabbitbrush.frazmtn.com> wrote, quoted or indirectly quoted > someone who said : >>When all the .class files are in the >>working directory they are in the CLASSPATH. Are all of the classes in >>a .jar in the CLASSPATH too? > When you have a jar, the classpath is ignored, unless you have one > inside the jar. All the classes in the jar are potentially loadable. That is true only if you use the jar together with the -jar parameter. If you instead add a jar to the -classpath, you can have multiple jars (or directories) on the classpath. Liebe Gruesse, Joerg -- Ich lese meine Emails nicht, replies to Email bleiben also leider ungelesen.
[toc] | [prev] | [next] | [standalone]
| From | Knute Johnson <nospam@rabbitbrush.frazmtn.com> |
|---|---|
| Date | 2014-09-14 07:51 -0700 |
| Message-ID | <lv4a1o$kuv$3@dont-email.me> |
| In reply to | #29967 |
On 9/13/2014 02:59, Joerg Meier wrote: > On Fri, 12 Sep 2014 18:11:05 -0700, Roedy Green wrote: > >> On Fri, 12 Sep 2014 08:11:39 -0700, Knute Johnson >> <nospam@rabbitbrush.frazmtn.com> wrote, quoted or indirectly quoted >> someone who said : >>> When all the .class files are in the >>> working directory they are in the CLASSPATH. Are all of the classes in >>> a .jar in the CLASSPATH too? >> When you have a jar, the classpath is ignored, unless you have one >> inside the jar. All the classes in the jar are potentially loadable. > > That is true only if you use the jar together with the -jar parameter. If > you instead add a jar to the -classpath, you can have multiple jars (or > directories) on the classpath. > > Liebe Gruesse, > Joerg > So any classes on the CLASSPATH are potentially loadable? knute...
[toc] | [prev] | [next] | [standalone]
| From | Arne Vajhøj <arne@vajhoej.dk> |
|---|---|
| Date | 2014-09-14 22:24 -0400 |
| Message-ID | <54164df8$0$302$14726298@news.sunsite.dk> |
| In reply to | #29978 |
On 9/14/2014 10:51 AM, Knute Johnson wrote: > On 9/13/2014 02:59, Joerg Meier wrote: >> On Fri, 12 Sep 2014 18:11:05 -0700, Roedy Green wrote: >>> On Fri, 12 Sep 2014 08:11:39 -0700, Knute Johnson >>> <nospam@rabbitbrush.frazmtn.com> wrote, quoted or indirectly quoted >>> someone who said : >>>> When all the .class files are in the >>>> working directory they are in the CLASSPATH. Are all of the classes in >>>> a .jar in the CLASSPATH too? >>> When you have a jar, the classpath is ignored, unless you have one >>> inside the jar. All the classes in the jar are potentially loadable. >> >> That is true only if you use the jar together with the -jar parameter. If >> you instead add a jar to the -classpath, you can have multiple jars (or >> directories) on the classpath. > > So any classes on the CLASSPATH are potentially loadable? Since classpath = path where classes can be laoded from then ... :-) Arne
[toc] | [prev] | [next] | [standalone]
| From | Knute Johnson <nospam@rabbitbrush.frazmtn.com> |
|---|---|
| Date | 2014-09-14 07:50 -0700 |
| Message-ID | <lv4a0f$kuv$2@dont-email.me> |
| In reply to | #29961 |
On 9/12/2014 18:11, Roedy Green wrote: > On Fri, 12 Sep 2014 08:11:39 -0700, Knute Johnson > <nospam@rabbitbrush.frazmtn.com> wrote, quoted or indirectly quoted > someone who said : > >> When all the .class files are in the >> working directory they are in the CLASSPATH. Are all of the classes in >> a .jar in the CLASSPATH too? > > When you have a jar, the classpath is ignored, unless you have one > inside the jar. All the classes in the jar are potentially loadable. That was my question. So any class in a jar can be loaded and a static method called on it by using its classname.methodname(). I assume then if the jar manifest has a Class-Path: statement pointing to another jar, all those classes are potentially loadable too? knute...
[toc] | [prev] | [next] | [standalone]
| From | Arne Vajhøj <arne@vajhoej.dk> |
|---|---|
| Date | 2014-09-14 22:26 -0400 |
| Message-ID | <54164e4d$0$302$14726298@news.sunsite.dk> |
| In reply to | #29977 |
On 9/14/2014 10:50 AM, Knute Johnson wrote: > On 9/12/2014 18:11, Roedy Green wrote: >> On Fri, 12 Sep 2014 08:11:39 -0700, Knute Johnson >> <nospam@rabbitbrush.frazmtn.com> wrote, quoted or indirectly quoted >> someone who said : >> >>> When all the .class files are in the >>> working directory they are in the CLASSPATH. Are all of the classes in >>> a .jar in the CLASSPATH too? >> >> When you have a jar, the classpath is ignored, unless you have one >> inside the jar. All the classes in the jar are potentially loadable. > > That was my question. So any class in a jar can be loaded and a static > method called on it by using its classname.methodname(). I assume then > if the jar manifest has a Class-Path: statement pointing to another jar, > all those classes are potentially loadable too? Yes. If it is in the classpath then it can be loaded - how the path got in the classpath should not matter. Arne
[toc] | [prev] | [next] | [standalone]
| From | Knute Johnson <nospam@rabbitbrush.frazmtn.com> |
|---|---|
| Date | 2014-09-17 14:52 -0700 |
| Message-ID | <lvcvq9$7gn$1@dont-email.me> |
| In reply to | #29984 |
On 9/14/2014 19:26, Arne Vajhøj wrote: > On 9/14/2014 10:50 AM, Knute Johnson wrote: >> On 9/12/2014 18:11, Roedy Green wrote: >>> On Fri, 12 Sep 2014 08:11:39 -0700, Knute Johnson >>> <nospam@rabbitbrush.frazmtn.com> wrote, quoted or indirectly quoted >>> someone who said : >>> >>>> When all the .class files are in the >>>> working directory they are in the CLASSPATH. Are all of the classes in >>>> a .jar in the CLASSPATH too? >>> >>> When you have a jar, the classpath is ignored, unless you have one >>> inside the jar. All the classes in the jar are potentially loadable. >> >> That was my question. So any class in a jar can be loaded and a static >> method called on it by using its classname.methodname(). I assume then >> if the jar manifest has a Class-Path: statement pointing to another jar, >> all those classes are potentially loadable too? > > Yes. > > If it is in the classpath then it can be loaded - how the path > got in the classpath should not matter. > > Arne > > Thanks! knute...
[toc] | [prev] | [next] | [standalone]
| From | Roedy Green <see_website@mindprod.com.invalid> |
|---|---|
| Date | 2014-09-12 18:13 -0700 |
| Message-ID | <4e671at8gb4i3dtou33c5ifnjuubnl83j8@4ax.com> |
| In reply to | #29950 |
On Fri, 12 Sep 2014 08:11:39 -0700, Knute Johnson <nospam@rabbitbrush.frazmtn.com> wrote, quoted or indirectly quoted someone who said : > >I have two questions, the first is how does the test1 class get loaded >when test is run from the .jar? It is the perfectly normal class load mechanism. See http://mindprod.com/jgloss/classpath.html for various explanations how it works. main is an ordinary static method. It can be called from other classes just like any other public static method. -- Roedy Green Canadian Mind Products http://mindprod.com There are only two industries that refer to their customers as users. ~ Edward Tufte (born: 1942 age: 72)
[toc] | [prev] | [next] | [standalone]
| From | Mike Amling <mamling@chaff.us> |
|---|---|
| Date | 2014-09-14 08:29 -0500 |
| Message-ID | <c7ljgbF19e0U1@mid.individual.net> |
| In reply to | #29950 |
On 9/12/14 10:11 AM, Knute Johnson wrote:
> Given two classes:
>
> public class test {
> public static void main(String... args) {
> System.out.println("calling test1.main()");
> test1.main();
> }
> }
>
> public class test1 {
> public static void main(String... args) {
> System.out.println("test1");
> }
> }
How did you get this to compile? Does the source actually call
test1.main(args)?
--Mike Amling
bWFpbihqYXZhLmxhbmcuU3RyaW5nW10pIGluIHRlc3QxIGNhbm5vdCBiZSBhcHBsaWVkIHRvICgp
[toc] | [prev] | [next] | [standalone]
| From | Knute Johnson <nospam@rabbitbrush.frazmtn.com> |
|---|---|
| Date | 2014-09-14 07:53 -0700 |
| Message-ID | <lv4a5r$kuv$4@dont-email.me> |
| In reply to | #29974 |
On 9/14/2014 06:29, Mike Amling wrote:
> On 9/12/14 10:11 AM, Knute Johnson wrote:
>> Given two classes:
>>
>> public class test {
>> public static void main(String... args) {
>> System.out.println("calling test1.main()");
>> test1.main();
>> }
>> }
>>
>> public class test1 {
>> public static void main(String... args) {
>> System.out.println("test1");
>> }
>> }
>
> How did you get this to compile? Does the source actually call
> test1.main(args)?
>
Yes and it works fine too. That is why I am curious to the mechanism
and wondering if there is a way to get from a String that contains the
class name to executing the class' main method.
knute...
[toc] | [prev] | [next] | [standalone]
| From | Roedy Green <see_website@mindprod.com.invalid> |
|---|---|
| Date | 2014-09-17 00:40 -0700 |
| Message-ID | <1cei1alcbdilpqv8qi2qih082nimv9f315@4ax.com> |
| In reply to | #29979 |
On Sun, 14 Sep 2014 07:53:48 -0700, Knute Johnson <nospam@rabbitbrush.frazmtn.com> wrote, quoted or indirectly quoted someone who said : >there is a way to get from a String that contains the >class name to executing the class' main method. see http://mindprod.com/classforname.html The tricky part is you must make sure all the classes you load this way are available in the jar or on the classpath. The dynamically loaded class implements an Interface or an abstract class. That gives you a handle to call its various instance methods. The problem is, interfaces don't have static methods, so there is no way to refer to "main". To get at it, you would need to use reflection. A simpler technique would be to invent an instance main method. Another technique is to have a lookup by string to class. Then you can call static methods too. -- Roedy Green Canadian Mind Products http://mindprod.com There are only two industries that refer to their customers as users. ~ Edward Tufte (born: 1942 age: 72)
[toc] | [prev] | [next] | [standalone]
| From | Joerg Meier <joergmmeier@arcor.de> |
|---|---|
| Date | 2014-09-18 00:10 +0200 |
| Message-ID | <vcudr4krx8h0.1qgb9fjfqvc92.dlg@40tude.net> |
| In reply to | #30002 |
On Wed, 17 Sep 2014 00:40:02 -0700, Roedy Green wrote:
> The problem is, interfaces don't have static methods, so there is no
> way to refer to "main".
As of Java 8, that is no longer correct. This will compile:
public interface App {
public static void main(String[] args) {}
}
as will:
public class Main implements App {
public static void main(String[] args) {}
}
Although I am uncertain of how much help that would actually be in this
situation.
Liebe Gruesse,
Joerg
--
Ich lese meine Emails nicht, replies to Email bleiben also leider
ungelesen.
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.java.programmer
csiph-web