如何在Windows上的Java应用程序中设置/更新PATH变量? [英] How can I set/update PATH variable from within java application on Windows?
问题描述
等效于此命令行的内容:
Something equivalent to this command line:
set PATH=%PATH%;C:\Something\bin
要运行我的应用程序,必须在PATH变量中添加一些内容.因此,我想在程序开始时捕获异常,如果程序无法启动,并显示一些向导供用户选择需要放在PATH中的程序的安装文件夹.我将使用该文件夹的绝对路径,并将其添加到PATH变量中,然后再次启动我的应用程序.
To run my application, some thing has to be in a PATH variable. So I want at the program beginning catch exceptions if program fails to start and display some wizard for user to select the installation folder of a program that needs to be in a PATH. The I would took that folder's absolute path and add it to the PATH variable and start my application again.
编辑:
那个东西"是VLC播放器.我需要PATH变量中的安装文件夹(例如:C:\ Program Files \ VideoLAN \ VLC).我的应用程序是单个可执行.jar文件,为了使用它,VLC必须位于PATH中.因此,当用户首次启动我的应用程序时,该小向导将弹出以选择VLC文件夹,然后我将使用它更新PATH.
That "something" is VLC player. I need it's installation folder in PATH variable (for example: C:\Program Files\VideoLAN\VLC). My application is single executable .jar file and in order to use it, VLC needs to be in a PATH. So when the user first starts my app, that little wizard would pop up to select the VLC folder and then I would update PATH with it.
推荐答案
您可以使用Process
对象执行命令,也可以使用BufferedReader
读取命令的输出,这是一个快速的示例,可以帮助您退出:
You can execute commands using the Process
object, you can also read the output of that using a BufferedReader
, here's a quick example that may help you out:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Main {
public static void main(String args[]) {
try {
Process proc = Runtime.getRuntime().exec("cmd set PATH=%PATH%;C:\\Something\\bin");
proc.waitFor();
BufferedReader reader = new BufferedReader(new InputStreamReader(proc.getInputStream()));
String line = reader.readLine();
while (line != null) {
//Handle what you want it to do here
line = reader.readLine();
}
}
catch (IOException e1) {
//Handle your exception here
}
catch(InterruptedException e2) {
//Handle your exception here
}
System.out.println("Path has been changed");
}
}
这篇关于如何在Windows上的Java应用程序中设置/更新PATH变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!