• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How can you get the path name from a file name

 
John Chien
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
In JDK 1.3, I can use the following statement to get the pathname of database file. However, I can not get it works in 1.4.
String path = getClass().getResource("../db/db.db").getPath();
Does anybody know what might be the problem ?
Thanks,
John Chien
 
Adam Till
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
Search this forum as its come up quite a few times. One option is:-
URL myURL = this.class.getResource(file to find);
Hope this helps,
 
John Chien
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Adam:
Thank you for the answer. Your suggestion is the same as what I did. The problem is that
getClass().getResource("../db/db.db");
or
className.class.getResource(../db/db.db");
returns null.
I tried several combination of file name, none of them works. I even gave the absolute name as file name string. It still returns me null.
I ran all this using JBuilder.
Thanks,
John Chien
 
Lin Feng
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why do not you use File class to get that?
 
John Chien
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use File does not work either.
I found the reason.
My "db.db" was originally in the .../src/suncertify/db/ directory.
I complied the .java files and generated all the .class file in the .../classes/suncertify/db directory.
When I execute the program, the program was looking for the "db.db" file in the "classes" directory instead of the "src" directory.
Hmmm !! This is strange. When I ran the same program in JDK 1.3 using Jbuilder 4, I do not have this problem. I am running program in JDK 1.4 using JBuilder 8 currently.
That means the database file has to be in the directory related to complied classes. I guess that I can solve the problem, if I set src directories in the CLASSPATH.
Is there any other simpler way to solve this issue ?
Thanks,
John Chien
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic