• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 196
  • Last Modified:

How can I get the name of the application from inside a package.

All,

I have a Log object inside a utilities package that is shared between several applications. How can it find the name of the application it is in so it can name its log file correctly?

Paul
0
PaulCaswell
Asked:
PaulCaswell
  • 6
  • 5
1 Solution
 
CEHJCommented:
You probably would have to pass it the name of your app
0
 
PaulCaswellAuthor Commented:
I looked in System.getProperty and there doesnt seem to be one there. I used to use       

String myName = this.getClass().getName();
fName = myName.split("\\.")[0]+".log";

but that doesnt work any more as its now in its own package.

Paul
0
 
CEHJCommented:
That would in any case only produce 'Log.log' wouldn't it?
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
RoyalNepalCommented:
Yeah..you need to write the name of the application to get the expected name when you do..

>>String myName = this.getClass().getName();
>>>fName = myName.split("\\.")[0]+".log";

otherwise it will give only gives you Log.log


0
 
PaulCaswellAuthor Commented:
Could I collect the name of the class at the bottom of the stack? I believe there is a way of walking the stack isnt there?

Paul
0
 
CEHJCommented:
You could, but only if the main class is at the bottom will you get what you want
0
 
PaulCaswellAuthor Commented:
>>only if the main class is at the bottom
I can assume that as this is a utility program. How do I get an instance of my own stack to walk?

Paul
0
 
PaulCaswellAuthor Commented:
Thanks guys! This seems to work! Or at least, it gets the name of the file containing the main method, which should be enough for me.

        // Who is at the bottom of the stack?
        Throwable stack = new Exception();
        StackTraceElement [] calls = stack.getStackTrace();
      fName = calls[calls.length-1].getFileName().split("\\.")[0];

Thanks!

Paul
0
 
CEHJCommented:
:-)
0
 
CEHJCommented:
You can actually do

calls[calls.length-1].getClassName()
0
 
PaulCaswellAuthor Commented:
Yeah but that will give me 'package.class' so I will get the package name when I split at the '.' which will usually be the same (except for the case). This way, if user changes the name of the file, the log file name will change too so I prefer this way. Thanks for the heads-up though.

Paul
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

  • 6
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now