- 浏览: 58159 次
- 性别:
- 来自: 北京
文章分类
最新评论
这个是我写的系统采集类实现了CPU个数,CPU频率,内存信息,磁盘信息,CPU利用率,内存利用率,磁盘利用率,系统启动时间的获得
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package com.fenghuo.jsnmp.logic.task;
import java.io.*;
import java.util.StringTokenizer;
/**
* 用于执行linux命令
* @author huangxf
*
*/
public class LinuxExec {
/**
* 获取cpu使用情况
* @return
* @throws Exception
*/
public double getCpuUsage() throws Exception {
double cpuUsed = 0;
Runtime rt = Runtime.getRuntime();
Process p = rt.exec("top -b -n 1");// 调用系统的“top"命令
BufferedReader in = null;
try {
in = new BufferedReader(new InputStreamReader(p.getInputStream()));
String str = null;
String[] strArray = null;
while ((str = in.readLine()) != null) {
int m = 0;
if (str.indexOf(" R ") != -1) {// 只分析正在运行的进程,top进程本身除外 &&
strArray = str.split(" ");
for (String tmp : strArray) {
if (tmp.trim().length() == 0) {
continue;
}
if (++m == 9) {// 第9列为CPU的使用百分比(RedHat
cpuUsed += Double.parseDouble(tmp);
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
in.close();
}
return cpuUsed;
}
/**
* 内存监控
* @return
* @throws Exception
*/
public double getMemUsage() throws Exception {
double menUsed = 0;
Runtime rt = Runtime.getRuntime();
Process p = rt.exec("top -b -n 1");// 调用系统的“top"命令
BufferedReader in = null;
try {
in = new BufferedReader(new InputStreamReader(p.getInputStream()));
String str = null;
String[] strArray = null;
while ((str = in.readLine()) != null) {
int m = 0;
if (str.indexOf(" R ") != -1) {// 只分析正在运行的进程,top进程本身除外 &&
//
// System.out.println("------------------3-----------------");
strArray = str.split(" ");
for (String tmp : strArray) {
if (tmp.trim().length() == 0) {
continue;
}
if (++m == 10) {
// 9)--第10列为mem的使用百分比(RedHat 9)
menUsed += Double.parseDouble(tmp);
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
in.close();
}
return menUsed;
}
/**
* 获取磁盘空间大小
*
* @return
* @throws Exception
*/
public double getDeskUsage() throws Exception {
double totalHD = 0;
double usedHD = 0;
Runtime rt = Runtime.getRuntime();
Process p = rt.exec("df -hl");//df -hl 查看硬盘空间
BufferedReader in = null;
try {
in = new BufferedReader(new InputStreamReader(p.getInputStream()));
String str = null;
String[] strArray = null;
while ((str = in.readLine()) != null) {
int m = 0;
strArray = str.split(" ");
for (String tmp : strArray) {
if (tmp.trim().length() == 0) {
continue;
}
++m;
// System.out.println("----tmp----" + tmp);
if (tmp.indexOf("G") != -1) {
if (m == 2) {
// System.out.println("---G----" + tmp);
if (!tmp.equals("") && !tmp.equals("0")) {
totalHD += Double.parseDouble(tmp.substring(0, tmp.length() - 1)) * 1024;
}
}
if (m == 3) {
// System.out.println("---G----" + tmp);
if (!tmp.equals("none") && !tmp.equals("0")) {
usedHD += Double.parseDouble(tmp.substring(
0, tmp.length() - 1)) * 1024;
}
}
}
if (tmp.indexOf("M") != -1) {
if (m == 2) {
// System.out.println("---M---" + tmp);
if (!tmp.equals("") && !tmp.equals("0")) {
totalHD += Double.parseDouble(tmp.substring(0, tmp.length() - 1));
}
}
if (m == 3) {
// System.out.println("---M---" + tmp);
if (!tmp.equals("none") && !tmp.equals("0")) {
usedHD += Double.parseDouble(tmp.substring(
0, tmp.length() - 1));
// System.out.println("----3----" + usedHD);
}
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
in.close();
}
return (usedHD / totalHD) * 100;
}
/**
* 获得CPU的个数
* @return
* @throws java.lang.Exception
*/
public int getIntCpuNum() throws Exception {
int rs = 0;
Runtime rt = Runtime.getRuntime();
// Process p = rt.exec("cat /proc/cpuinfo | grep processor | wc -l");//获得CPU个数
Process p = rt.exec("cat /proc/cpuinfo");//获得CPU个数
BufferedReader in = null;
try {
in = new BufferedReader(new InputStreamReader(p.getInputStream()));
String str = null;
String[] strarr = null;
while ((str = in.readLine()) != null) {
strarr = str.split(":");
if (strarr[0].trim().equals("processor")) {
rs = Integer.valueOf(strarr[1].trim())+1;
}
}
// String str = in.readLine();
// System.out.println("============================================");
// System.out.println(str);
// str = str.trim();
// System.out.println(str);
// System.out.println("============================================");
// rs = Integer.valueOf(str);
} catch (Exception e) {
e.printStackTrace();
} finally {
in.close();
}
return rs;
}
/**
* 获得内存的大小
* @return
* @throws java.lang.Exception
*/
public int getIntMemMax() throws Exception {
int rs = 0;
Runtime rt = Runtime.getRuntime();
// Process p = rt.exec("cat /proc/meminfo|grep -i \"^memtotal\"|cut -d\":\" -f2|cut -d\"k\" -f1");//获得内存大小
Process p = rt.exec("cat /proc/meminfo");//获得内存大小
BufferedReader in = null;
try {
in = new BufferedReader(new InputStreamReader(p.getInputStream()));
int[] result = new int[4];
String str = null;
StringTokenizer token = null;
while ((str = in.readLine()) != null) {
token = new StringTokenizer(str);
if (!token.hasMoreTokens()) {
continue;
}
str = token.nextToken();
if (!token.hasMoreTokens()) {
continue;
}
if (str.equalsIgnoreCase("MemTotal:")) {
result[0] = Integer.parseInt(token.nextToken());
rs = result[0];
} else if (str.equalsIgnoreCase("MemFree:")) {
result[1] = Integer.parseInt(token.nextToken());
} else if (str.equalsIgnoreCase("SwapTotal:")) {
result[2] = Integer.parseInt(token.nextToken());
} else if (str.equalsIgnoreCase("SwapFree:")) {
result[3] = Integer.parseInt(token.nextToken());
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
in.close();
}
return rs / 1000;
}
/**
* 获得的CPUHMZ
* @return
* @throws java.lang.Exception
*/
public int getIntCpuHMZ() throws Exception {
int cpuhmz = 0;
Runtime rt = Runtime.getRuntime();
Process p = rt.exec("cat /proc/cpuinfo ");//读CPU信息文件
BufferedReader in = null;
try {
in = new BufferedReader(new InputStreamReader(p.getInputStream()));
String str = null;
String[] strarr = null;
while ((str = in.readLine()) != null) {
strarr = str.split(":");
if (strarr.length > 0) {
if (strarr[0].trim().equals("cpu MHz")) {
cpuhmz = Double.valueOf(strarr[1].trim()).intValue();
}
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
in.close();
}
return cpuhmz;
}
/**
* 获取磁盘空间大小(总)
*
* @return
* @throws Exception
*/
public int getDeskAll() throws Exception {
double totalHD = 0;
Runtime rt = Runtime.getRuntime();
Process p = rt.exec("df -hl");//df -hl 查看硬盘空间
BufferedReader in = null;
try {
in = new BufferedReader(new InputStreamReader(p.getInputStream()));
String str = null;
String[] strArray = null;
while ((str = in.readLine()) != null) {
int m = 0;
strArray = str.split(" ");
for (String tmp : strArray) {
if (tmp.trim().length() == 0) {
continue;
}
++m;
// System.out.println("----tmp----" + tmp);
if (tmp.indexOf("G") != -1) {
if (m == 2) {
// System.out.println("---G----" + tmp);
if (!tmp.equals("") && !tmp.equals("0")) {
totalHD += Double.parseDouble(tmp.substring(0, tmp.length() - 1)) * 1024;
}
}
if (m == 3) {
// System.out.println("---G----" + tmp);
}
}
if (tmp.indexOf("M") != -1) {
if (m == 2) {
// System.out.println("---M---" + tmp);
if (!tmp.equals("") && !tmp.equals("0")) {
totalHD += Double.parseDouble(tmp.substring(0, tmp.length() - 1));
}
}
if (m == 3) {
// System.out.println("---M---" + tmp);
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
in.close();
}
Double d = new Double(totalHD);
return d.intValue();
}
/**
* 查询系统时间
* @return
* @throws java.io.IOException
*/
public long getSysStartTime() throws IOException, InterruptedException {
long statime = 0;
Runtime rt = Runtime.getRuntime();
Process p = rt.exec("uptime");//查看系统启动时间
// System.out.println(p.exitValue());
// p.waitFor();
BufferedReader in = null;
try {
in = new BufferedReader(new InputStreamReader(p.getInputStream()));
String str = in.readLine();
String[] strs = str.split(",");
String[] rss = strs[1].trim().split(" ");
if (rss.length == 1) {
String[] times = rss[0].split(":");
int hour = Integer.valueOf(times[0]);
int min = Integer.valueOf(times[1]);
statime = (hour * 60 + min) * 60 * 1000;
} else if (rss.length == 2) {
int min = Integer.valueOf(rss[0]);
statime = min * 60 * 1000;
}
} catch (Exception e) {
e.printStackTrace();
} finally {
in.close();
}
return statime;
}
public void shutdownh() throws IOException {
Runtime rt = Runtime.getRuntime();
Process p = rt.exec("shutdown -h");
}
public void shutdownr() throws IOException {
Runtime rt = Runtime.getRuntime();
Process p = rt.exec("shutdown -r");
}
public static void main(String[] args) throws Exception {
LinuxExec cpu = new LinuxExec();
System.out.println("---------------cpu used:" + cpu.getCpuUsage() + "%");
System.out.println("---------------mem used:" + cpu.getMemUsage() + "%");
System.out.println("---------------HD used:" + cpu.getDeskUsage() + "%");
System.out.println("---------------CPU NUM:" + cpu.getIntCpuNum());
System.out.println("---------------CPU HMZ:" + cpu.getIntCpuHMZ());
System.out.println("---------------MEN MAX:" + cpu.getIntMemMax());
System.out.println("---------------DISK MAX:" + cpu.getDeskAll());
System.out.println("---------------STARTTIME:" + cpu.getSysStartTime());
System.out.println("------------jvm监控----------------------");
Runtime lRuntime = Runtime.getRuntime();
System.out.println("--------------Free Momery:" + lRuntime.freeMemory() + "K");
System.out.println("--------------Max Momery:" + lRuntime.maxMemory() + "K");
System.out.println("--------------Total Momery:" + lRuntime.totalMemory() + "K");
System.out.println("---------------Available Processors :" + lRuntime.availableProcessors());
}
}
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package com.fenghuo.jsnmp.logic.task;
import java.io.*;
import java.util.StringTokenizer;
/**
* 用于执行linux命令
* @author huangxf
*
*/
public class LinuxExec {
/**
* 获取cpu使用情况
* @return
* @throws Exception
*/
public double getCpuUsage() throws Exception {
double cpuUsed = 0;
Runtime rt = Runtime.getRuntime();
Process p = rt.exec("top -b -n 1");// 调用系统的“top"命令
BufferedReader in = null;
try {
in = new BufferedReader(new InputStreamReader(p.getInputStream()));
String str = null;
String[] strArray = null;
while ((str = in.readLine()) != null) {
int m = 0;
if (str.indexOf(" R ") != -1) {// 只分析正在运行的进程,top进程本身除外 &&
strArray = str.split(" ");
for (String tmp : strArray) {
if (tmp.trim().length() == 0) {
continue;
}
if (++m == 9) {// 第9列为CPU的使用百分比(RedHat
cpuUsed += Double.parseDouble(tmp);
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
in.close();
}
return cpuUsed;
}
/**
* 内存监控
* @return
* @throws Exception
*/
public double getMemUsage() throws Exception {
double menUsed = 0;
Runtime rt = Runtime.getRuntime();
Process p = rt.exec("top -b -n 1");// 调用系统的“top"命令
BufferedReader in = null;
try {
in = new BufferedReader(new InputStreamReader(p.getInputStream()));
String str = null;
String[] strArray = null;
while ((str = in.readLine()) != null) {
int m = 0;
if (str.indexOf(" R ") != -1) {// 只分析正在运行的进程,top进程本身除外 &&
//
// System.out.println("------------------3-----------------");
strArray = str.split(" ");
for (String tmp : strArray) {
if (tmp.trim().length() == 0) {
continue;
}
if (++m == 10) {
// 9)--第10列为mem的使用百分比(RedHat 9)
menUsed += Double.parseDouble(tmp);
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
in.close();
}
return menUsed;
}
/**
* 获取磁盘空间大小
*
* @return
* @throws Exception
*/
public double getDeskUsage() throws Exception {
double totalHD = 0;
double usedHD = 0;
Runtime rt = Runtime.getRuntime();
Process p = rt.exec("df -hl");//df -hl 查看硬盘空间
BufferedReader in = null;
try {
in = new BufferedReader(new InputStreamReader(p.getInputStream()));
String str = null;
String[] strArray = null;
while ((str = in.readLine()) != null) {
int m = 0;
strArray = str.split(" ");
for (String tmp : strArray) {
if (tmp.trim().length() == 0) {
continue;
}
++m;
// System.out.println("----tmp----" + tmp);
if (tmp.indexOf("G") != -1) {
if (m == 2) {
// System.out.println("---G----" + tmp);
if (!tmp.equals("") && !tmp.equals("0")) {
totalHD += Double.parseDouble(tmp.substring(0, tmp.length() - 1)) * 1024;
}
}
if (m == 3) {
// System.out.println("---G----" + tmp);
if (!tmp.equals("none") && !tmp.equals("0")) {
usedHD += Double.parseDouble(tmp.substring(
0, tmp.length() - 1)) * 1024;
}
}
}
if (tmp.indexOf("M") != -1) {
if (m == 2) {
// System.out.println("---M---" + tmp);
if (!tmp.equals("") && !tmp.equals("0")) {
totalHD += Double.parseDouble(tmp.substring(0, tmp.length() - 1));
}
}
if (m == 3) {
// System.out.println("---M---" + tmp);
if (!tmp.equals("none") && !tmp.equals("0")) {
usedHD += Double.parseDouble(tmp.substring(
0, tmp.length() - 1));
// System.out.println("----3----" + usedHD);
}
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
in.close();
}
return (usedHD / totalHD) * 100;
}
/**
* 获得CPU的个数
* @return
* @throws java.lang.Exception
*/
public int getIntCpuNum() throws Exception {
int rs = 0;
Runtime rt = Runtime.getRuntime();
// Process p = rt.exec("cat /proc/cpuinfo | grep processor | wc -l");//获得CPU个数
Process p = rt.exec("cat /proc/cpuinfo");//获得CPU个数
BufferedReader in = null;
try {
in = new BufferedReader(new InputStreamReader(p.getInputStream()));
String str = null;
String[] strarr = null;
while ((str = in.readLine()) != null) {
strarr = str.split(":");
if (strarr[0].trim().equals("processor")) {
rs = Integer.valueOf(strarr[1].trim())+1;
}
}
// String str = in.readLine();
// System.out.println("============================================");
// System.out.println(str);
// str = str.trim();
// System.out.println(str);
// System.out.println("============================================");
// rs = Integer.valueOf(str);
} catch (Exception e) {
e.printStackTrace();
} finally {
in.close();
}
return rs;
}
/**
* 获得内存的大小
* @return
* @throws java.lang.Exception
*/
public int getIntMemMax() throws Exception {
int rs = 0;
Runtime rt = Runtime.getRuntime();
// Process p = rt.exec("cat /proc/meminfo|grep -i \"^memtotal\"|cut -d\":\" -f2|cut -d\"k\" -f1");//获得内存大小
Process p = rt.exec("cat /proc/meminfo");//获得内存大小
BufferedReader in = null;
try {
in = new BufferedReader(new InputStreamReader(p.getInputStream()));
int[] result = new int[4];
String str = null;
StringTokenizer token = null;
while ((str = in.readLine()) != null) {
token = new StringTokenizer(str);
if (!token.hasMoreTokens()) {
continue;
}
str = token.nextToken();
if (!token.hasMoreTokens()) {
continue;
}
if (str.equalsIgnoreCase("MemTotal:")) {
result[0] = Integer.parseInt(token.nextToken());
rs = result[0];
} else if (str.equalsIgnoreCase("MemFree:")) {
result[1] = Integer.parseInt(token.nextToken());
} else if (str.equalsIgnoreCase("SwapTotal:")) {
result[2] = Integer.parseInt(token.nextToken());
} else if (str.equalsIgnoreCase("SwapFree:")) {
result[3] = Integer.parseInt(token.nextToken());
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
in.close();
}
return rs / 1000;
}
/**
* 获得的CPUHMZ
* @return
* @throws java.lang.Exception
*/
public int getIntCpuHMZ() throws Exception {
int cpuhmz = 0;
Runtime rt = Runtime.getRuntime();
Process p = rt.exec("cat /proc/cpuinfo ");//读CPU信息文件
BufferedReader in = null;
try {
in = new BufferedReader(new InputStreamReader(p.getInputStream()));
String str = null;
String[] strarr = null;
while ((str = in.readLine()) != null) {
strarr = str.split(":");
if (strarr.length > 0) {
if (strarr[0].trim().equals("cpu MHz")) {
cpuhmz = Double.valueOf(strarr[1].trim()).intValue();
}
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
in.close();
}
return cpuhmz;
}
/**
* 获取磁盘空间大小(总)
*
* @return
* @throws Exception
*/
public int getDeskAll() throws Exception {
double totalHD = 0;
Runtime rt = Runtime.getRuntime();
Process p = rt.exec("df -hl");//df -hl 查看硬盘空间
BufferedReader in = null;
try {
in = new BufferedReader(new InputStreamReader(p.getInputStream()));
String str = null;
String[] strArray = null;
while ((str = in.readLine()) != null) {
int m = 0;
strArray = str.split(" ");
for (String tmp : strArray) {
if (tmp.trim().length() == 0) {
continue;
}
++m;
// System.out.println("----tmp----" + tmp);
if (tmp.indexOf("G") != -1) {
if (m == 2) {
// System.out.println("---G----" + tmp);
if (!tmp.equals("") && !tmp.equals("0")) {
totalHD += Double.parseDouble(tmp.substring(0, tmp.length() - 1)) * 1024;
}
}
if (m == 3) {
// System.out.println("---G----" + tmp);
}
}
if (tmp.indexOf("M") != -1) {
if (m == 2) {
// System.out.println("---M---" + tmp);
if (!tmp.equals("") && !tmp.equals("0")) {
totalHD += Double.parseDouble(tmp.substring(0, tmp.length() - 1));
}
}
if (m == 3) {
// System.out.println("---M---" + tmp);
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
in.close();
}
Double d = new Double(totalHD);
return d.intValue();
}
/**
* 查询系统时间
* @return
* @throws java.io.IOException
*/
public long getSysStartTime() throws IOException, InterruptedException {
long statime = 0;
Runtime rt = Runtime.getRuntime();
Process p = rt.exec("uptime");//查看系统启动时间
// System.out.println(p.exitValue());
// p.waitFor();
BufferedReader in = null;
try {
in = new BufferedReader(new InputStreamReader(p.getInputStream()));
String str = in.readLine();
String[] strs = str.split(",");
String[] rss = strs[1].trim().split(" ");
if (rss.length == 1) {
String[] times = rss[0].split(":");
int hour = Integer.valueOf(times[0]);
int min = Integer.valueOf(times[1]);
statime = (hour * 60 + min) * 60 * 1000;
} else if (rss.length == 2) {
int min = Integer.valueOf(rss[0]);
statime = min * 60 * 1000;
}
} catch (Exception e) {
e.printStackTrace();
} finally {
in.close();
}
return statime;
}
public void shutdownh() throws IOException {
Runtime rt = Runtime.getRuntime();
Process p = rt.exec("shutdown -h");
}
public void shutdownr() throws IOException {
Runtime rt = Runtime.getRuntime();
Process p = rt.exec("shutdown -r");
}
public static void main(String[] args) throws Exception {
LinuxExec cpu = new LinuxExec();
System.out.println("---------------cpu used:" + cpu.getCpuUsage() + "%");
System.out.println("---------------mem used:" + cpu.getMemUsage() + "%");
System.out.println("---------------HD used:" + cpu.getDeskUsage() + "%");
System.out.println("---------------CPU NUM:" + cpu.getIntCpuNum());
System.out.println("---------------CPU HMZ:" + cpu.getIntCpuHMZ());
System.out.println("---------------MEN MAX:" + cpu.getIntMemMax());
System.out.println("---------------DISK MAX:" + cpu.getDeskAll());
System.out.println("---------------STARTTIME:" + cpu.getSysStartTime());
System.out.println("------------jvm监控----------------------");
Runtime lRuntime = Runtime.getRuntime();
System.out.println("--------------Free Momery:" + lRuntime.freeMemory() + "K");
System.out.println("--------------Max Momery:" + lRuntime.maxMemory() + "K");
System.out.println("--------------Total Momery:" + lRuntime.totalMemory() + "K");
System.out.println("---------------Available Processors :" + lRuntime.availableProcessors());
}
}
相关推荐
Java程序调用linux命令、脚本,支持程序在服务器上使用linux命令。工具类为:ProcessUtil,支持单个命令和批量命令执行函数,同时在工具类中添加了getFileLineNumByCmd函数支持通过linux命令获取文件行数的样例。
### Java调用Linux命令 Java提供了一种强大的机制来执行操作系统级别的任务,其中包括调用Linux命令。实现这一功能的核心是`Runtime`类中的`exec()`方法。这个方法允许Java应用程序创建一个新的进程来执行指定的...
java调用linux系统命令的封装工具类。
java调用Linux命令获取输出流,用于解决获取不到输出流的方法
本文将详细介绍如何使用Java调用Linux命令,以满足特定场景的需求,如文中提到的通过Java接口重启keepalived服务。首先,我们将理解Java调用Linux命令的基本原理,然后通过具体的代码示例来展示实现这一功能的过程。...
java 执行linux命令源码,java调用shell脚本源码,java web发布war到tomcat,servlet文件上传,ajax文件上传。 java web 上传war包、停止、启动、发布tomcat。 命令修改成自己的目录即可使用。
本文将详细探讨如何利用Java调用Linux命令,这一技能对于任何希望在Java应用中集成Linux功能的开发者来说都是至关重要的。 ### Java调用Linux命令的方法 Java提供了多种方式来执行外部命令或脚本,其中最常用的是...
sshxcute 就是这样一个框架工具集,它基于 JSCH 构建,允许工程师利用Java 代码通过 SSH 连接远程批量执行 Linux/UNIX 系统上的命令或者脚本,同时加入了判断成功与否,取回输出等多种实用功能。sshxcute 不管是针对...
在这个特定的问题"Java调用Linux CentOS系统安装的OpenOffice4报错"中,开发者可能在尝试使用Java来自动化处理文档,如转换Office格式,而OpenOffice4提供了一个开源的解决方案,可以将Microsoft Office文档转换为...
SSHXCUTE.jar是一个Java库,它允许程序员通过SSH连接到Linux系统并执行shell命令,而无需深入了解SSH的底层细节。 SSHXCUTE.jar的核心功能包括: 1. **建立SSH连接**:通过提供主机名、用户名、密码或密钥对信息,...
2. **Java调用Linux命令** - 在Java中,可以通过`Runtime.getRuntime().exec()`或`ProcessBuilder`类来执行Linux命令。在描述中提到的`getDiskUsage`方法就使用了`ProcessBuilder`来执行`df -hl -P`命令,获取磁盘...
Java调用DOS命令是指在Java程序中执行操作系统级别的命令,比如Windows下的CMD命令或Linux下的Shell命令。这种功能在很多场景下都很有用,比如自动化脚本、文件操作、系统管理等。Java提供了Runtime类和...
本文将深入讲解如何在Java程序中调用Linux命令,并通过实例来阐述这一过程。 一、Runtime类 `Runtime`类是每个Java应用程序都有的一个单例对象,它提供了执行外部进程的能力。通过`getRuntime()`方法可以获得`...
使用java远程调用Linux命令_java-linux-command
在Java中,我们可以通过`Runtime.exec()`或`ProcessBuilder`类来执行这样的系统命令。 首先,让我们详细了解一下`Runtime.exec()`方法。这个方法允许Java程序在运行时执行外部程序或命令。例如,如果我们要在CMD中...
本文将详细介绍如何在Java代码中调用Linux/Unix命令,以及相关的技术要点。 首先,Java提供了`Runtime`类和`ProcessBuilder`类来执行外部命令。`Runtime.getRuntime().exec()`方法是较早的方式,而`ProcessBuilder`...
java代码执行linux系统命令
这两个类提供了与操作系统交互的能力,让我们能够执行系统命令并获取其结果。下面我们将深入探讨如何在Java中调用OS命令来删除文件,并分析这种方法相对于Java内置的文件操作方法的优缺点。 首先,我们来看一个简单...
java调用操作系统命令源码工具类,包含远程输入用户名和密码方式及本地调用,同时兼容处理了Windows及Linux命令和执行结果的返回。使用者可以通过工具类方法直接调用,传入操作系统的命令即可执行,方法返回操作结果...
java调用执行系统命令的工作类。包括在Linux和windows系统,已经系统错误输出流的监控。