Environment Variables in JAVA (tomcat)

时间:2017-12-18 07:30:25

标签: java linux tomcat tomcat8 environment

I have my @RequestParam(value = "name", required=false) final String name as follows

/etc/environment

I have sourced APP="/opt/apps/" PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games" JAVA_HOME="/usr/lib/jvm/java-8-oracle" on my machine. I am able to get the output when I type source /etc/environment on terminal

But When I call it from inside a java file

echo $APP

I get only the output of PATH but not the other 2 env variables.

OUTPUT

LOGGER.error("APP: " + System.getenv("APP")); LOGGER.error("PATH: " + System.getenv("PATH")); LOGGER.error("JAVA_HOME: " + System.getenv("JAVA_HOME"));

Please correct me what am I doing wrong

2 个答案:

答案 0 :(得分:3)

It sounds like APP and JAVA_HOME are not exported. With unix shells, there is a difference between set (variable is visible in your current shell) and export (variable is visible to all sub-processes). If you just type "X=Y", X it is set, but not exported. PATH is mostly exported somewhere already.

You can test this by running " <?php include('config.php'); $delete_id = $_POST['delete_id']; $update_id = $_POST['update_id']; $select_id = $_POST['select_id']; $sql_delete = "DELETE FROM tbl_cart WHERE id='".$delete_id."'"; $results = mysqli_query($conn, $sql_delete); $cart_value = array($update_id); $cart_value_arr = explode(',',$cart_value[0]); $cart_remove = $delete_id; $cart_remove_arr = array($cart_remove); $arr = array_diff($cart_value_arr,$cart_remove_arr); echo $result_val = implode(',', $arr); $sql_update = "UPDATE tbl_enquiry SET `cart_id`='$result_val' WHERE enquiry_id='".$select_id."'"; $update_result = mysqli_query($conn, $sql_update); if($results == 1){ $result = 1; } else { $result = 0; } ?> " in your console. If APP doesn't show up, the variable will not be visible to java, or any other program you run, even though export | grep APP works just fine.

If this is the case, simply add "export APP" to your .profile, or the script with which you are starting your java application (or to test, simply type that on the console before you start java).

答案 1 :(得分:0)

You shouldn't source your /etc/environment. That is not the way to get variables into your process. The /etc/environment file is not a script, so it makes no sense to source it. It is just a list of settings, a bit like a properties file.

See this for more details.

It is read when you login. Therefore, to make the variables visible, you should logout and back in.