Home > Mobile >  Jar file does not have access to write to files with an extension [closed]
Jar file does not have access to write to files with an extension [closed]

Time:09-21

On a Windows 10 machine, I have a Java project in Eclipse. When I run the main method in Eclipse using a launcher, the program correctly writes to a csv file. It works when the filename is an arbitrary extension or no extension as well. I am using the class FileWriter to write files.

I exported the project as a runnable jar, and when I run the jar in the command prompt using "java -jar", and I use the extension ".csv",".txt", ".doc", ".xls" (among others), I get a FileNotFoundException that says access is denied. However, when I provide a filename with no extension or with an extension like ".foo", running the jar successfully writes out a file. I'd like to get it so that the jar writes out a file with a "csv" extension.

java.io.FileNotFoundException: csv\report.csv (Access is denied)
    at java.base/java.io.FileOutputStream.open0(Native Method)
    at java.base/java.io.FileOutputStream.open(FileOutputStream.java:291)
    at java.base/java.io.FileOutputStream.<init>(FileOutputStream.java:234)
    at java.base/java.io.FileOutputStream.<init>(FileOutputStream.java:123)
    at java.base/java.io.FileWriter.<init>(FileWriter.java:66)
    at project.TestRunner.createCSVFile(TestRunner.java:103)

UPDATE Before I was using a 64-bit version of Java 16 (see below).

java version "16.0.2" 2021-07-20
Java(TM) SE Runtime Environment (build 16.0.2 7-67)
Java HotSpot(TM) 64-Bit Server VM (build 16.0.2 7-67, mixed mode, sharing)

When I used a 32-bit version of Java 8, the problem disappeared.

java version "1.8.0_301"
Java(TM) SE Runtime Environment (build 1.8.0_301-b09)
Java HotSpot(TM) Client VM (build 25.301-b09, mixed mode, sharing)

The problem ended up being an overprotective anti-virus software.

CodePudding user response:

I think that you are mistaken about the cause of this.

Windows file extensions are (fundamentally) a convention. As you noted, Java will happily read and write files with any extension ... or no extension. The Java itself doesn't care, and the Windows OS allows it.

Apparently, you are seeing some different behaviors when you put your application in a JAR file, and run it using java -jar ....

Firstly, this is NOT Java's doing. Java is entirely agnostic about the file extensions. And there is no substantial difference between executing from a JAR or from within an IDE. (Assuming that the JAR was made correctly. And even then, the differences I am thinking about wouldn't have this effect.)

Secondly, I don't think this is Windows doing. As far as I know, Windows itself does not implement file access control that is sensitive to filename extensions. (Or if it does, I can't find this in the MS Windows documentation.)

That leaves the following possibilities:

  • It could potentially be something like an over-protective antivirus, or a localization to Windows access control. (I don't know if this is technically possible ...)

  • The most likely explanation is that you are misinterpreting the error messages. For example:

    • They could be talking about the permissions on the directory rather than the file.
    • It could be that you are trying to overwrite an existing file which you don't have write permissions for.
    • You are using relative pathnames, and are running in a different "current directory".
    • Something else like that.

UPDATE - Based on the stacktrace, I suspect that this is a "current directory" problem.

  • Related