Pritam Kumar
Pritam Kumar

Reputation: 33

Is there any way I can call my shell script in Java and set values to the shell script variables via Java class

I have a shell script file which has three variables defined in it. I need to set values for those variable in java class, which means, I need to get access of the shell script file in my Java class, then I need to get access of those three variables, and then I need to set values of those three variables from Java. I am not sure how to achieve this requirement. My shell script file is located in windows D:/. Here is my shell script code:

path=`xxx`
echo "$path"

Src
Dest
Env

I need to set the value of Src, Dest and Env

Upvotes: 1

Views: 227

Answers (1)

Andrew McGuinness
Andrew McGuinness

Reputation: 2147

A process inherits a copy of all environment variables from its parent process, which is the process that runs it.

So if you want to set environment variables in a Java program and use them in a shell script, you need the Java program to run the shell script.

That is possible using the java.lang.ProcessBuilder class, but quite unusual. Are you sure it's what you want?

If you need the java program to get the values of the variables, and then use them in the shell script, you could have the java program write the values to a file and then read them from the file in your script, which is likely to be simpler, easier and more portable.

Upvotes: 0

Related Questions