首页 > 后端开发 > 正文

java如何获得系统环境变量和当前程序运行的进程号?

2024-05-30 00:56:49 | 我爱编程网

今天我爱编程网小编整理了java如何获得系统环境变量和当前程序运行的进程号?相关内容,希望能帮助到大家,一起来看下吧。

本文目录一览:

java如何获得系统环境变量和当前程序运行的进程号?

Java获取程序运行的当前工作目录

使用下面这个PathUtil的getProgramPath()就可以获得当前程序运行的目录。

import java.net.URL;

import java.net.URLDecoder;

class PathUtil {

/**

* Get the env of windir, such as "C:\WINDOWS".

*

* @return the env of windir value.

*/

public static String getWindir() {

return System.getenv("windir");

}

/**

* Get file separator, such as "/" on unix.

*

* @return the separator of file.

*/

public static String getFileSeparator() {

return System.getProperty("file.separator");

}

/**

* Get line separator, such as "\n" on unix.

*

* @return the separator of line.

*/

public static String getLineSeparator() {

return System.getProperty("line.separator");

}

/**

* Get programPath

*

* @return programPath

*/

public static String getProgramPath() {

Class cls = PathUtil.class;

ClassLoader loader = cls.getClassLoader();

//

// Get the full name of the class.

//

String clsName = cls.getName() + ".class";

//

// Get the package that include the class.

//

Package pack = cls.getPackage();

String path = "";

//

// Transform package name to path.

//

if (pack != null) {

String packName = pack.getName();

//

// Get the class's file name.

//

clsName = clsName.substring(packName.length() + 1);

//

// If package is simple transform package name to path directly,

// else transform package name to path by package name's

// constituent.

//

path = packName;

if (path.indexOf(".") > 0) {

path = path.replace(".", "/");

}

path = path + "/";

}

URL url = loader.getResource(path + clsName);

//

// Get path information form the instance of URL.

//

String retPath = url.getPath();

//

// Delete protocol name "file:" form path information.

//

try {

int pos = retPath.indexOf("file:");

if (pos > -1) {

retPath = retPath.substring(pos + 5);

}

//

// Delete the information of class file from the information of

// path.

//

pos = retPath.indexOf(path + clsName);

retPath = retPath.substring(0, pos - 1);

//

// If the class file was packageed into JAR e.g. file, delete the

// file name of the corresponding JAR e.g..

//

if (retPath.endsWith("!")) {

retPath = retPath.substring(0, retPath.lastIndexOf("/"));

}

retPath = URLDecoder.decode(retPath, "utf-8");

} catch (Exception e) {

retPath = null;

e.printStackTrace();

}

return retPath;

}

}

测试类:

public class Test{

public static void main(String args[]){

String s = PathUtil.getProgramPath();

System.out.println(s);

}

}

java如何获得系统环境变量和当前程序运行的进程号?

查看java进程java进程linux

linux命令里的jps是什么意思?

jps(JavaVirtualMachineProcessStatusTool)是JDK1.5提供的一个显示当前所有java进程pid的命令,简单实用,非常适合在linux/unix平台上简单察看当前java进程的一些简单情况。

Java工程师需要掌握Linux的哪些方面?

基本操作,会部署程序,查看进程,内存,硬盘,会安装jdk,tomcat!基本命令例如:cp,mv,ls,ps,top,df,cd,tail,ping,ifconfig,scp,rm,chmod,chown,yum,apt-get,telnet,ftp,kill,grep等!尤其要求熟悉vim.可以熟悉掌握centos,Ubuntu!,如果想做简单维护工作的话,可以学习shell编程!

JAVA基础:java如何判断某个进程是否在启用?

这个实现起来非常简单,只需要遍历当前的进程列表,如果包含有我们查询的某个进程关键字,则说明这个进程正在启用或运行,否则,没有运行,下面我简单介绍一下实现过程,感兴趣的朋友可以尝试一下,Windows环境和Linux环境基本类似:

Windows

Java测试代码如下,基本思想先运行Tasklist命令,获取当前系统运行的所有进程,然后循环读取每个进程,与我们所要搜索的进程关键字进行匹配,如果包含有进程关键字(indexOf函数的返回值就不会是-1),则说明这个进程正在运行,否则,进程没有运行:

程序运行截图如下,这里我对进程名进行了红色标记,如下,可以看出,当前搜索的chrome进程正在运行:我爱编程网

Linux

这里的Java代码基本和前面的Windows平台类似,唯一的区别就是运行“ps-aux”命令获取当前系统的所有进程,然后循环遍历进程列表,如果匹配到进程的关键字,则说明搜索的进程正在运行,否则,没有运行:

程序运行截图如下,这里我也用颜色对进程名进行了标记,如下,可以看出,当前的vsftpd进程正在运行:

至此,我们就完成了利用Java来判断某个进程是否正在启用或运行。总的来说,整个过程非常简单,代码也非常容易理解,只要你有一定的Java基础,熟悉一下上面的代码,很快就能掌握的,网上也有相关教程和资料,介绍的非常详细,感兴趣的话,可以搜一下,希望以上分享的内容能对你有所帮助吧,也欢迎大家评论、留言进行补充。

java如何获得系统环境变量和当前程序运行的进程号?

java如何获得系统环境变量和当前程序运行的进程号?

import java.util.*;

import java.io.*;

class SysProb

{

//返回当前系统变量的函数,结果放在一个Properties里边,这里只针对win2k以上的,其它系统可以自己改进

public Properties getEnv() throws Exception

{

Properties prop=new Properties();

String OS = System.getProperty("os.name").toLowerCase();

Process p=null;

if(OS.indexOf("windows")>-1)

{

p=Runtime.getRuntime().exec("cmd /c set"); //其它的操作系统可以自行处理, 我这里是win2k

}

BufferedReader br=new BufferedReader(new InputStreamReader(p.getInputStream()));

String line;

while((line=br.readLine())!=null)

{

int i=line.indexOf("=");

if(i>-1)

{

String key=line.substring(0,i);

String value=line.substring(i+1);

prop.setProperty(key,value);

}

}

return prop;

}

//具体用法

public static void main(String[] args)

{

try

{

SysProb sp=new SysProb();

Properties p=sp.getEnv();

System.out.println(p.getProperty("Path")); //注意大小写,如果写成path就不对了

}

catch(Exception e)

{

System.out.println(e);

}

}

}

其他类型:

%COMPUTERNAME% 返回计算机的名称。

%COMSPEC% 返回命令行解释器可执行程序的准确路径。 %WINDIR% 返回操作系统目录的位置。

%OS% 返回操作系统的名称。Windows 2000 将操作系统显示为 Windows_NT。 %PATH% 指定可执行文件的搜索路径。

%USERDOMAIN% 返回包含用户帐户的域的名称。 %USERNAME% 返回当前登录的用户的名称。

以上就是java如何获得系统环境变量和当前程序运行的进程号?全部内容,更多相关信息,敬请关注我爱编程网。

免责声明:文章内容来自网络,如有侵权请及时联系删除。
与“java如何获得系统环境变量和当前程序运行的进程号?”相关推荐