Elemer Kuchar

Greenhorn
+ Follow
since Jul 10, 2003
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Elemer Kuchar

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
20 years ago