Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how can i get path of class file in an app.?

 
sanjeev mehra
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can we get the path of class file programaticaly in an application? i belive getCodeBase() helps in applet.
thanks,
sanjeev.
 
Karthik Guru
Ranch Hand
Posts: 1209
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
did u try
getResource() in class Class?
actually there is one in ClassLoader class as well.
URl u = string.class.getResource("test.class");
String path = new File(u.getFile()).getAbsolutePath();
 
sanjeev mehra
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am going to try it. Thanks for the reply. Thanks a lot.
Sanjeev.
 
Kiran Kumar
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you can use this code to see from where the class is being loaded.
MyClass.class.getProtectionDomain().getCodeSource().getLocation()
hope this helps
 
Peter den Haan
author
Ranch Hand
Posts: 3252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by sanjeev mehra:
can we get the path of class file programaticaly in an application?
The above suggestions should work, but I'm curious as to why you would need this. If it is to impose some restrictions, best use the normal Java security features. If you need to load some additional resources like graphics or HTML documentation, put them in the jar(s) and use ClassLoader.getResource() or ClassLoader.getResourceAsStream(). (You can obtain a suitable classloader using, for example, DataInterface.class.getClassLoader()).
- Peter
 
Bhuvan mehra
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i am also using this and the reason is when server will be loaded i will provide exact loction of db.db file to user and ask him if he want to change it.
on client side if user choose localmode, same situation.
regards,
bhuvan.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic