java root权限,Java应用程序是否有获得root权限的方法?

在Java中使用`Files.walk()`遍历 `/var/` 目录时可能会遇到权限问题,导致`AccessDeniedException`。文章讨论了如何避免这种异常,提出通过两种方法实现:一是使用Stream过滤掉无法访问的路径,二是实现`FileVisitor`来提前检查目录权限并跳过不可访问的子树。这两种方法提供了在不提升权限的情况下遍历文件系统的灵活性。
摘要由CSDN通过智能技术生成

When running Files.walk(Paths.get("/var/")).count() as an unprivileged user, the execution might throw an exception as there are folders inside /var/ that need root permission to be traversed.

I am not looking for a way to execute a bash command as root (e.g. sudo find /var), using Process, etc.

I just want to make sure Files.walk(Paths.get("/var/")).count() does not throw an AccessDeniedException:

Exception in thread "restartedMain" java.lang.reflect.InvocationTargetException

at sun.reflect.NativeMethodAccessorImpl.invoke0

at sun.reflect.NativeMethodAccessorImpl.invoke

at sun.reflect.DelegatingMethodAccessorImpl.invoke

at java.lang.reflect.Method.invoke

at org.springframework.boot.devtools.restart.RestartLauncher.run

Caused by: java.io.UncheckedIOException: java.nio.file.AccessDeniedException: /var/cache/httpd

at java.nio.file.FileTreeIterator.fetchNextIfNeeded

at java.nio.file.FileTreeIterator.hasNext

at java.util.Iterator.forEachRemaining

at java.util.Spliterators$IteratorSpliterator.forEachRemaining

at java.util.stream.AbstractPipeline.copyInto

at java.util.stream.AbstractPipeline.wrapAndCopyInto

at java.util.stream.ReduceOps$ReduceOp.evaluateSequential

at java.util.stream.AbstractPipeline.evaluate

at java.util.stream.LongPipeline.reduce

at java.util.stream.LongPipeline.sum

at java.util.stream.ReferencePipeline.count

at com.example.DemoApplication.main

... 5 more

Caused by: java.nio.file.AccessDeniedException: /var/cache/httpd

at sun.nio.fs.UnixException.translateToIOException

at sun.nio.fs.UnixException.rethrowAsIOException

at sun.nio.fs.UnixException.rethrowAsIOException

at sun.nio.fs.UnixFileSystemProvider.newDirectoryStream

at java.nio.file.Files.newDirectoryStream

at java.nio.file.FileTreeWalker.visit

at java.nio.file.FileTreeWalker.next

at java.nio.file.FileTreeIterator.fetchNextIfNeeded

This is just an example. Using filter(...) it is possible to work around the exception. But this example can be expanded to other use cases too.

So in short Is this possible at all, for CLI, JavaFX, etc. apps to gain root permission after they have been executed from command line via a method such as java -jar app.jar?

解决方案

If what you want is actually skipping the paths where you have no access, you have two approaches:

Streams

In the answer to this question it is explained how to obtain the stream of all files of a subtree you can access.

But this example can be expanded to other use cases too.

FileVisitor

Using a FileVisitor adds a lot of code, but grants you much more flexibility when walking directory trees. To solve the same problem you can replace Files.walk() with:

Files.walkFileTree(Path start, FileVisitor super Path> visitor);

extending SimpleFileVisitor (to count the files) and overriding some methods.

You can:

Override the visitFileFailed method, to handle the case you cannot access a file for some reasons; (Lukasz_Plawny's advice)

(optional) Override the preVisitDirectory method, checking for permissions before accessing the directory: if you can't access it, you can simply skip its subtree (keep in mind that you may be able to access a directory, but not all its files);

e.g. 1

@Override

public FileVisitResult visitFileFailed(Path file, IOException exc) {

// you can log the exception 'exc'

return FileVisitResult.SKIP_SUBTREE;

}

e.g. 2

@Override

public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) {

if(!Files.isReadable(dir))

return FileVisitResult.SKIP_SUBTREE;

return FileVisitResult.CONTINUE;

}

Hope it helps.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值