1

I have this code ;

package test;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.attribute.PosixFileAttributeView;
import java.nio.file.attribute.PosixFileAttributes;
import java.nio.file.attribute.PosixFilePermissions;

public class Test {

private static final String COMMAND = "/bin/bash -c umask -S";


public static String getUmask() {
    final Runtime runtime = Runtime.getRuntime();
    Process process = null;
    try {
        process = runtime.exec(COMMAND);
        BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
        String umask = reader.readLine();
        if (process.waitFor() == 0)
            return umask;
    } catch (final IOException e) {
        e.printStackTrace();
    } catch (final InterruptedException e) {
        e.printStackTrace();
        Thread.currentThread().interrupt();
    }
    return "";
}

public static void main(String[] args) throws IOException {
    /*
     * NIO
     */
    
    PosixFileAttributes attrs = Files.getFileAttributeView(Paths.get("testFile"), PosixFileAttributeView.class)
            .readAttributes();
    System.out.format("%s %s%n", attrs.owner().getName(), PosixFilePermissions.toString(attrs.permissions()));

    /*
     * execute shell command to get umask of current process
     */
    System.out.println(getUmask());

}

}

I am getting this error when i run the code ;

Exception in thread "main" java.lang.NullPointerException at test.Test.main(Test.java:41)

How can i solve this problem and can you edit the part which part should change? I am using Windows 10 OS. Thank you for your help...

Tarık
  • 13
  • 3
  • 2
    What OS are you using? It might not support POSIX. https://stackoverflow.com/questions/14415960/java-lang-unsupportedoperationexception-posixpermissions-not-supported-as-in may help you. – Jems Dec 27 '20 at 22:11
  • 1
    I think we can infer that the OP is using Windows .... from the pathname he is using. – Stephen C Dec 27 '20 at 23:43
  • 2
    Since you are using Windows, you will nee to use `DosFileAttributeView` or `AclFileAttributeView` to set the permissions. – Stephen C Dec 27 '20 at 23:50
  • Yes i am using Windows OS. But which part of the code should i fix to run this ? – Tarık Dec 28 '20 at 14:46

0 Answers0