Asked  7 Months ago    Answers:  5   Viewed   29 times

In Java, I'm dynamically creating a set of files and I'd like to change the file permissions on these files on a linux/unix file system. I'd like to be able to execute the Java equivalent of chmod. Is that possible Java 5? If so, how?

I know in Java 6 the File object has setReadable()/setWritable() methods. I also know I could make a system call to do this, but I'd like to avoid that if possible.

 Answers

71

Full control over file attributes is available in Java 7, as part of the "new" New IO facility (NIO.2). For example, POSIX permissions can be set on an existing file with setPosixFilePermissions(), or atomically at file creation with methods like createFile() or newByteChannel().

You can create a set of permissions using EnumSet.of(), but the helper method PosixFilePermissions.fromString() will uses a conventional format that will be more readable to many developers. For APIs that accept a FileAttribute, you can wrap the set of permissions with with PosixFilePermissions.asFileAttribute().

Set<PosixFilePermission> ownerWritable = PosixFilePermissions.fromString("rw-r--r--");
FileAttribute<?> permissions = PosixFilePermissions.asFileAttribute(ownerWritable);
Files.createFile(path, permissions);

In earlier versions of Java, using native code of your own, or exec-ing command-line utilities are common approaches.

Tuesday, June 1, 2021
 
Owen
answered 7 Months ago
17

just use File.getName()

File f = new File("C:\Hello\AnotherFolder\The File Name.PDF");
System.out.println(f.getName());

using String methods:

  File f = new File("C:\Hello\AnotherFolder\The File Name.PDF");  
System.out.println(f.getAbsolutePath().substring(f.getAbsolutePath().lastIndexOf("\")+1));
Wednesday, June 9, 2021
 
turson
answered 6 Months ago
46

Setting user.dir is unsupported. It should be considered a read-only property.

For example the evaluation of Bug 4117557 in the Sun Bug Parade contains this text:

"user.dir", which is initialized during jvm startup, should be used as an informative/readonly system property, try to customize it via command line -Duser.dir=xyz will end up at implementation dependend/unspecified behavior.

While this text is about setting it on the command line, setting it via setProperty() is most likely equally undefined.

When you can reproduce the problem without setting user.dir manually, then you've found a genuine problem.

Monday, July 5, 2021
 
mnagel
answered 6 Months ago
17

There is an issue already filed about this at the OpenJFX docs.

While it hasn't been resolved yet, there is a possible workaround, based on:

NetBeans only adds javadoc/source jars for a jar with the exact same name and -javadoc/-source suffix

So here are the steps to solve it:

  • Install NetBeans 10 and JDK 11.0.2.

  • Clone the HelloFX sample for NetBeans and Maven, from the OpenJFX samples.

  • Update the JavaFX dependencies to 11.0.2.

    <dependency>
        <groupId>org.openjfx</groupId>
        <artifactId>javafx-controls</artifactId>
        <version>11.0.2</version>
    </dependency>
    <dependency>
        <groupId>org.openjfx</groupId>
        <artifactId>javafx-fxml</artifactId>
        <version>11.0.2</version>
    </dependency>
    
  • Run it:

    mvn clean compile exec:java
    
  • Check that the JavaFX dependencies have been downloaded to your local m2 repository. Under <user home>/.m2/repository/org/openjfx/javafx-base/11.0.2 for instance you will find javafx-base-11.0.2.jar and javafx-base-mac-11.0.2.jar (or win, or linux based on your platform).

  • Back on NetBeans, right click in the Dependencies folder and select Download Sources (see the task progress in the bottom right taskbar), and then Download Javadoc(see the task progress).

  • Go to your m2 repository and verify that there are now -source and -javadoc jar files.

However, this won't solve the issue yet, there is an extra step:

  • In your m2 repository, manually rename the -source and -javadoc jar files using your platform classifier, to -mac-source and -mac-javadoc (or win, or linux based on your platform). Do this for the different JavaFX modules:

Back to NetBeans, check that now you have JavaDoc, or if you press Ctrl/CMD+Click you can access the source.

Note that this fix has to be done only once, the rest of your Maven projects should pick JavaDoc and Sources.

Thursday, August 5, 2021
 
spender
answered 4 Months ago
85

OK, I seem to have figured it out, thanks to cocarin for pointing to Far sources. Far uses NtQueryInformationFile to get times and NtSetInformationFile to set them, and FILE_BASIC_INFORMATION structure contains all 4 times, including change time.

QueryInformationFile docs: http://msdn.microsoft.com/en-us/library/windows/hardware/ff567052(v=vs.85).aspx (ZwQueryInformationFile)

SetInformationFile docs: http://msdn.microsoft.com/en-us/library/windows/hardware/ff567096(v=vs.85).aspx (ZwSetInformationFile)

FILE_BASIC_INFORMATION docs: http://msdn.microsoft.com/en-us/library/windows/hardware/ff545762(v=vs.85).aspx

typedef struct _FILE_BASIC_INFORMATION {
  LARGE_INTEGER CreationTime;
  LARGE_INTEGER LastAccessTime;
  LARGE_INTEGER LastWriteTime;
  LARGE_INTEGER ChangeTime;        // <--- win!
  ULONG         FileAttributes;
} FILE_BASIC_INFORMATION, *PFILE_BASIC_INFORMATION;

Not sure if there are any nice wrappers for Python but this is good enough, I can write my own wrapper.

Thursday, October 14, 2021
 
fancyPants
answered 2 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :  
Share