inheriting a main method

A

Arne Vajhøj

Mike said:
Arne said:
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
 
T

Toffstier

Arne said:
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.");
}
 
R

Ross

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() );
}
}
 
M

Mike Schilling

Toffstier said:
Arne said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,540
Members
45,025
Latest member
KetoRushACVFitness

Latest Threads

Top