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

Posted on 2006-06-20
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#.)
Question by:stockblocks
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
LVL 86

Expert Comment

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

Accepted Solution

StillUnAware earned 200 total points
ID: 16943557
You simply do that:

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

Author Comment

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!

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

LVL 14

Expert Comment

ID: 16944286
glad to help
LVL 86

Expert Comment

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

Author Comment

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

Featured Post

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
ejb stateless example 2 68
eclipse console opening separately 2 49
Java pass by reference 3 74
How do i find out all classes which contain nested class 2 14
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses

734 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