File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes getting path of a file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "getting path of a file" Watch "getting path of a file" New topic
Author

getting path of a file

Deyna Cegielski
Ranch Hand

Joined: Nov 24, 2004
Posts: 60
at the moment i am running a .bat file using its absolute path but id like for it to be able to execute it no-matter where the file exists (as long as it in the same folder as my .java file

is there any easy way to do this?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38765
    
  23
Have you looked in the java.io.File class in the API specification?
You can put the name in as the argument in the contructor, and there are two methods which get the path. Have you tried that? Does it work?
Do you want canonical path or absolute?
If you aren't sure, try absolute first and see whether it works.

CR
Deyna Cegielski
Ranch Hand

Joined: Nov 24, 2004
Posts: 60
yup i had a nose in my book and the io class has the methods i need.

but the fact stil remains i need to hardcode the directory to which my program will reside? id like this to be dynamic so when my program loads it will execute a .bat stored in the same directory as the .java file (there could be anywhere).

is there any way around this?
[ April 15, 2006: Message edited by: Deyna Cegielski ]
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18825
    
  40

This is actually an issue for the batch file, and not the java program. The java program can't fix this by itself, because the path and classpath may also not be correct, and hence, it will fail to run.

I am assuming this is for windows (as you called it a ".bat")... When a batch file is executed, the "%0" variable contains the complete information about it -- it drive, path, and name.

You need to get the drive and name, modify your path and/or classpath based on it, and then start your java program. And if you want your java program to know what it is, you can either set an environment variable, or pass it as a parameter.

BTW, you will probably have to do some parsing to get "%0" to what you want. For that you can use this.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Deyna Cegielski
Ranch Hand

Joined: Nov 24, 2004
Posts: 60
ok i understand what your saying, but this is the way i am currently trying to implement it:

try {
Runtime r;
Process p;
String[] cmds = {"cmd", "/k", "C:\\BatchFile.bat"};
r = Runtime.getRuntime();
p = r.exec(cmds);
}
catch(Exception e) {
System.out.println("Exception: " + e);
}

this is the code to execute the .bat file from java. as you can see the pathname to the file is hardcoded into the program (and this works fine but i am aiming to make it dynamic)

using:

File f = new File("BatchFile.bat");
String path = f.getAbsolutePath();

works and returns full path of the file BUT the problem i am having is with string that is returned. it is returned in the form C:\folder\subfoler\filename but due to the path localization in java i cannot use

String[] cmds = {"cmd", "/k", path};

the string needs to be in the form C:\\folder\\subfolder\\filename and i have been trying to look for a way to modify the string into this format.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18825
    
  40

Sorry... I thought the batch file was running the Java program -- not the other way around.

Henry
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18825
    
  40


File f = new File("BatchFile.bat");
String path = f.getAbsolutePath();

works and returns full path of the file BUT the problem i am having is with string that is returned. it is returned in the form C:\folder\subfoler\filename but due to the path localization in java i cannot use

String[] cmds = {"cmd", "/k", path};

the string needs to be in the form C:\\folder\\subfolder\\filename and i have been trying to look for a way to modify the string into this format.


I think you have a slight mis-understanding with Java Strings... The reason that you need to do this...



Is because the "\" has special meaning for a java string literal. In order words, a "\\" is a single backslash. Try...



to check it out. You'll notice that it prints a single backslash.

So... what is being returned from getAbsolutePath() is what you want.

Henry
[ April 15, 2006: Message edited by: Henry Wong ]
Deyna Cegielski
Ranch Hand

Joined: Nov 24, 2004
Posts: 60
ye i am aware of the need to use "\\" instead of "\" to get an actual backslash, but if i use

String[] cmds = {"cmd", "/k", "C:\folder\subfolder\filename"};

it doesnt work but if i use

String[] cmds = {"cmd", "/k", "C:\\folder\\subfolder\\filename};

it does work

but getAbsolutePath() returns a string with single \'s as you would expect

i need to modify this string changing all the occurence of "\" to "\\"
[ April 15, 2006: Message edited by: Deyna Cegielski ]
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18825
    
  40

Let me try another tact... when you do this...




what do you get? And how are the two strings different?

Henry
Deyna Cegielski
Ranch Hand

Joined: Nov 24, 2004
Posts: 60
you would get

C:\folder\subfolder\filename and
C:\folder\subfolder\filename

now if we use getAbsolutePath() and store it in a String called path

path = "C:\folder\subfolder\filename"

and use

String[] cmds = {"cmd", "/k", path};
r = Runtime.getRuntime();
p = r.exec(cmds);

this will not execute my .bat file (i know this because i cannot connect to the database)

what im saying is in order for

p = r.exec(cmds);

to work, path need to be a string with the general syntax of "C:\\folder\\subfolder\\filename" so hence i need to modify the original path from

"C:\folder\subfolder\filename"

to

"C:\\folder\\subfolder\\filename"

i know the 2nd string works as i have hardcoded it to test and after running my program i CAN connect to my database
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18825
    
  40

Sorry, but you are still *not* getting it...


now if we use getAbsolutePath() and store it in a String called path

path = "C:\folder\subfolder\filename"


No... to "use the getAbsolutePath() and store it in a String called path", you have to do this...





Anyway... since you really want to do this.... here is how you convert a string from a single backslash to two backslashes.



Henry
Deyna Cegielski
Ranch Hand

Joined: Nov 24, 2004
Posts: 60
hmmm i think we are not quite on the same wavelegth on this one

i purposely ommitted code thinking you would understand what i was saying

im familiar with the File class yet im seeming to come across as telling you i dont understand how to get the absolute path of a file. to be hoenst my problem wasnt with the File class but modifying the string f.getAbsolutePath() returned to use with the Runtimeclass, which you have explained in your final post.

thanks
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18825
    
  40

to be hoenst my problem wasnt with the File class but modifying the string f.getAbsolutePath() returned to use with the Runtimeclass, which you have explained in your final post.


What I was trying to tell you is that the string being returned from the getAbsolutePath() is correct. It doesn't have to be converted for the Runtime class.

I didn't do you a favor by answering your question. All I did was cause a problem that you may encounter later.

Henry
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18825
    
  40

Apologies... with one foot out the door... I totally forgot the regex !! ...

The correct syntax should have been...



In any case, my position still holds. I still don't think that you need to add the extra backslashes.

Henry
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: getting path of a file