The Artima Developer Community
Sponsored Link

Java Answers Forum
Getting main loaded class

14 replies on 1 page. Most recent reply: Nov 16, 2005 4:27 AM by Joe Parks

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 14 replies on 1 page
Makhmud Kasumov

Posts: 16
Nickname: marik
Registered: Oct, 2005

Getting main loaded class Posted: Oct 31, 2005 6:09 AM
Reply to this message Reply
Advertisement
Is there a way in Java API to get the name of the main loaded class (the one specified in command line, i.e. java MyClass)?

Thank you


Kondwani Mkandawire

Posts: 530
Nickname: spike
Registered: Aug, 2004

Re: Getting main loaded class Posted: Oct 31, 2005 6:31 AM
Reply to this message Reply
> Is there a way in Java API to get the name of the main
> loaded class (the one specified in command line, i.e. java
> MyClass)?
>
> Thank you

I don't know exactly what you are trying to achieve, but
this returns a class Name of the file containing the main
method.
import java.io.*;
 
public class MyClass{
   public static void main(String []args){
      //MyClass my_clizzo = new MyClass();
      System.out.println(new MyClass().getClass());
   }
}

Makhmud Kasumov

Posts: 16
Nickname: marik
Registered: Oct, 2005

Re: Getting main loaded class Posted: Oct 31, 2005 7:36 AM
Reply to this message Reply
It's like this.
I have some base class Program which contains <main> method.

public class Program {
public static void main (String[] args) {
}
}

Then I create new class MyProgram which extends Program, but does NOT override <main> method:

public class MyProgram extends Program {
}

I launch MyProgram class: java MyProgram
It starts Program.main method.
And now I want to find out from inside the Program.main method which class has been actually loaded (that is, MyProgram class).

Or is it wicked?
Thanks

Matthias Neumair

Posts: 660
Nickname: neumi
Registered: Sep, 2003

Re: Getting main loaded class Posted: Nov 7, 2005 2:28 AM
Reply to this message Reply
Your question doesn't make much sense to me.

Where exactly do you want to get the class if you don't want to override the main method?

In the super.main method? Then just change it like Kondwani said.

Makhmud Kasumov

Posts: 16
Nickname: marik
Registered: Oct, 2005

Re: Getting main loaded class Posted: Nov 7, 2005 5:26 AM
Reply to this message Reply
I have a base class Program and many subclasses, let's say, MyProgram1, MyProgram2, MyProgram3. None of the subclasses overrides Program.main() method. And class Program knows nothing about its subclasses MyProgramX. So I launch one of the MyProgramX classes. Since there is no main() method overriden in MyProgramX, JVM runs Program.main().

Now I want to instantiate a class that was actually launched (one of the MyProgramX). But I don't know its name. I cannot suppose anything about it. Is it possible to get the name of the class that was launched?

In other words, I want to get the first argument passed to JVM:

java MyProgram1
this ____^

Kondwani Mkandawire

Posts: 530
Nickname: spike
Registered: Aug, 2004

Re: Getting main loaded class Posted: Nov 7, 2005 7:44 AM
Reply to this message Reply
> In other words, I want to get the first argument passed to
> JVM:
>
> java MyProgram1
> this ____^

That's kind of wierd but in any case why don't you just
set a global static variable and as soon as main is
invoked instantiate it to hold args[0] as in the parameter
from:
public static void main(String []args)

Joe Parks

Posts: 107
Nickname: joeparks
Registered: Aug, 2003

Re: Getting main loaded class Posted: Nov 7, 2005 9:21 PM
Reply to this message Reply
I don't think you can get what you want. Although you can launch the vm with a class that merely inherits a main method (rather than implementing it), the method that gets invoked is static. That is, it is tied to the class in which it was defined.
As far as the VM is concerned, your base class will always be the "main class". That you can launch it through a subclass is merely interesting VM trivia.

The class name, by the way, is not a member of the array of Strings passed to the main method.

Matthias Neumair

Posts: 660
Nickname: neumi
Registered: Sep, 2003

Re: Getting main loaded class Posted: Nov 7, 2005 10:26 PM
Reply to this message Reply
I wasn't able to get anything useful either.

Overloading a method wich returns some sort of class id was uselesse, I could not find any information about the main class in the system properties either.

The VM itselft surely has some infos, but I don't know how to access them.

Kondwani Mkandawire

Posts: 530
Nickname: spike
Registered: Aug, 2004

Re: Getting main loaded class Posted: Nov 7, 2005 11:11 PM
Reply to this message Reply
>
> The class name, by the way, is not a member of the array
> of Strings passed to the main method.

My baad, I had spoken with out running a test... I used
a programming language a good while back that had its
Program name as one of the arguments, I confused myself
to thinking it was Java. I'll write a couple of println-type
onliners in the languages I know just to verify if I dreamt
this up or I actually have done something of that nature...

Kondwani Mkandawire

Posts: 530
Nickname: spike
Registered: Aug, 2004

Re: Getting main loaded class Posted: Nov 7, 2005 11:19 PM
Reply to this message Reply
Once again: My bad, it was C:


#include <stdio.h>

main(int argc, char **argv)

{ /* program to print arguments from command line */

int i;

printf("argc = %d\n\n",argc);
for (i=0;i<argc;++i)
printf("argv[%d]: %s\n",i, argv[i]);
}

argv[0] is the name of the program, in the manner I compiled
it that would be a.out (din't use the -O Option, or whatever
option C uses - haven't touched C for 2 years)...

Anyways, that's where the confusion came from... My
to the OP for misleading them...

Makhmud Kasumov

Posts: 16
Nickname: marik
Registered: Oct, 2005

Re: Getting main loaded class Posted: Nov 8, 2005 12:15 AM
Reply to this message Reply
Yes, it has probably no solution, unfortunately.
Indeed args[0] holds just the first <parameter>, not the class name launched. And there is no info about the main class in system properties.

So far, I am forced to deal it this way:
java MyProgram1 MyProgram1

Ugly but works...
If anybody has any idea about the topic - please write, I'd appreciate!

Matthias Neumair

Posts: 660
Nickname: neumi
Registered: Sep, 2003

Re: Getting main loaded class Posted: Nov 8, 2005 1:55 AM
Reply to this message Reply
The simpliest way would definitively be to overload the main class.
Inside, just call super.main and add something like "/class=..." to the parameters.

Slager .

Posts: 16
Nickname: slager
Registered: May, 2003

Re: Getting main loaded class Posted: Nov 8, 2005 8:41 AM
Reply to this message Reply
Since 1.4 you can do:

Exception ex = new Exception();
StackTraceElement[] el = ex.getStackTrace();
System.out.println("The instance name is '" + el[0].getClassName() + "'");


Or something similar is possible with 1.3 and string parsing:


Exception ex = new Exception();
StringWriter w = new StringWriter();
ex.printStackTrace(new PrintWriter(w,true));
String sTrace = aWriter.toString();
... parse it....

Slager .

Posts: 16
Nickname: slager
Registered: May, 2003

Re: Getting main loaded class Posted: Nov 8, 2005 10:34 AM
Reply to this message Reply
But... it doesn't actually get what you wanted :)

Joe Parks

Posts: 107
Nickname: joeparks
Registered: Aug, 2003

Re: Getting main loaded class Posted: Nov 16, 2005 4:27 AM
Reply to this message Reply
You won't be able to call "super", because the method is static. You don't have access to an instance of the class.

Flat View: This topic has 14 replies on 1 page
Topic: please help me!!!! Previous Topic   Next Topic Topic: Transpose matrix

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use