Konrad Kar
Konrad Kar

Reputation: 11

Setting permissions for created directory to copy files into it

During the execution of my program it creates a directory which contains two sub-directories/two folders. Into one of these folders I need to copy a Jar-File. My programm resembles an installation routine. The copying of the Jar file is not the problem here, but the permissions of the created directories.
I tried to set the permissions of the directories (before actually creating them with the mkdirs() method) with File.setWritable(true, false) and also with the .setExecutable and .setReadable methods, but the access to the sub-directories is still denied.

Here's an excerpt of my code for the creation of one of the two sub-directories:

folderfile = new File("my/path/to/directory");
folderfile.setExecutable(true, false);
folderfile.setReadable(true, false);
folderfile.setWritable(true, false);
result = folderfile.mkdirs();

if (result) {
    System.out.println("Folder created.");
}else {
    JOptionPane.showMessageDialog(chooser, "Error");
}
File source = new File("src/config/TheJar.jar");
File destination = folderfile;

copyJar(source, destination);

And my "copyJar" method:

private void copyJar(File source, File dest) throws IOException {

        InputStream is = null;
        OutputStream os = null;
        try {
            is = new FileInputStream(source);
            os = new FileOutputStream(dest);
            byte[] buffer = new byte[1024];
            int length;
            while ((length = is.read(buffer))>0) {
                os.write(buffer, 0, length);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } 
        is.close();
        os.close();

    }

At os = new FileOutputStream(dest); the debugger throws a FileNotFoundException with the description that the access to the directory has been denied.

Does anyone have an idea what I am doing wrong or have a better solution for setting the permissions via Java? Thanks in advance!

Upvotes: 0

Views: 1020

Answers (2)

Konrad Kar
Konrad Kar

Reputation: 11

I solved the problem. In the end it was much easier to solve than expected.

The main problem was not the permission issue but the FileNotFoundException. The file that is assigned to the OutputStream is not really a file, but just a directory so that the Stream can't find it. You have to create the file before initializing the OutputStream and after that you copy your source file into the newly created file. The code:

private void copyJar(File source, File dest) throws IOException {

        InputStream is = null;
        File dest2 = new File(dest+"/TheJar.jar");
        dest2.createNewFile();
        OutputStream os = null;
        try {
            is = new FileInputStream(source);
            os = new FileOutputStream(dest2);
            byte[] buffer = new byte[1024];
            int length;
            while ((length = is.read(buffer))>0) {
                os.write(buffer, 0, length);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } 
        is.close();
        os.close();

    }

Upvotes: 0

Azylaans
Azylaans

Reputation: 1229

A similar question was asked there are several years.

A possible solution for Java 7 and Unix system is available here : How do i programmatically change file permissions?

Or, below the best response, a example with JNA.

I hope that that will help you !

Upvotes: 1

Related Questions