简体   繁体   English

如何在java中获取Windows进程的工作目录?

[英]How do i get the working directory of a windows process in java?

I would like to get the path to the working directory of a specific process (for example for the PID of the process). 我想获取特定进程的工作目录的路径(例如,进程的PID)。 I am Not Talking about the working or current Directory of the process where my Java Code is running. 我不是在谈论我的Java代码运行过程的工作目录或当前目录。 Its a simple task with Linux, but for Windows i cant find a proper solution. 它是一个简单的Linux任务,但对于Windows我找不到合适的解决方案。 Furthermore, it would be nice, if its a Command or a Framework for Java, because i will need the path in my Code. 此外,如果它是一个Command或Java框架,那将是很好的,因为我将需要我的代码中的路径。 I am not looking for the path to the executable, also Not for a solution with wmic or process explorer. 我不是在寻找可执行文件的路径,也不是寻找使用wmic或process explorer的解决方案。 Already thanks for the help. 已经感谢您的帮助。

I already tried commands like tlist and wmic, but those solutions cant be utilize in my code. 我已经尝试过像tlist和wmic这样的命令,但这些解决方案无法在我的代码中使用。 I am looking for a solution that i can use without special installations on Windows. 我正在寻找一种解决方案,我无需在Windows上进行特殊安装即可使用。

JNI and JNA provide means to call directly into native libraries from Java code, and it is feasible to use these to call out to Windows libraries. JNI和JNA提供了从Java代码直接调用本机库的方法,使用它们调用Windows库是可行的。

There is a github project that appears to be close to the need: https://github.com/kohsuke/winp . 有一个github项目似乎接近需要: https//github.com/kohsuke/winp Perhaps you can add the needed code and send up a pull request, or fork the project. 也许您可以添加所需的代码并发送拉取请求,或者分叉项目。

Note that any solution here is going to be windows-specific, meaning the application using it will not run on another platform. 请注意,此处的任何解决方案都将是特定于Windows的,这意味着使用它的应用程序将无法在其他平台上运行。 Given the nature of the question, that doesn't sound like it would ever be a concern. 鉴于问题的性质,这听起来并不像是一个问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM