aspose file tools*
The moose likes Java in General and the fly likes Finding the caller of a class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Finding the caller of a class" Watch "Finding the caller of a class" New topic
Author

Finding the caller of a class

Leslie Chaim
Ranch Hand

Joined: May 22, 2002
Posts: 336
What is the Java equivalent of C's argv[0] or the Shell's $0


Normal is in the eye of the beholder
Michael Ernest
High Plains Drifter
Sheriff

Joined: Oct 25, 2000
Posts: 7292

er, something like getClass().toString() ?


Make visible what, without you, might perhaps never have been seen.
- Robert Bresson
Leslie Chaim
Ranch Hand

Joined: May 22, 2002
Posts: 336
Kind of, but I am trying to do that from the static main().
I simple want to say to the user if he/she failed to provide the proper cmd line args with a usage message such as:
Usage: java <Class> {port}
I don't want to "hardcode" Class
Maulin Vasavada
Ranch Hand

Joined: Nov 04, 2001
Posts: 1871
Hi lesli,
i found two ways of doing what u want.
1. if u 've no-arg constructor then u can do it
2. even if u don't have no-arg constructor u can do it :-)
(in short u can do it anyway)
here is a code i tested...

btw, Michael i liked your signature. wow! u took SCJP test when i didn't know Java existed :-)
regards,
maulin
Maulin Vasavada
Ranch Hand

Joined: Nov 04, 2001
Posts: 1871
btw, leslie,
i didnt get your point about "not hardcoding the class name"...
well, i would say it would not be too much hassle or problem if u do hardcoding. would it be? because anyways if u change ur class name anytime in future then u can change the corresponding message line as well... OR i am missing ur point completly....please explain me if i'm wrong...i'm slow at things :-(
regards
maulin
Leslie Chaim
Ranch Hand

Joined: May 22, 2002
Posts: 336
I completely got it thanks. I was simply looking for a "generic" way to report a class name in a Usage prompt. In addition, consider that I have a template of code (used as test harness) which I plug in any class which I create. Can you see in what type of "hard-coding" problems I can run into?
Originally posted by Maulin Vasavada:

btw, Michael i liked your signature. wow! u took SCJP test when i didn't know Java existed :-)

And I didn't even know what IT meant! BTW, and talking about minimums... I got 67% on the SCJP in March 14 this year (I plan to officially brag :wink: after I become a SCJD)
Nevertheless, here is my excuse:
"My total real practice was less then 50 lines of Java code"
That's how great Michael and the rest of the RHE gang were!
Elemer Kuchar
Greenhorn

Joined: Jul 10, 2003
Posts: 1
Hello,
may be following solution could help:
Create Throwable object, print its stack trace into String and parse the class name from the string. You can also obtain other useful info like line number, function name etc. from it. I test the solution on Java HotSpot(TM) Server VM 1.3.1 (UNIX) and Java HotSpot(TM) Client VM 1.4.1 (win2000).

Sample code:

public class MyTest {
public static void main( String[] args ) {
if ( args.length == 0 ) {
// get stack trace, it contains the class name
java.io.StringWriter sw = new java.io.StringWriter() ;
Throwable t = new Throwable() ;
t.printStackTrace( new java.io.PrintWriter( sw ) ) ;
String stackTrace = sw.toString() ;
// extract class name
int beginPos = stackTrace.indexOf( "(" ) + 1 ;
int endPos = stackTrace.lastIndexOf( "." ) ;
String className = stackTrace.substring( beginPos, endPos ) ;
// usage...
System.out.println( "Usage: java " + className + " <param>" ) ;
}

}
}
Elemer
Philip Shanks
Ranch Hand

Joined: Oct 15, 2002
Posts: 189
I don't know if this gets at what you want or not. It's a variant on Maulin's code above, but doesn't need to have the class name hardcoded (this one refers to itself, but any class could be instantiated in the newInstance() call). I guess I don't really know how your test harness works.

Invocation:
java MyClass
Output:
usage: java MyClass {port}
[ July 11, 2003: Message edited by: Philip Shanks ]

Philip Shanks, SCJP - Castro Valley, CA
My boss never outsources or has lay-offs, and He's always hiring. I work for Jesus! Prepare your resume!
 
 
subject: Finding the caller of a class