简体   繁体   English

Linux从Java jar写入文件

[英]Linux write to file from java jar

I am writing to text file from a Java jar executable. 我正在从Java jar可执行文件写入文本文件。 The jar generates the correct file but the permission of the file is set to root, therefore my other Javascript cannot read the file. 该jar会生成正确的文件,但是该文件的权限设置为root,因此我的其他Javascript无法读取该文件。 It returns "Permission Denied" when I attempt to read the file via ajax. 当我尝试通过ajax读取文件时,它返回“权限被拒绝”。 How can I fix the issue when creating the file in Java? 在Java中创建文件时如何解决该问题? I am currently running Java 1.6. 我当前正在运行Java 1.6。

I did not have this issue when I was running the application within eclipse. 当我在Eclipse中运行应用程序时,我没有这个问题。 Once I ported it over to jar permission changed to root access. 一旦将其移植到jar上,权限将更改为root访问。

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. 写入文件效果很好,它会使用正确的目录和所需的数据创建文件,只有在eclipse中运行文件和通过jar运行文件之间的区别是文件权限。 The jar set the file to root. jar将文件设置为root。

I'm not sure, but with Files class you can read and write attributes in files. 我不确定,但是使用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. 如果您也想操作它,则有一个setOwner方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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