• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How can you get the path name from a file name

 
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
reply
    Bookmark Topic Watch Topic
  • New Topic