• 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
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

getting path of a file

 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Marshal
Posts: 80230
424
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
author
Posts: 23958
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Deyna Cegielski
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 23958
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry... I thought the batch file was running the Java program -- not the other way around.

Henry
 
Henry Wong
author
Posts: 23958
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 23958
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 23958
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 23958
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 23958
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
a fool thinks himself to be wise, but a wise man knows himself to be a fool - shakespeare. foolish tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic