Odd file.exists() problem

O

O.B.

I'm hoping there is just something syntactically wrong with the
following. I've verified that the file exists, but it keeps saying that
it doesn't. Thoughts?

String filename = "C:\\Java\\j2sdk1.4.2_10\\jre\\bin\\java";
File testFile = new File(filename);
if (testFile.exists()) {
System.out.println(filename + " exists.");
} else {
System.out.println(filename + " does not exist.");
}
 
O

Oliver Wong

O.B. said:
I'm hoping there is just something syntactically wrong with the following.
I've verified that the file exists, but it keeps saying that it doesn't.
Thoughts?

String filename = "C:\\Java\\j2sdk1.4.2_10\\jre\\bin\\java";
File testFile = new File(filename);
if (testFile.exists()) {
System.out.println(filename + " exists.");
} else {
System.out.println(filename + " does not exist.");
}

Might you be thinking "java.exe" instead of "java"?

- Oliver
 
O

O.B.

O.B. said:
I'm hoping there is just something syntactically wrong with the
following. I've verified that the file exists, but it keeps saying that
it doesn't. Thoughts?

String filename = "C:\\Java\\j2sdk1.4.2_10\\jre\\bin\\java";
File testFile = new File(filename);
if (testFile.exists()) {
System.out.println(filename + " exists.");
} else {
System.out.println(filename + " does not exist.");
}

Darn ... I hit send too early. My UNIX background got the best of me;
I'm missing ".exe". Argh!
 
D

Daniel Dyer

I'm hoping there is just something syntactically wrong with the
following. I've verified that the file exists, but it keeps saying that
it doesn't. Thoughts?

String filename = "C:\\Java\\j2sdk1.4.2_10\\jre\\bin\\java";
File testFile = new File(filename);
if (testFile.exists()) {
System.out.println(filename + " exists.");
} else {
System.out.println(filename + " does not exist.");
}

I think you are missing the file extension. Shouldn't it be "java.exe"?
If you are viewing the directory in Windows Explorer it hides known file
extensions by default.

Dan.
 
K

Knute Johnson

O.B. said:
Darn ... I hit send too early. My UNIX background got the best of me;
I'm missing ".exe". Argh!

You've probably just typed something wrong somewhere. Since you are a
unix fan you can use the / instead of the \\.
 
R

Raymond DeCampo

Knute said:
You've probably just typed something wrong somewhere. Since you are a
unix fan you can use the / instead of the \\.

Only if you want to break any routine that depends on File.separator.

Ray
 
C

Chris Uppal

Raymond said:
Only if you want to break any routine that depends on File.separator.

Any routine that depends carelessly on File.separator being the /only/
allowable path separator for the platform is already broken.

'/' has been a legal path separator on M$-ware since at least MS-DOS 3.

Incidentally, the Sun implementation of File normalises paths to use '\\'
throughout (on Windows). That's probably a good idea, since otherwise
File.separator would be totally useless rather than merely dangerously
misleading. But I don't know if it is documented to do so; if not then relying
on that aspect of its behaviour may not be justifiable in production code.

-- chris
 
D

Dimitri Maziuk

Chris Uppal sez:
Any routine that depends carelessly on File.separator being the /only/
allowable path separator for the platform is already broken.

'/' has been a legal path separator on M$-ware since at least MS-DOS 3.

Incidentally, the Sun implementation of File normalises paths to use '\\'
throughout (on Windows). That's probably a good idea, since otherwise
File.separator would be totally useless rather than merely dangerously
misleading. But I don't know if it is documented to do so; if not then relying
on that aspect of its behaviour may not be justifiable in production code.

I don't know what File normalises paths to, but IME
"some/path/some/file" works on both unix and winders whereas
"\\some\\path\\some\\file" is M$-only. Handy when you want to
read paths from a properties file.

Another undocumented trick is "java foo/bar/MyClass" -- you can
use tab-completion on unix command line.

Dima
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,810
Latest member
Kassie0918

Latest Threads

Top