This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How can I get application path from java code?

 
Fabio Saracino
Greenhorn
Posts: 5
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Henry Wong
author
Marshal
Pie
Posts: 20835
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • 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 ]
 
Rob Spoor
Sheriff
Pie
Posts: 20380
45
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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!
 
Sri Anand
Ranch Hand
Posts: 392
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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!
 
Brian Legg
Ranch Hand
Posts: 488
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Paul Clapham
Sheriff
Pie
Posts: 20184
25
MySQL Database
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why do you think you need the path where the application is located?
 
Rob Spoor
Sheriff
Pie
Posts: 20380
45
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic