简体   繁体   中英

Linux write to file from java jar

I am writing to text file from a Java jar executable. The jar generates the correct file but the permission of the file is set to root, therefore my other Javascript cannot read the file. It returns "Permission Denied" when I attempt to read the file via ajax. How can I fix the issue when creating the file in Java? I am currently running Java 1.6.

I did not have this issue when I was running the application within eclipse. Once I ported it over to jar permission changed to root access.

I am running the jar a

exec('java -jar writeToFile.jar');

The writing to file is standard

public void writeContents(String fp,String contents){
    try{
        BufferedWriter write = new BufferedWriter(new FileWriter(fp));
        write.write(contents);
        write.close();
    }catch(Exception e){
        System.out.println("Failed to write to file " + fp + " error: " + e.toString());
        e.printStackTrace();
    }
}

Writing to file works fine, it creates the file the correct directory and with the desired data, only difference between running it in eclipse and running it via jar is the file permission. The jar set the file to root.

I'm not sure, but with Files class you can read and write attributes in files. May be there are some specifict attribute to your Operational System that must be set.

There are a setOwner method if you want to manipulate it too.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM