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.getProperty() method. Is there some way to achieve this or using some other approach?

2

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)

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like