简体   繁体   English

使用Java在Linux中列出子目录/文件

[英]listing subdirectories/files in linux using java

I was trying to run this code on Linux as a normal user but due to insufficient privileges listfiles() returning null. 我试图以普通用户身份在Linux上运行此代码,但是由于特权不足,listfiles()返回null。

File parentDirectory = new File("/root");
File[] childFiles = parentDirectory.listFiles();

How can I run the above code without running java application in SU mode. 如何在不以SU模式运行Java应用程序的情况下运行上述代码。

  • Can I ask the user to enter root password? 我可以要求用户输入root密码吗? So that I can run linux commands instead of java functions. 这样我就可以运行linux命令而不是Java函数。

What all options I have to achieve this in java? 我必须在Java中实现这些所有选项吗?

You can make the /root/ directory world-readable or group-readable. 您可以将/root/目录设置为世界可读或组可读。 In the latter case, you must also add the user running the program to the appropriate group. 在后一种情况下,还必须将运行该程序的用户添加到适当的组中。

World readable: 世界可读:

sudo chmod o+r /root

Group readable: 群组可读:

sudo chmod g+r /root

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM