How to Convert Java to Python

Techwalla may earn compensation through affiliate links in this story. Learn more about our affiliate and product review process here.
Converting code by hand from one language to another is difficult.

"Don't reinvent the wheel" is one of the first lessons learned by an aspiring programmer; don't spend time recoding something that has already been done. If the code doesn't match your desired language, however, it may be difficult to convert. Converting code from Java to Python is made possible, however, by a utility that will automatically convert much of Java to Python. By using this application, you can save massive amounts of time using the Java wheel in a Python program.

Advertisement

Step 1

Download and extract java2python. The file you download is a gzip file, and it contains within it a tarball file; both are compression schemes, and both can be decompressed with 7zip, an open-source program.

Video of the Day

Step 2

Place the contents of the java2python folder on the root of your C:\ drive.

Advertisement

Step 3

Open a command prompt and navigate to "C:\java2python" before typing in "python setup.py install" without quotes. This will tell the Python interpreter to run the setup script and prepare your computer. Change directories to "C:\java2python\bin" and keep the window open.

Advertisement

Step 4

Copy the Java file to be converted into your bin subfolder, under java2python. In the command line, run "j2py -i input_file.java -o output_file.py," replacing the input_file and output_file with your filenames.

Advertisement

Step 5

Open the new Python folder and read the code. It probably won't be perfect, so you'll need to go over it to make sure it makes sense from a Python point of view. Even spending time manually checking, however, you will have saved large amounts of time from hand-converting.

Video of the Day

Advertisement

Advertisement

references & resources