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

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

Chloe Bennett

Chloe Bennett

Culture, Media & Entertainment Columnist

Chloe Bennett explores the intersection of pop culture, streaming entertainment, digital trends, and contemporary lifestyle. Her weekly commentary reaches thousands of culture enthusiasts.

Share this article
Twitter Facebook Pinterest