Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > inheriting a main method

Reply
Thread Tools

inheriting a main method

 
 
Arne Vajhøj
Guest
Posts: n/a
 
      09-18-2009
Mike Schilling wrote:
> Arne Vajhøj wrote:
>>>> If we can assume that Java version >= 1.5, main thread has
>>>> thread id 1 and max. stack depth is 1000:
>>>>
>>>> StackTraceElement[] ste =
>>>> ManagementFactory.getThreadMXBean().getThreadInfo( 1,
>>>> 1000).getStackTrace(); String clznam =
>>>> ste[ste.length-1].getClassName();
>>> Is that really going to give you the class mentioned in the command
>>> line rather than the class which defines the static main() method?

>> You are correct. It returns the parent class where main is
>> defined.

>
> Right. My strong impression is that trying to figure out which subclass was
> mentioned in the command line is simply hopeless (or at best extremely
> non-portable) and that, accordingly, the OP should figure out some other way
> of doing things. Fortunately, there are dozens of them.


True.

JNI and an OS specific call is not a good solution.

Arne
 
Reply With Quote
 
 
 
 
Toffstier
Guest
Posts: n/a
 
      10-05-2009
On 15 Sep., 03:32, "Mike Schilling" <mscottschill...@hotmail.com>
wrote:
> Arne Vajhøj wrote:
> >>> If we can assume that Java version >= 1.5, main thread has
> >>> thread id 1 and max. stack depth is 1000:

>
> >>> * * * * StackTraceElement[] ste =
> >>> ManagementFactory.getThreadMXBean().getThreadInfo( 1,
> >>> * * * * 1000).getStackTrace(); String clznam =
> >>> ste[ste.length-1].getClassName();

>
> >> Is that really going to give you the class mentioned in the command
> >> line rather than the class which defines the static main() method?

>
> > You are correct. It returns the parent class where main is
> > defined.

>
> Right. *My strong impression is that trying to figure out which subclass was
> mentioned in the command line is simply hopeless (or at best extremely
> non-portable) and that, accordingly, the OP should figure out some other way
> of doing things. *Fortunately, there are dozens of them.


public static String getMainClassName()
{
for(final Map.Entry<String, String> entry : System.getenv().entrySet
())
{
if(entry.getKey().startsWith("JAVA_MAIN_CLASS"))
return entry.getValue();
}
throw new IllegalStateException("Cannot determine main class.");
}
 
Reply With Quote
 
 
 
 
Ross
Guest
Posts: n/a
 
      10-05-2009
On Oct 5, 11:41 am, Toffstier <mjcap...@gmail.com> wrote:
> public static String getMainClassName()
> {
> for(final Map.Entry<String, String> entry : System.getenv().entrySet
> ())
> {
> if(entry.getKey().startsWith("JAVA_MAIN_CLASS"))
> return entry.getValue();
> }
> throw new IllegalStateException("Cannot determine main class.");
> }


Thanks, but this doesn't work for me. Even trying to create the parent
class leads to an error being thrown.

$ java TestMain
Exception in thread "main" java.lang.IllegalStateException: Cannot
determine main class.
at TestMain.getMainClassName(TestMain.java:14)
at TestMain.main(TestMain.java:19)
$

Here are the keys available on my computer.

entry key is TERM
entry key is SSH_AGENT_PID
entry key is JAVA_HOME
entry key is SHLVL
entry key is JMFHOME
entry key is XFILESEARCHPATH
entry key is GDM_XSERVER_LOCATION
entry key is SESSION_MANAGER
entry key is COLORTERM
entry key is GNOME_DESKTOP_SESSION_ID
entry key is MAIL
entry key is GDMSESSION
entry key is HOSTNAME
entry key is PWD
entry key is FIREFOX_DSP
entry key is WINDOWID
entry key is KDEDIR
entry key is LOGNAME
entry key is G_BROKEN_FILENAMES
entry key is QTDIR
entry key is _
entry key is SSH_AUTH_SOCK
entry key is NLSPATH
entry key is GNOME_KEYRING_SOCKET
entry key is LD_LIBRARY_PATH
entry key is OLDPWD
entry key is SHELL
entry key is INPUTRC
entry key is DBUS_SESSION_BUS_ADDRESS
entry key is HISTSIZE
entry key is GTK_RC_FILES
entry key is CLASSPATH
entry key is PATH
entry key is JRE_HOME
entry key is DESKTOP_SESSION
entry key is DISPLAY
entry key is USER
entry key is DESKTOP_STARTUP_ID
entry key is HOME
entry key is PKG_CONFIG_PATH
entry key is LESSOPEN
entry key is XAUTHORITY
entry key is HTC_XML
entry key is LS_COLORS
entry key is LANG
entry key is SSH_ASKPASS

Here's the source code I used.


import java.util.*;

public class TestMain
{

public static String getMainClassName()
{
for(final Map.Entry<String, String> entry : System.getenv
().entrySet
())
{
System.out.println( "entry key is " + entry.getKey() );
if(entry.getKey().startsWith("JAVA_MAIN_CLASS"))
return entry.getValue();
}
throw new IllegalStateException("Cannot determine main class.");
}

public static void main( String args[] )
{
System.out.println( "main class is " + getMainClassName() );
}
}
 
Reply With Quote
 
Mike Schilling
Guest
Posts: n/a
 
      10-05-2009
Toffstier wrote:
> On 15 Sep., 03:32, "Mike Schilling" <mscottschill...@hotmail.com>
> wrote:
>> Arne Vajhøj wrote:
>>>>> If we can assume that Java version >= 1.5, main thread has
>>>>> thread id 1 and max. stack depth is 1000:

>>
>>>>> StackTraceElement[] ste =
>>>>> ManagementFactory.getThreadMXBean().getThreadInfo( 1,
>>>>> 1000).getStackTrace(); String clznam =
>>>>> ste[ste.length-1].getClassName();

>>
>>>> Is that really going to give you the class mentioned in the
>>>> command
>>>> line rather than the class which defines the static main()
>>>> method?

>>
>>> You are correct. It returns the parent class where main is
>>> defined.

>>
>> Right. My strong impression is that trying to figure out which
>> subclass was mentioned in the command line is simply hopeless (or
>> at
>> best extremely non-portable) and that, accordingly, the OP should
>> figure out some other way of doing things. Fortunately, there are
>> dozens of them.

>
> public static String getMainClassName()
> {
> for(final Map.Entry<String, String> entry :
> System.getenv().entrySet ())
> {
> if(entry.getKey().startsWith("JAVA_MAIN_CLASS"))
> return entry.getValue();
> }
> throw new IllegalStateException("Cannot determine main class.");
> }


How is JAVA_MAIN_CLASS getting set? It's not something the JVM does
automagically.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
What will happen if main called in side main function? Ravi C Programming 17 04-01-2006 10:08 PM
int main() or int main(void)? Frederick Ding C Programming 10 12-04-2005 12:44 AM
adding main.aspx.vb & main.aspx.resx under aspx John M ASP .Net 1 05-29-2005 09:27 PM
int main(int argc, char *argv[] ) vs int main(int argc, char **argv ) Hal Styli C Programming 14 01-20-2004 10:00 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57