Set environment variable in shell script/access in Java program

前端 未结 2 1791
难免孤独
难免孤独 2021-01-16 15:10

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=         


        
相关标签:
2条回答
  • 2021-01-16 15:26

    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

    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?

    0 讨论(0)
  • 2021-01-16 15:46

    What are you trying to do exactly?

    Running JAVA=/home/ubuntu java SystemEnv works fine (i.e. it outputs "/home/ubuntu")

    If you want to export environment variables to the parent process, you have to source it:

    source ./myscript.sh
    . ./myscript.sh # Alternative form
    
    0 讨论(0)
提交回复
热议问题