【问题标题】:access to environment variables in java在java中访问环境变量
【发布时间】:2013-08-01 14:08:58
【问题描述】:

我想在我的应用程序中做一个按钮,让我进入系统中的环境变量列表 像下面的例子进入文件系统

                   b = new Button(dialog, SWT.PUSH);
                 b.setText("Browse file system");
                   b.addSelectionListener(new SelectionAdapter() {
    @Override
    public void widgetSelected(SelectionEvent e) {
    DirectoryDialog fileDialog = new DirectoryDialog(parent);
    fileDialog.setFilterPath(pathText.getText());
    String file = fileDialog.open();
    if (file != null) {
        File workspace = ResourcesPlugin.getWorkspace().getRoot()
            .getLocation().toFile().getAbsoluteFile();

        String workspacePath = workspace.toString();
        if (file.contains(workspacePath))
        file = file.replace(workspacePath, "$WORKSPACE")
            .replace("\\", "/");
        pathText.setText(file);
    }
    super.widgetSelected(e);
    }
});

谁能帮帮我??

【问题讨论】:

    标签: java eclipse environment-variables


    【解决方案1】:

    System.getenv() 给你一个简单的环境变量映射:

    来自Oracle doc

    import java.util.Map;
    
    public class EnvMap {
      public static void main (String[] args) {
        Map<String, String> env = System.getenv();
        for (String envName : env.keySet()) {
            System.out.format("%s=%s%n",
                              envName,
                              env.get(envName));
        }
      }
    }
    

    【讨论】:

    • 感谢您的回复,但我还有一个问题,我尝试使用功能:
    【解决方案2】:

    我怀疑我能否清楚地理解您的问题,但有一个关于使用环境变量的标准 Oracle 文档:

    http://docs.oracle.com/javase/tutorial/essential/environment/env.html

    【讨论】:

      猜你喜欢
      • 2011-04-26
      • 2010-11-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-26
      • 1970-01-01
      • 2010-10-12
      相关资源
      最近更新 更多