运行du-f linux命令并在java中获得结果
我有这样一个场景: 我需要连接到远程服务器(linux)并执行du-h命令来查找特定文件夹(比如/home/oracle/TEST)的磁盘使用情况。所以,我需要找到测试文件夹的磁盘使用情况。并用java打印结果。我该怎么做运行du-f linux命令并在java中获得结果,java,linux,runtime.exec,Java,Linux,Runtime.exec,我有这样一个场景: 我需要连接到远程服务器(linux)并执行du-h命令来查找特定文件夹(比如/home/oracle/TEST)的磁盘使用情况。所以,我需要找到测试文件夹的磁盘使用情况。并用java打印结果。我该怎么做 public static void main(String[] args) { String s = null; try { // run the Unix "ps -ef" command
public static void main(String[] args) {
String s = null;
try {
// run the Unix "ps -ef" command
Socket s1=new Socket("10.1.7.237",1521);
System.out.println(s1.isConnected());
System.out.println(s1.getRemoteSocketAddress());
System.out.println("connected");
System.out.println();
PrintWriter wr=new PrintWriter(new OutputStreamWriter(s1.getOutputStream()),true);
wr.println("Hi Server...");
wr.flush();
BufferedReader br=new BufferedReader(new InputStreamReader(s1.getInputStream()));
System.out.println(br.readLine());
Process p = Runtime.getRuntime().exec("du -h");
BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));
BufferedReader stdError = new BufferedReader(new InputStreamReader(p.getErrorStream()));
// read the output from the command
System.out.println("Here is the standard output of the command:\n");
while ((s = stdInput.readLine()) != null) {
System.out.println(s);
}
// read any errors from the attempted command
System.out.println("Here is the standard error of the command (if any):\n");
while ((s = stdError.readLine()) != null) {
System.out.println(s);
}
System.exit(0);
}
catch (IOException e) {
System.out.println("exception happened - here's what I know: ");
e.printStackTrace();
System.out.println(e.getMessage());
System.exit(-1);
}
}
我也尝试过这一点,但出现了以下异常:
Cannot run program "du": CreateProcess error=2, The system cannot find the file specified
java.io.IOException: Cannot run program "du": CreateProcess error=2, The system cannot find the file specified
at java.lang.ProcessBuilder.start(Unknown Source)
at java.lang.Runtime.exec(Unknown Source)
at java.lang.Runtime.exec(Unknown Source)
at java.lang.Runtime.exec(Unknown Source)
at testprog.main(testprog.java:27)
Caused by: java.io.IOException: CreateProcess error=2, The system cannot find the file specified
at java.lang.ProcessImpl.create(Native Method)
at java.lang.ProcessImpl.<init>(Unknown Source)
at java.lang.ProcessImpl.start(Unknown Source)
... 5 more
无法运行程序“du”:CreateProcess错误=2,系统找不到指定的文件
java.io.IOException:无法运行程序“du”:CreateProcess错误=2,系统找不到指定的文件
位于java.lang.ProcessBuilder.start(未知源)
位于java.lang.Runtime.exec(未知源)
位于java.lang.Runtime.exec(未知源)
位于java.lang.Runtime.exec(未知源)
位于testprog.main(testprog.java:27)
原因:java.io.IOException:CreateProcess error=2,系统找不到指定的文件
在java.lang.ProcessImpl.create(本机方法)
位于java.lang.ProcessImpl。(未知源)
位于java.lang.ProcessImpl.start(未知源)
... 还有5个
在Java中执行命令时,您不是在经典的shell中。因此,您无法访问相同的环境变量、内置命令等。
尝试使用
/usr/bin/du
您不能使用Runtime.getRuntime().exec(“du-h”)代码>直接在远程机器上执行某些操作。该命令在启动java程序的机器上执行
回答您的问题。我不认为您实际上是在远程服务器上执行du-h命令。
getRuntime()获取本地主机上的运行时实例。
您应该使用一个可以远程登录到远程主机并在那里执行命令的库,如
你可以在这里看到:
此运行时正在主机计算机上而不是远程服务器上获取主机环境和exec“du-h”请尝试:
Runtime.getRuntime().exec("ssh ${hostname-goes-here} du -h /path/to/folder");
当然,您需要在客户端的路径上提供ssh
,为了使上述命令能够工作,您需要将公钥添加到~/.ssh/authorized_keys
文件中。我检测到错误:如果用tasklist替换du-h命令,程序工作正常。问题是Rumtime。getRuntime.exec()在当前运行时执行该命令(就像我在windows中所做的那样)但是我想在linux服务器上运行这个命令,而不是在windows上。这不会给你你想要的结果。您希望在远程计算机上执行您的命令。使用tasklist
可以得到windows框的结果,而不是远程linux机器的结果。