utkarsh
utkarsh

Reputation: 221

How to use variable of shell script in java program without passing it as argument

I am calling a java program from shell script. Shell script contains one variable which needs to use in java program.

Shell Script:

for s in 0 1
do
javac -cp $CLASSPATH ./src/*.java
java -cp $CLASSPATH Test
done

Test.java

main() {
sValue = ...; // here I need to get value from shell
someMethod(sValue) {
. ... . 
}
}

shell script has variable s with value 0 and 1. I need to use this variable in java program Test.

In my program I am can't pass this value as argument as "java -cp $CLASSPATH Test $s". So please provide some other solution.

Upvotes: 0

Views: 755

Answers (2)

PeterMmm
PeterMmm

Reputation: 24630

Propably (depends on shell) you will export the s variable to make it visible to the java process:

for s in 0 1
do
export V=$s
javac -cp $CLASSPATH ./src/*.java
java -cp $CLASSPATH Test
done

And then System.getenv("V") in Test.java.

Upvotes: 1

rolfl
rolfl

Reputation: 17707

What you want is System.getEnv(...).

Upvotes: 0

Related Questions