Solved

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

Posted on 2006-07-22
12
184 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
Comment Utility
You probably would have to pass it the name of your app
0
 
LVL 16

Author Comment

by:PaulCaswell
Comment Utility
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
Comment Utility
That would in any case only produce 'Log.log' wouldn't it?
0
 
LVL 2

Expert Comment

by:RoyalNepal
Comment Utility
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
Comment Utility
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
Comment Utility
You could, but only if the main class is at the bottom will you get what you want
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 16

Author Comment

by:PaulCaswell
Comment Utility
>>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
Comment Utility
0
 
LVL 16

Author Comment

by:PaulCaswell
Comment Utility
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
Comment Utility
:-)
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
You can actually do

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

Author Comment

by:PaulCaswell
Comment Utility
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

728 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now