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.
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.
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.
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.
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
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
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop