aspose file tools*
The moose likes Beginning Java and the fly likes How can I get application path from java code? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How can I get application path from java code?" Watch "How can I get application path from java code?" New topic
Author

How can I get application path from java code?

Fabio Saracino
Greenhorn

Joined: Nov 23, 2008
Posts: 5
Hello, I need to know the application path from java code. How can I get it? For example, if my code is in C:\Myapp, I need to know "C:\Myapp". Thank you.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18847
    
  40

Not sure what you are asking... Are you looking for the main? The location of the main() method could be in any one of the classes in your class path. In fact, more than one class can have a main method -- some of which could be used for testing purposes.

In some cases, you may be able to figure out how to call your application, merely by looking at the class names -- and some trial and error. But if you got some WAD of code from someone, and don't have an idea how to call it, it will be tough.


[EDIT: in retrospect, your question may be related to something else. Anyway, could you clarify a bit more? ]

Henry
[ November 24, 2008: Message edited by: Henry Wong ]

Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19697
    
  20

Officially you can't.

There's always a trick though
This is not guaranteed to to work on all ClassLoaders, but it does with the system ClassLoader and URLClassLoader:

This code uses the fact that for the above mentioned ClassLoaders, it can use the class file itself as a resource. The getResource method will return a URL that contains information on the exact location. Try it out with classes both in JAR files and in usual folders, and I'm sure you can work out the rest.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Fabio Saracino
Greenhorn

Joined: Nov 23, 2008
Posts: 5
Hello Henry, I have a Netbeans project application and I need the complete path of this application. I must find it from the java code, in someway.
Thanks!
Sri Anand
Ranch Hand

Joined: Mar 06, 2005
Posts: 392
you can configure the path of the application in properties file or web.xml, and always retrieve from there
Fabio Saracino
Greenhorn

Joined: Nov 23, 2008
Posts: 5
Hello Rob, I tried your suggestion and it works. But I have a problem, because the code, from which I find the path, is not the code of the application that I need the path. I have a class that walk through another class and I must find the path of the project of the second class in the first class!
Brian Legg
Ranch Hand

Joined: Nov 07, 2008
Posts: 488
I'm just making stuff up but couldn't you create a File object (not an actual file) and it always makes your current directory the default. Then you could call some method to get the path from it (not sure what the method is called), then just append the current class name to the end?

Hope that helps at all.


SCJA
~Currently preparing for SCJP6
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

Why do you think you need the path where the application is located?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19697
    
  20

Originally posted by Fabio Saracino:
Hello Rob, I tried your suggestion and it works. But I have a problem, because the code, from which I find the path, is not the code of the application that I need the path. I have a class that walk through another class and I must find the path of the project of the second class in the first class!

Well it should work with any class, as long as the ClassLoader is playing nice
You just need to use that Class object's getResource method, and use that Class' class file. You could even tell where the String class is located by calling String.class.getResource("String.class");


Originally posted by Brian Legg:
I'm just making stuff up but couldn't you create a File object (not an actual file) and it always makes your current directory the default. Then you could call some method to get the path from it (not sure what the method is called), then just append the current class name to the end?

Hope that helps at all.

Because the class can be found through the class path, and need not be relative to the current directory.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How can I get application path from java code?