简体   繁体   中英

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.

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.

您想要的是System.getEnv(...)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM