Solved

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

Posted on 2006-07-22
12
185 Views
Last Modified: 2010-03-31
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
Comment
Question by:PaulCaswell
  • 6
  • 5
12 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 17160778
You probably would have to pass it the name of your app
0
 
LVL 16

Author Comment

by:PaulCaswell
ID: 17160787
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 17160794
That would in any case only produce 'Log.log' wouldn't it?
0
ScreenConnect 6.0 Free Trial

At ScreenConnect, partner feedback doesn't fall on deaf ears. We collected partner suggestions off of their virtual wish list and transformed them into one game-changing release: ScreenConnect 6.0. Explore all of the extras and enhancements for yourself!

 
LVL 2

Expert Comment

by:RoyalNepal
ID: 17160807
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
 
LVL 16

Author Comment

by:PaulCaswell
ID: 17160888
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 17160899
You could, but only if the main class is at the bottom will you get what you want
0
 
LVL 16

Author Comment

by:PaulCaswell
ID: 17160908
>>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
 
LVL 86

Accepted Solution

by:
CEHJ earned 125 total points
ID: 17160917
0
 
LVL 16

Author Comment

by:PaulCaswell
ID: 17160982
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 17160989
:-)
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 17161009
You can actually do

calls[calls.length-1].getClassName()
0
 
LVL 16

Author Comment

by:PaulCaswell
ID: 17161043
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

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
mockito example issue 8 72
hibernate jars 4 29
Tomcat: Unable to run tomcat service. 2 18
runtime exception 2 28
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …

770 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