Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Java in General and the fly likes Current directory as a file address? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Current directory as a file address?" Watch "Current directory as a file address?" New topic
Author

Current directory as a file address?

Ilya PA
Greenhorn

Joined: Sep 05, 2001
Posts: 15
Hi,
I am experiencing a difficulty to specify the current directory as a path to an input text file in a program that reads from a file. JVM only accepts an absolute path to my input.txt and not the relative. How can i specify the current directory(relative to the class file with the main() method in it) as a parameter for the FileReader's constructor using the relative path?
Thank you very much,
Ilya.
Maulin Vasavada
Ranch Hand

Joined: Nov 04, 2001
Posts: 1871
hi,
try System.getProperty("user.dir");
and then append 'input.txt' file name with that path...
did i get your question correctly?
regards
maulin
Ilya PA
Greenhorn

Joined: Sep 05, 2001
Posts: 15
Thank you for the reply,
but the output of the
System.getProperty("user.dir")
is the Java SDK directory, but i would like to get the address of a location where my classes are stored, not the SDK. Is it possible to get that?
Thanx alot!
Ilya.
Maulin Vasavada
Ranch Hand

Joined: Nov 04, 2001
Posts: 1871
hi Ilya,
according to the API user.dir property reflects the "current user directory" while the code is in the execution.
so u will get the runtime directory from where u r running the code.
say if i have 2 dirs,
1. c:\maulin\java
2. c:\maulin\temp
1st dir has testUSERDIR.java/.class files
2nd dir has test1.java/.class files
code for the testUSERDIR.java

and code for test1.java is,

now, if i run the test1 from c:\maulin\temp having c:\maulin\java in the classpath as,
c:\maulin\temp>java -cp %CLASSPATH%;c:\maulin\java test1
then i get output as,
c:\maulin\temp
if i move my test1.class to c:\ then i get output,
c:\
is not this you are trying to do?
the reason that u get Java SDK directory in the output is - you are running your code from there.
DO YOU WANT TO KNOW FROM WHICH DIRECTORY THE CLASS IS LOADED FROM INSTEAD??? that would be difficult. this implies that- you want output of my above code test1 as "c:\maulin\java" even if we run the test1 from c:\maulin\temp. is that what u want?
regards
maulin.
Ilya PA
Greenhorn

Joined: Sep 05, 2001
Posts: 15
Hi Maulin,
Thank you very much for staying with me all this time. The example you've given was helpful to understand the use of the System.getProperty("user.dir"). But also would like to know if it is possible to get the directory that "the class was loaded from" as you have mentioned in your last reply.
Please excuse my english. It took me quite some time to deliver my question right.
Thanks heaps,
Ilya.
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Hmm...I was intrigued by your question so I had to browse through the API docs to see if I could find any other possible solutions. Perhaps the Class.getResource() or Class.getResourceAsString() methods will help you. I haven't tried them yet, but you should be able to test them easily to see if they meet your needs.
HTH
Layne


Java API Documentation
The Java Tutorial
Maulin Vasavada
Ranch Hand

Joined: Nov 04, 2001
Posts: 1871
hi Ilya,
i tired the following code,

the configuration i had was,
1. i created input.txt and put it under C:\
2. i have everything (JRE) configured on D:\jdk
3. i have the above code in D:\maulin\java
i ran the above code via,
d:\maulin\java>java %CLASSPATH%;C:\ DetectClassPath
and it worked fine...
the "/" in front of input.txt in getResourceAsStream() method is requires as per the description of that method in the API...otherwise that "." before txt extension will cause it to believe that its a java package...
regards
maulin
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Current directory as a file address?
 
Similar Threads
problem in uploading a file
Specifying relative path
net.sf.jasperreports.engine.JRException: java.io.FileNotFoundException
Absolute path , Relative Path
Unable to compile Packge servlet