「java获取系统运行状态」请允许获取系统运行状态

博主:adminadmin 2022-12-03 10:24:07 63

今天给各位分享java获取系统运行状态的知识,其中也会对请允许获取系统运行状态进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!

本文目录一览:

如何利用Java获取系统的信息(CPU,内存,各

我知道并且用过的 有一种方法 就是snmp服务 加上snmp jar包可以获取到内存 cpu 磁盘 进程 网络等信息

请问如何知道JAVA 程序系统运行环境,如内存,硬盘等?

Runtime类获得的内存情况是关于this虚拟机的,所以没什么太大用处。

File对象可获得硬盘情况。getFreeSpace==getUsableSpace==可用空间,getTotalSpace是总空间。

java 如何获取系统运行时间

有两种方法:

方法一:用java.util.Date类来实现,并结合java.text.DateFormat类来实现时间的格式化,看下面代码:

mport java.util.*; 

import java.text.*;

//以下默认时间日期显示方式都是汉语语言方式

//一般语言就默认汉语就可以了,时间日期的格式默认为MEDIUM风格,比如:2008-6-16 20:54:53

//以下显示的日期时间都是再Date类的基础上的来的,还可以利用Calendar类来实现见类TestDate2.java

public class TestDate { 

  public static void main(String[] args) { 

     Date now = new Date(); 

DateFormat d1 = DateFormat.getDateInstance(); //默认语言(汉语)下的默认风格(MEDIUM风格,比如:2008-6-16 20:54:53)

     String str1 = d1.format(now);

     DateFormat d2 = DateFormat.getDateTimeInstance(); 

     String str2 = d2.format(now); 

     DateFormat d3 = DateFormat.getTimeInstance(); 

     String str3 = d3.format(now); 

     DateFormat d4 = DateFormat.getInstance(); //使用SHORT风格显示日期和时间

     String str4 = d4.format(now);

     DateFormat d5 = DateFormat.getDateTimeInstance(DateFormat.FULL,DateFormat.FULL); //显示日期,周,时间(精确到秒)

     String str5 = d5.format(now);

     DateFormat d6 = DateFormat.getDateTimeInstance(DateFormat.LONG,DateFormat.LONG); //显示日期。时间(精确到秒)

     String str6 = d6.format(now);

     DateFormat d7 = DateFormat.getDateTimeInstance(DateFormat.SHORT,DateFormat.SHORT); //显示日期,时间(精确到分)

     String str7 = d7.format(now);

     DateFormat d8 = DateFormat.getDateTimeInstance(DateFormat.MEDIUM,DateFormat.MEDIUM); //显示日期,时间(精确到分)

     String str8 = d8.format(now);//与SHORT风格相比,这种方式最好用

 System.out.println("用Date方式显示时间: " + now);//此方法显示的结果和Calendar.getInstance().getTime()一样

     

     

     System.out.println("用DateFormat.getDateInstance()格式化时间后为:" + str1);

     System.out.println("用DateFormat.getDateTimeInstance()格式化时间后为:" + str2);

     System.out.println("用DateFormat.getTimeInstance()格式化时间后为:" + str3);

     System.out.println("用DateFormat.getInstance()格式化时间后为:" + str4);

     

     System.out.println("用DateFormat.getDateTimeInstance(DateFormat.FULL,DateFormat.FULL)格式化时间后为:" + str5);

     System.out.println("用DateFormat.getDateTimeInstance(DateFormat.LONG,DateFormat.LONG)格式化时间后为:" + str6);

     System.out.println("用DateFormat.getDateTimeInstance(DateFormat.SHORT,DateFormat.SHORT)格式化时间后为:" + str7);

     System.out.println("用DateFormat.getDateTimeInstance(DateFormat.MEDIUM,DateFormat.MEDIUM)格式化时间后为:" + str8);

  }

}

运行结果:

用Date方式显示时间: Thu Sep 17 09:39:46 CST 2015

用DateFormat.getDateInstance()格式化时间后为:2015-9-17

用DateFormat.getDateTimeInstance()格式化时间后为:2015-9-17 9:39:46

用DateFormat.getTimeInstance()格式化时间后为:9:39:46

用DateFormat.getInstance()格式化时间后为:15-9-17 上午9:39

用DateFormat.getDateTimeInstance(DateFormat.FULL,DateFormat.FULL)格式化时间后为:2015年9月17日 星期四 上午09时39分46秒 CST

用DateFormat.getDateTimeInstance(DateFormat.LONG,DateFormat.LONG)格式化时间后为:2015年9月17日 上午09时39分46秒

用DateFormat.getDateTimeInstance(DateFormat.SHORT,DateFormat.SHORT)格式化时间后为:15-9-17 上午9:39

用DateFormat.getDateTimeInstance(DateFormat.MEDIUM,DateFormat.MEDIUM)格式化时间后为:2015-9-17 9:39:46

方法二:用java.util.Calendar类来实现,看下面:

import java.util.*; 

import java.text.*;

//以下是利用Calendar类来实现日期时间的,和Date类相比较比较简单

public class TestDate2 { 

  public static void main(String[] args) { 

     

     Calendar ca = Calendar.getInstance();

     int year = ca.get(Calendar.YEAR);//获取年份

     int month=ca.get(Calendar.MONTH);//获取月份 

     int day=ca.get(Calendar.DATE);//获取日

     int minute=ca.get(Calendar.MINUTE);//分 

     int hour=ca.get(Calendar.HOUR);//小时 

     int second=ca.get(Calendar.SECOND);//秒

     int WeekOfYear = ca.get(Calendar.DAY_OF_WEEK); 

     

     

     System.out.println("用Calendar.getInstance().getTime()方式显示时间: " + ca.getTime());

     System.out.println("用Calendar获得日期是:" + year +"年"+ month +"月"+ day + "日");

     

     System.out.println("用Calendar获得时间是:" + hour +"时"+ minute +"分"+ second +"秒");

     System.out.println(WeekOfYear);//显示今天是一周的第几天(我做的这个例子正好是周二,故结果显示2,如果你再周6运行,那么显示6)

     

  }

}

运行结果是:

用Calendar.getInstance().getTime()方式显示时间: Thu Sep 17 09:40:40 CST 2015

用Calendar获得日期是:2015年8月17日

用Calendar获得时间是:9时40分40秒

5

总结:中的来说,方法二是最方便的,方法一显得分笨拙,不过看个人喜欢了。

java怎样获得系统服务状态

java本身做不到,这种东西可以用c语言写吧,然后通过jni调

不过也可以用java执行系统命令

下面是大多数linux启动的服务

chkconfig命令可以帮助你开启还是关闭服务,

service command is usefull

主意,在suse linux下面,没有service命令!

做为RED HAT LINUX的启动来说,如果只是个人使用的话,只需要启用以下服务,如果还在精减的话,只启用Network就可,这是Fedora core 2启动服务.

acpid:提供高级电源管理。

cpuspeed:可以提高系统运行效率。

crond:执行例行性程序。

Iptables: 防火墙。

Kudzu:自动检测硬件的变更。

Network: 激活网络接口。

Random:加快系统的启动。

Readahead和Readahead_early:加快系统的启动。

Syslog:把各类事件写入日志。

acpid 配置文件:/proc/acpi/event

请问Java中获得CPU使用率的办法?windows系统

1、确定当前系统安装的jdk是1.6版本以上

2、windows系统中有获取cpu使用率的可执行文件exe,只要在java中获取该文件的执行路径,通过Java调用即可。

3、获取操作系统可执行文件目录procCmd

4、调用java的Runtime.getRuntime().exec执行cmd应用程序

5、利用java中sleep来计算睡眠前后cpu的忙碌时间与空闲时间,因为sleep不会释放系统资源

6、根据忙碌时间占总时间的比例来计算cpu使用率

示例:

private double getCpuRatioForWindows() {  

        try {  

            String procCmd = System.getenv("windir")  

                    + "//system32//wbem//wmic.exe process get Caption,CommandLine,"  

                    + "KernelModeTime,ReadOperationCount,ThreadCount,UserModeTime,WriteOperationCount";  

            // 取进程信息  

            long[] c0 = readCpu(Runtime.getRuntime().exec(procCmd));  

            Thread.sleep(CPUTIME);  

            long[] c1 = readCpu(Runtime.getRuntime().exec(procCmd));  

            if (c0 != null  c1 != null) {  

                long idletime = c1[0] - c0[0];  

                long busytime = c1[1] - c0[1];  

                return Double.valueOf(  

                        PERCENT * (busytime) / (busytime + idletime))  

                        .doubleValue();  

            } else {  

                return 0.0;  

            }  

        } catch (Exception ex) {  

            ex.printStackTrace();  

            return 0.0;  

        }  

    }

java获取系统运行状态的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于请允许获取系统运行状态、java获取系统运行状态的信息别忘了在本站进行查找喔。

The End

发布于:2022-12-03,除非注明,否则均为首码项目网原创文章,转载请注明出处。