Example usage for java.nio.file Files isExecutable

List of usage examples for java.nio.file Files isExecutable

Introduction

In this page you can find the example usage for java.nio.file Files isExecutable.

Prototype

public static boolean isExecutable(Path path) 

Source Link

Document

Tests whether a file is executable.

Usage

From source file:Main.java

public static void main(String[] args) throws Exception {
    Path path = FileSystems.getDefault().getPath("/home/docs/users.txt");
    System.out.println(Files.isExecutable(path));
}

From source file:Main.java

public static void main(String[] args) {

    Path path = FileSystems.getDefault().getPath("C:/tutorial/Java/JavaFX", "Demo.txt");

    boolean is_executable = Files.isExecutable(path);
}

From source file:Test.java

private static void displayFileAttributes(Path path) throws Exception {
    String format = "Exists: %s %n" + "notExists: %s %n" + "Directory: %s %n" + "Regular: %s %n"
            + "Executable: %s %n" + "Readable: %s %n" + "Writable: %s %n" + "Hidden: %s %n" + "Symbolic: %s %n"
            + "Last Modified Date: %s %n" + "Size: %s %n";

    System.out.printf(format, Files.exists(path, LinkOption.NOFOLLOW_LINKS),
            Files.notExists(path, LinkOption.NOFOLLOW_LINKS),
            Files.isDirectory(path, LinkOption.NOFOLLOW_LINKS),
            Files.isRegularFile(path, LinkOption.NOFOLLOW_LINKS), Files.isExecutable(path),
            Files.isReadable(path), Files.isWritable(path), Files.isHidden(path), Files.isSymbolicLink(path),
            Files.getLastModifiedTime(path, LinkOption.NOFOLLOW_LINKS), Files.size(path));
}

From source file:it.sonarlint.cli.tools.CommandExecutor.java

public int execute(String[] args, @Nullable Path workingDir, Map<String, String> addEnv) throws IOException {
    if (!Files.isExecutable(file)) {
        Set<PosixFilePermission> perms = new HashSet<PosixFilePermission>();
        perms.add(PosixFilePermission.OWNER_READ);
        perms.add(PosixFilePermission.OWNER_EXECUTE);
        Files.setPosixFilePermissions(file, perms);
    }/*from  w  w w .j  ava 2s .  c  om*/

    ExecuteWatchdog watchdog = new ExecuteWatchdog(TIMEOUT);
    CommandLine cmd = new CommandLine(file.toFile());
    cmd.addArguments(args);
    DefaultExecutor exec = new DefaultExecutor();
    exec.setWatchdog(watchdog);
    exec.setStreamHandler(createStreamHandler());
    exec.setExitValues(null);
    if (workingDir != null) {
        exec.setWorkingDirectory(workingDir.toFile());
    }
    in.close();
    LOG.info("Executing: {}", cmd.toString());
    Map<String, String> env = new HashMap<>(System.getenv());
    env.putAll(addEnv);
    return exec.execute(cmd, env);
}

From source file:com.sonar.scanner.api.it.tools.CommandExecutor.java

public int execute(String[] args, Map<String, String> env, @Nullable Path workingDir) throws IOException {
    if (!Files.isExecutable(file)) {
        Set<PosixFilePermission> perms = new HashSet<PosixFilePermission>();
        perms.add(PosixFilePermission.OWNER_READ);
        perms.add(PosixFilePermission.OWNER_EXECUTE);
        Files.setPosixFilePermissions(file, perms);
    }/*from   ww w  . j  av a  2 s.co m*/

    watchdog = new ExecuteWatchdog(TIMEOUT);
    CommandLine cmd = new CommandLine(file.toFile());
    cmd.addArguments(args, false);
    DefaultExecutor exec = new DefaultExecutor();
    exec.setWatchdog(watchdog);
    exec.setStreamHandler(createStreamHandler());
    exec.setExitValues(null);
    if (workingDir != null) {
        exec.setWorkingDirectory(workingDir.toFile());
    }
    in.close();
    LOG.info("Executing: {}", cmd.toString());
    return exec.execute(cmd, env);
}

From source file:its.tools.CommandExecutor.java

public void execute(String[] args, @Nullable Path workingDir) throws IOException {
    if (!Files.isExecutable(file)) {
        Set<PosixFilePermission> perms = new HashSet<PosixFilePermission>();
        perms.add(PosixFilePermission.OWNER_READ);
        perms.add(PosixFilePermission.OWNER_EXECUTE);
        Files.setPosixFilePermissions(file, perms);
    }/*w  w  w.  j  a v a2 s .c  o m*/

    watchdog = new ExecuteWatchdog(TIMEOUT);
    CommandLine cmd = new CommandLine(file.toFile());
    cmd.addArguments(args);
    DefaultExecutor exec = new DefaultExecutor();
    exec.setWatchdog(watchdog);
    exec.setStreamHandler(createStreamHandler());
    exec.setExitValues(null);
    if (workingDir != null) {
        exec.setWorkingDirectory(workingDir.toFile());
    }
    in.close();
    LOG.info("Executing: {}", cmd.toString());
    exec.execute(cmd, new ResultHander());
}

From source file:org.osiam.OsiamHome.java

public void initialize() {
    try {/* w  ww  .  j  av  a  2s  .  com*/
        if (Files.notExists(osiamHome)) {
            Files.createDirectories(osiamHome);
        } else {
            checkState(Files.isDirectory(osiamHome), "'osiam.home' (%s) is not a directory", osiamHome);
            checkState(Files.isReadable(osiamHome), "'osiam.home' (%s) is not readable", osiamHome);
            checkState(Files.isExecutable(osiamHome), "'osiam.home' (%s) is not accessible", osiamHome);
        }

        if (!isEmpty(osiamHome)) {
            return;
        }

        checkState(Files.isWritable(osiamHome), "'osiam.home' (%s) is not writable", osiamHome);
        Resource[] resources = new PathMatchingResourcePatternResolver().getResources("classpath:/home/**/*");
        for (Resource resource : resources) {
            // don't process directories
            if (resource.getURL().toString().endsWith("/")) {
                continue;
            }
            copyToHome(resource, osiamHome);
        }
        if (Files.notExists(osiamHome.resolve("data"))) {
            Files.createDirectories(osiamHome.resolve("data"));
        }

        hasInitializedHome = true;
    } catch (IOException e) {
        throw new IllegalStateException("Could not initialize osiam.home", e);
    }
}

From source file:org.hawkular.metrics.clients.ptrans.fullstack.CollectdITest.java

private void assumeCollectdIsPresent() {
    Path path = Paths.get(COLLECTD_PATH);
    assumeTrue(COLLECTD_PATH + " does not exist", Files.exists(path));
    assumeTrue(COLLECTD_PATH + " is not a file", Files.isRegularFile(path));
    assumeTrue(COLLECTD_PATH + " is not executable", Files.isExecutable(path));
}

From source file:com.facebook.buck.zip.UnzipTest.java

@Test
public void testExtractZipFilePreservesExecutePermissionsAndModificationTime() throws IOException {

    // getFakeTime returs time with some non-zero millis. By doing division and multiplication by
    // 1000 we get rid of that.
    final long time = ZipConstants.getFakeTime() / 1000 * 1000;

    // Create a simple zip archive using apache's commons-compress to store executable info.
    try (ZipArchiveOutputStream zip = new ZipArchiveOutputStream(zipFile.toFile())) {
        ZipArchiveEntry entry = new ZipArchiveEntry("test.exe");
        entry.setUnixMode((int) MorePosixFilePermissions.toMode(PosixFilePermissions.fromString("r-x------")));
        entry.setSize(DUMMY_FILE_CONTENTS.length);
        entry.setMethod(ZipEntry.STORED);
        entry.setTime(time);/*from ww  w.j a  v  a  2  s. c o  m*/
        zip.putArchiveEntry(entry);
        zip.write(DUMMY_FILE_CONTENTS);
        zip.closeArchiveEntry();
    }

    // Now run `Unzip.extractZipFile` on our test zip and verify that the file is executable.
    Path extractFolder = tmpFolder.newFolder();
    ImmutableList<Path> result = Unzip.extractZipFile(zipFile.toAbsolutePath(), extractFolder.toAbsolutePath(),
            Unzip.ExistingFileMode.OVERWRITE);
    Path exe = extractFolder.toAbsolutePath().resolve("test.exe");
    assertTrue(Files.exists(exe));
    assertThat(Files.getLastModifiedTime(exe).toMillis(), Matchers.equalTo(time));
    assertTrue(Files.isExecutable(exe));
    assertEquals(ImmutableList.of(extractFolder.resolve("test.exe")), result);
}

From source file:com.ibm.streamsx.topology.internal.context.remote.ZippedToolkitRemoteContext.java

private static void addAllToZippedArchive(Map<Path, String> starts, Path zipFilePath) throws IOException {
    try (ZipArchiveOutputStream zos = new ZipArchiveOutputStream(zipFilePath.toFile())) {
        for (Path start : starts.keySet()) {
            final String rootEntryName = starts.get(start);
            Files.walkFileTree(start, new SimpleFileVisitor<Path>() {
                public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                    // Skip pyc files.
                    if (file.getFileName().toString().endsWith(".pyc"))
                        return FileVisitResult.CONTINUE;

                    String entryName = rootEntryName;
                    String relativePath = start.relativize(file).toString();
                    // If empty, file is the start file.
                    if (!relativePath.isEmpty()) {
                        entryName = entryName + "/" + relativePath;
                    }/*from www  .  ja  va  2 s. c  o m*/
                    // Zip uses forward slashes
                    entryName = entryName.replace(File.separatorChar, '/');

                    ZipArchiveEntry entry = new ZipArchiveEntry(file.toFile(), entryName);
                    if (Files.isExecutable(file))
                        entry.setUnixMode(0100770);
                    else
                        entry.setUnixMode(0100660);

                    zos.putArchiveEntry(entry);
                    Files.copy(file, zos);
                    zos.closeArchiveEntry();
                    return FileVisitResult.CONTINUE;
                }

                public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs)
                        throws IOException {
                    final String dirName = dir.getFileName().toString();
                    // Don't include pyc files or .toolkit 
                    if (dirName.equals("__pycache__"))
                        return FileVisitResult.SKIP_SUBTREE;

                    ZipArchiveEntry dirEntry = new ZipArchiveEntry(dir.toFile(), rootEntryName + "/"
                            + start.relativize(dir).toString().replace(File.separatorChar, '/') + "/");
                    zos.putArchiveEntry(dirEntry);
                    zos.closeArchiveEntry();
                    return FileVisitResult.CONTINUE;
                }
            });
        }
    }
}