在Java程序的shell脚本/访问中设置环境变量 [英] Set environment variable in shell script/access in Java program

查看:1004
本文介绍了在Java程序的shell脚本/访问中设置环境变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Ubuntu 10.04中使用Shell Scrip设置环境,并希望在Java程序中访问.我已经这样写了shell脚本:

I want to set environment using shell scrip in Ubuntu 10.04 and want to access in java program. I have wrote shell script like this:

#! /bin/sh
export JAVA=/home/ubuntu
echo "Variable $JAVA"

我的Java程序是:

import java.util.Map;

public class SystemEnv
{
    public static void main(String[] args)
    {

        Map<String, String> variables = System.getenv();
        for (Map.Entry<String, String> entry : variables.entrySet())
        {
           String name = entry.getKey();
           String value = entry.getValue();
           System.out.println(name + "=" + value);
        }
        System.out.println(System.getenv(("JAVA")));
    }
}

当我在不使用Shell脚本的情况下执行此命令时,效果很好,但是在Shell脚本中却无法运行.

When I execute this command without shell script it works well, but in shell script it does not.

推荐答案

您如何采购脚本?

$./myscript.sh 

$source ./myscript.sh 

第二个将环境变量设置为当前shell. Java程序看起来还可以.

The second will set the environment variable to current shell. The java program looks ok.

基于评论

这是与subshel​​l相关的问题.快速阅读
有什么区别在执行bash脚本和采购bash脚本之间?

It was a problem related to subshell. A quick read is
What is the difference between executing a bash script and sourcing a bash script?

这篇关于在Java程序的shell脚本/访问中设置环境变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆