Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Why file.exists() returns false on Windows 2003 server?

Status
Not open for further replies.

zhangling

Programmer
Jan 25, 2007
8
US
I have a simple jave program as below:

package us.ma.main;

import java.io.File;

public class DeleteFile {

public static void main(String[] args) {
String fileName = "c:/Barcode/BarcodeSeqence.txt";
File file = new File( fileName );
if( file.exists()){
file.delete();
System.out.println( fileName + " deleted." );
}
else{
System.out.println( fileName + " not found." );
}
}
}

The program is called by another application using the following command line (enclosed by #):

#java -Xms64m -Xmx512m -classpath ".;c:\barcode\ResetSequenceNumber.jar" us.ma.main.DeleteFile#

It worked fine on WIndows XP and Windows 2000 until the code was moved to Windows 2003 server. The calling application gives me this error message: "The system can not find the file specified". I add the following debug code and running on Windows 2003 server:

String user = System.getProperty("user.name");
System.out.println( "userName: " + user );
if( file.canRead()){
System.out.println( "Has read access" );
}
else{
System.out.println( "Can not read the file" );
}
if(file.canWrite()){
System.out.println( "Has write access" );
}
else{
System.out.println( "Can not write to the file" );
}

I found out that file.exists() returns false,
both if( file.canRead()) and if( file.canWrite())
return false even though the file is physically exist and the file is owned by Administrator.

Can anyone please advice?

I started this thread earlier, it seemed not posted successfully. Here I am trying it again.

Thanks.
Ling

 
The logic to delete file is packaged in ResetSequenceNumber.jar file. The application to launch this JAR file is a third party product. It is required to specify a command line in the application's configuration file.

The initial testing was on my XP workstation using cmd.exe.
It was then installed on Production (Windows 2000) server 9 month ago. We didn't have any problem until the server was upgraded to Windows 2003 server.

Why do I use 'java -Xms64m -Xmx512m' for executing a JAR in a command prompt? Because this is the only way I know.

Thank you for your time. Real appreciate for any kinds of help.

 
zhangling, the -Xms64m -Xmx512m options are there to tell the JVM at startup that it can start with a memory heap of 64 megabytes, but that the heap must not grow beyond 512 megabytes. For such a simple, small, application these are not required.

Tim
 
I tried to use \\ instead of / and got the same error. Thanks, Dian.

I am a C++ programmer and new to java. Thanks to Tim for teaching. It's great to know.

 
What release version of Java are you using? This does sound a bit like a bug in the JVM to me.

Tim
 
Well, you could either try updating your version 1.5 to the latest build (which is 19, I believe), or download and try out Java 1.6. It's entirely possible that either of these might fix your issue.

Tim
 
This sounds a lot like a security/access issue. Does the usercode that is running the actual commandline, have access to the directory and/or file you are trying to delete? If it's a (web)service or webserver, then most likely the account that starts the service doesn't have access there, while you, using cmd, are probably logged on as an administrator, so have access to (nearly) all dirs/files on the harddisk.

You could retrieve the current username using
Code:
System.getProperty("user.name")

HTH
TonHu
 
I found the problem. It's my mistake! The java program tried to look for a file: "BarcodeSeqence.txt"(incorrect spell). The actual file name is "BarcodeSequence.txt". That's how I got the error message.

I do want to apologize for any confusion that may cause.

Thank you all for the help!

 
Good old 'user error', eh? :) Don't worry, we all do things like this more times than we'd like to admit.

Tim
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top