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"
and my java program is :
import java.util.Map;
public class SystemEnv
{
public static void main(String[] args)
{
Map variables = System.getenv();
for (Map.Entry entry : variables.entrySet())
{
String name = entry.getKey();
String value = entry.getValue();
System.out.println(name + "=" + value);
}
System.out.println(System.getenv(("JAVA")));
}
}
When I execute this command without shell script it works well, but in shell script it does not.
解决方案
How are you sourcing the script?
$./myscript.sh
or
$source ./myscript.sh
The second will set the environment variable to current shell. The java program looks ok.
EDIT: based on the comment