How to Set a System Property from a Python Script to Use It in Java Code?
I Want a String in Java Code Whose Value Is Set in a Python Script. Can I Set It as a System Property from the Script in Order to Use It with System...
I want a String in Java code whose value is set in a python script. Can I set it as a System Property from the script in order to use it with System.getProperty() method. Is there some way to achieve this or using some other approach?
3 Answers
If you are using pyspark you can use the setSystemProperty() method of the SparkContext, e.g.
import pyspark
sc=pyspark.SparkContext()
sc.setSystemProperty("com.amazonaws.services.s3.enableV4", "true")
You can set a Java property on the command line like this:
java -D<propertyName1>=<propertyVal1> -D<propertyName2>=<propertyVal2> ...
The System.getProperty("propertyName1") will return "propertyVal1".
Example:
java -Dfile.encoding=utf-8 -jar MyJar.jar
This will work in a bash script. I've never tried it from Python, but i guess this answer might help you achieve that.
If you are using Jython, you can import java.lang.System class and call the System.setProperty() method to set whatever system properties you want to set:
Code snippets:
from java.lang import System
System.setProperty('app.env', 'DEV')
from yourpackage import YourMainClass
args = ['Your','Args']
YourMainClass.main(args)