今日搬砖?时需要获取系统运行时间、版本号等相关信息,使用Java自带的类进行获取系统运行的相关信息,在这整理记录分享一下,感兴趣的小伙伴可以自己尝试尝试。
首先获取jvm相关信息,包含jvm的名称、版本号、启动时间、运行时间、环境变量、进程id等等
public class Test {
public static void main(String[] args) {
RuntimeMXBean runtimeMXBean = ManagementFactory.getRuntimeMXBean();
// jvmName
System.out.printf("jvmName: %s %n", runtimeMXBean.getVmName());
// jvmVersion
System.out.printf("jvmVersion: %s %n", runtimeMXBean.getVmVersion());
// jvmVendor
System.out.printf("jvmVendor: %s %n", runtimeMXBean.getVmVendor());
// startTime 使用hutool中DateUtil进行转换
long startTime = runtimeMXBean.getStartTime();
System.out.printf("startTime: %s %n", DateUtil.date(startTime).toString());
// updateTime
long uptime = runtimeMXBean.getUptime();
System.out.printf("updateTime: %s %n", DateUtil.formatBetween(uptime, BetweenFormater.Level.SECOND));
// classPath
System.out.printf("classPath: %s %n", runtimeMXBean.getClassPath());
// systemProperties
System.out.printf("jvmName: %s %n", runtimeMXBean.getSystemProperties());
// bootClassPath
System.out.printf("bootClassPath: %s %n", runtimeMXBean.getBootClassPath());
// processId
System.out.printf("processId: %s %n", runtimeMXBean.getName().split("@")[0]);
}
}
还可以获取JVM内存相关信息,例如堆内存。
public class Test {
public static void main(String[] args) {
MemoryMXBean memoryMXBean = ManagementFactory.getMemoryMXBean();
MemoryUsage heapMemoryUsage = memoryMXBean.getHeapMemoryUsage();
// heapMemoryUsed
System.out.printf("heapMemoryUsed: %d MB %n", heapMemoryUsage.getUsed()/1024/1024);
// heapMemoryMax
System.out.printf("heapMemoryMax: %d MB %n", heapMemoryUsage.getMax()/1024/1024);
// heapMemoryCommitted
System.out.printf("heapMemoryCommitted: %d MB %n", heapMemoryUsage.getCommitted()/1024/1024);
MemoryUsage nonHeapMemoryUsage = memoryMXBean.getNonHeapMemoryUsage();
// nonHeapMemoryUsed
System.out.printf("nonHeapMemoryUsed: %d MB %n", nonHeapMemoryUsage.getUsed()/1024/1024);
// nonHeapMemoryMax
System.out.printf("nonHeapMemoryMax: %d MB %n", nonHeapMemoryUsage.getMax()/1024/1024);
// nonHeapMemoryCommitted
System.out.printf("nonHeapMemoryCommitted: %d MB %n", nonHeapMemoryUsage.getCommitted()/1024/1024);
}
}
获取JDK相关信息。包含jdk的版本、安装路径、当前运行jar包路径、运行jar文件名等。
public class Test {
public static void main(String[] args) {
// jdkVersion
System.out.printf("jdkVersion: %s %n", System.getProperty("java.version"));
// java_home
System.out.printf("java_home: %s %n", System.getProperty("java.home"));
// jar包路径
String path = System.getProperty("java.class.path");
int firstIndex = path.lastIndexOf(System.getProperty("path.separator")) + 1;
int lastIndex = path.lastIndexOf(File.separator) + 1;
String jarPath = path.substring(firstIndex, lastIndex);
System.out.printf("jarPath: %s %n", jarPath);
// 当前运行jar文件名
String jarName = path.substring(lastIndex);
System.out.printf("jarName: %s %n", jarName);
}
}
获取java虚拟机线程信息,包含线程的阻塞时间、次数、线程的堆栈信息等等。
public class Test {
public static void main(String[] args) {
ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean();
// ThreadCount
System.out.printf("ThreadCount: %d %n", threadMXBean.getThreadCount());
// AllThreadIds
System.out.printf("AllThreadIds: %s %n", threadMXBean.getAllThreadIds());
// TotalStartedThread
System.out.printf("TotalStartedThread: %d %n", threadMXBean.getTotalStartedThreadCount());
// DaemonThread
System.out.printf("DaemonThread: %d %n", threadMXBean.getDaemonThreadCount());
// PeakThread
System.out.printf("PeakThread: %d %n", threadMXBean.getPeakThreadCount());
// ThreadInfo
System.out.printf("ThreadInfo: %s %n", threadMXBean.getThreadInfo(threadMXBean.getAllThreadIds()));
}
}
获取java虚拟机类加载相关信息。
public class Test {
public static void main(String[] args) {
ClassLoadingMXBean classLoadingMXBean = ManagementFactory.getClassLoadingMXBean();
// LoadedClassCount
System.out.printf("LoadedClassCount: %d %n", classLoadingMXBean.getLoadedClassCount());
// TotalLoadedClassCount
System.out.printf("TotalLoadedClassCount: %d %n", classLoadingMXBean.getTotalLoadedClassCount());
// UnloadedClassCount
System.out.printf("UnloadedClassCount: %d %n", classLoadingMXBean.getUnloadedClassCount());
}
}
获取操作系统以及主机硬件信息,包含系统名称、版本、物理内存、可用内存等等。
public class Test {
public static void main(String[] args) {
// 系统版本
OperatingSystemMXBean operatingSystemMXBean = (OperatingSystemMXBean) ManagementFactoryHelper.getOperatingSystemMXBean();
// osName
System.out.printf("osName: %s %n", operatingSystemMXBean.getName());
// Arch
System.out.printf("Arch: %s %n", operatingSystemMXBean.getArch());
// Version
System.out.printf("Version: %s %n", operatingSystemMXBean.getVersion());
// 物理内存
long totalPhysicalMemorySize = operatingSystemMXBean.getTotalPhysicalMemorySize()/1024/1024/1024;
// totalPhysicalMemorySize
System.out.printf("totalPhysicalMemorySize: %d GB %n", totalPhysicalMemorySize);
}
}
原文链接:https://monkey.blog.xpyvip.top/archives/java-huo-qu-jvm-he-cao-zuo-xi-tong-xiang-guan-xin-xi
大约一年前,我决定确保每个包含非唯一文本的Flash通知都将从模块中的方法中获取文本。我这样做的最初原因是为了避免一遍又一遍地输入相同的字符串。如果我想更改措辞,我可以在一个地方轻松完成,而且一遍又一遍地重复同一件事而出现拼写错误的可能性也会降低。我最终得到的是这样的:moduleMessagesdefformat_error_messages(errors)errors.map{|attribute,message|"Error:#{attribute.to_s.titleize}#{message}."}enddeferror_message_could_not_find(obje
我主要使用Ruby来执行此操作,但到目前为止我的攻击计划如下:使用gemsrdf、rdf-rdfa和rdf-microdata或mida来解析给定任何URI的数据。我认为最好映射到像schema.org这样的统一模式,例如使用这个yaml文件,它试图描述数据词汇表和opengraph到schema.org之间的转换:#SchemaXtoschema.orgconversion#data-vocabularyDV:name:namestreet-address:streetAddressregion:addressRegionlocality:addressLocalityphoto:i
我真的很习惯使用Ruby编写以下代码:my_hash={}my_hash['test']=1Java中对应的数据结构是什么? 最佳答案 HashMapmap=newHashMap();map.put("test",1);我假设? 关于java-等价于Java中的RubyHash,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/22737685/
我正在尝试从Postgresql表(table1)中获取数据,该表由另一个相关表(property)的字段(table2)过滤。在纯SQL中,我会这样编写查询:SELECT*FROMtable1JOINtable2USING(table2_id)WHEREtable2.propertyLIKE'query%'这工作正常:scope:my_scope,->(query){includes(:table2).where("table2.property":query)}但我真正需要的是使用LIKE运算符进行过滤,而不是严格相等。然而,这是行不通的:scope:my_scope,->(que
有没有办法在这个简单的get方法中添加超时选项?我正在使用法拉第3.3。Faraday.get(url)四处寻找,我只能先发起连接后应用超时选项,然后应用超时选项。或者有什么简单的方法?这就是我现在正在做的:conn=Faraday.newresponse=conn.getdo|req|req.urlurlreq.options.timeout=2#2secondsend 最佳答案 试试这个:conn=Faraday.newdo|conn|conn.options.timeout=20endresponse=conn.get(url
我有一个存储主机名的Ruby数组server_names。如果我打印出来,它看起来像这样:["hostname.abc.com","hostname2.abc.com","hostname3.abc.com"]相当标准。我想要做的是获取这些服务器的IP(可能将它们存储在另一个变量中)。看起来IPSocket类可以做到这一点,但我不确定如何使用IPSocket类遍历它。如果它只是尝试像这样打印出IP:server_names.eachdo|name|IPSocket::getaddress(name)pnameend它提示我没有提供服务器名称。这是语法问题还是我没有正确使用类?输出:ge
我想获取模块中定义的所有常量的值:moduleLettersA='apple'.freezeB='boy'.freezeendconstants给了我常量的名字:Letters.constants(false)#=>[:A,:B]如何获取它们的值的数组,即["apple","boy"]? 最佳答案 为了做到这一点,请使用mapLetters.constants(false).map&Letters.method(:const_get)这将返回["a","b"]第二种方式:Letters.constants(false).map{|c
我安装了ruby版本管理器,并将RVM安装的ruby实现设置为默认值,这样'哪个ruby'显示'~/.rvm/ruby-1.8.6-p383/bin/ruby'但是当我在emacs中打开inf-ruby缓冲区时,它使用安装在/usr/bin中的ruby。有没有办法让emacs像shell一样尊重ruby的路径?谢谢! 最佳答案 我创建了一个emacs扩展来将rvm集成到emacs中。如果您有兴趣,可以在这里获取:http://github.com/senny/rvm.el
假设我有这个范围:("aaaaa".."zzzzz")如何在不事先/每次生成整个项目的情况下从范围中获取第N个项目? 最佳答案 一种快速简便的方法:("aaaaa".."zzzzz").first(42).last#==>"aaabp"如果出于某种原因你不得不一遍又一遍地这样做,或者如果你需要避免为前N个元素构建中间数组,你可以这样写:moduleEnumerabledefskip(n)returnto_enum:skip,nunlessblock_given?each_with_indexdo|item,index|yieldit
我正在尝试使用boilerpipe来自JRuby。我看过guide从JRuby调用Java,并成功地将它与另一个Java包一起使用,但无法弄清楚为什么同样的东西不能用于boilerpipe。我正在尝试基本上从JRuby中执行与此Java等效的操作:URLurl=newURL("http://www.example.com/some-location/index.html");Stringtext=ArticleExtractor.INSTANCE.getText(url);在JRuby中试过这个:require'java'url=java.net.URL.new("http://www