Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Finding the caller of a class

 
Leslie Chaim
Ranch Hand
Posts: 336
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the Java equivalent of C's argv[0] or the Shell's $0
 
Michael Ernest
High Plains Drifter
Sheriff
Posts: 7292
Netbeans IDE VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
er, something like getClass().toString() ?
 
Leslie Chaim
Ranch Hand
Posts: 336
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1873
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1873
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 336
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 189
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic