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