• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

How can I get application path from java code?

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
author
Posts: 23926
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Sheriff
Posts: 22665
127
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Fabio Saracino
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Ranch Hand
Posts: 392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you can configure the path of the application in properties file or web.xml, and always retrieve from there
 
Fabio Saracino
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Ranch Hand
Posts: 488
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Marshal
Posts: 27287
87
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why do you think you need the path where the application is located?
 
Rob Spoor
Sheriff
Posts: 22665
127
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic