Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Can Java source code know it's parent file and package names without explicitly defining static String's?

Posted on 2006-06-20
6
Medium Priority
?
200 Views
Last Modified: 2010-03-31
I'm beginning to use JBuilder 2005 for servlet programming.

I often embed references to the source filename (and associated package it belongs to) when reporting error exceptions.  Currently, I have private static String vars named -- appropriately -- CLASS_NAME and PACKAGE_NAME.

Is there a better way?  I was wondering if there was some magical code call that automatically identifies their names.  If not a JDK call, but perhaps a JBuilder macro-like call that can be embedded in my .java files (for example -- and I'm making this up --  #CLASSNAME#.)
0
Comment
Question by:stockblocks
  • 2
  • 2
  • 2
6 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 16943546
Use log4j. It will report the class and package which logged the message
0
 
LVL 14

Accepted Solution

by:
StillUnAware earned 800 total points
ID: 16943557
You simply do that:

String name = someObject.getClass().getName();
String pack = someObject.getClass().getPackage().toString();
0
 

Author Comment

by:stockblocks
ID: 16944209
Excellent.  I see that getClass().getName() reports both package and class names together, which is perfect!

Should have known this all along. Thanks!

SB
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 14

Expert Comment

by:StillUnAware
ID: 16944286
glad to help
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 16944452
Log4j is the de facto standard for logging - i'd use it if i were you ;-)
0
 

Author Comment

by:stockblocks
ID: 16944591
CEHJ,  sorry -- thanks for the tip! I'll google it to learn more.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses

782 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question