• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to find physical path of current java class using java code....  RSS feed

 
Pras Tiwari
Ranch Hand
Posts: 186
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all,

I am creating a java class & packaging it into jar. Now I want to add some code into my java class so that it will print its current physical path in file system (either jars path in which it is packaged or java files path if its not packaged in jar) on console.

Any idea?

Many thanks in advance.

Pras
 
Nicholas Jordan
Ranch Hand
Posts: 1282
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Very simple.


Sorry bout the irrelevant comment, I am in the middle of a project - I use these answer question programs as handy utiilty code.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hm, that seems like a convoluted (and dysfunctional) way to get the current working directory. As simpler way that actually works would be

or

However, that's not what Prashant asked. He's apparently looking for the location of the class file. He's a way that can be found (assuming the current class is called Test.class):

[ February 07, 2008: Message edited by: Jim Yingst ]
 
Rob Spoor
Sheriff
Posts: 21131
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That won't work if the JAR file is run from another location. E.g.:

It would print C:\\, ot C:\\SomeFolder.

There is a trick that works with MOST class loaders; not all though. It is based on the idea that a .class file itself can mostly also be treated as a resource:

Some notes:
1) getShortName returns the class name without the package.
2) you can try to convert the URL to a File using "new File(url.toURI())"

This works (as tested) with both URLClassLoader and the system class loader.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[Rob]: That won't work if the JAR file is run from another location.

Hm, I think my error was more general than that - I included an extra getClassLoader() call that made it only work for classes in the default package. The Class remembers the relevant package, and looks in appropriate subdirectorries, but the ClassLoader does not. I edited my above post to omit that extra call now. The above code works fine on my system even for a jar file in another directory - e.g.

As for parsing the result, well that's up to the user what form they want it in; I didn't really care much about the details of that.
[ February 07, 2008: Message edited by: Jim Yingst ]
 
Rob Spoor
Sheriff
Posts: 21131
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jim Yingst:
[Rob]: That won't work if the JAR file is run from another location.

I was referring to Nicholas' solution; your solution was a simplified version of mine, which definitely works.

As long as you don't use some custom class loader that returns something completely different when calling getResource() that is. Fortunately, the default class loaders available in Java do not.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[Rob']: I was referring to Nicholas' solution; your solution was a simplified version of mine, which definitely works.

No, your solution is a complexified version of mine.
[ February 08, 2008: Message edited by: Jim Yingst ]
 
Nicholas Jordan
Ranch Hand
Posts: 1282
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jim Yingst:
[Rob']: I was referring to Nicholas' solution; your solution was a simplified version of mine, which definitely works.


Well was I dysfunctional)? -
 
Rob Spoor
Sheriff
Posts: 21131
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your solution only works if you are in the path where the JAR file or package folder is located, since it prints out the current working directory. And as Jim has shown, you don't need a separate class for it; just using new File(".") works just as well.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nick's code doesn't work at all. It just prints ".". With minor tweaking, it could print the current user directory, but that's not what the original poster asked for, is it?
 
Nicholas Jordan
Ranch Hand
Posts: 1282
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jim Yingst:
Nick's code doesn't work at all.

(...snip...)



I have used it several times - it works so reliably that I do not need to defend my dysfuctional() ... it works, see java.io.File() line 183: public File(String pathname). It may be different on your shell which is designed to resemble the unice, but on winnie '.' resolves to current directory. You can do super("."); on an extends java.io.File and if(isDirectory()) evaluates to true. datfunctonal() ( your shell ) may not work but dysfunctional() does work.
[ February 10, 2008: Message edited by: Nicholas Jordan ]
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nick, try running the program you posted. I don't care if you've run a similar program - it's obvious that you haven't run the one that you actually posted. This has nothing to do with shells or operating systems. Your program is equally useless on any system.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can actually argue that the fact that it extends java.io.File is an obfuscation (and not a very good one). All the code does is call a method which prints a string that is set to a period. In effect, all it does is print a period.

Henry
 
Nicholas Jordan
Ranch Hand
Posts: 1282
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay, I see your point - rewrote it but cannot get a test run: keep getting no class definiton for main.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Which, again, prints "."
 
Nicholas Jordan
Ranch Hand
Posts: 1282
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Then replace return toString();// with return super.getAbsolutePath()
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!