How to Convert Java to Python

By Brad Malloy

"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.

Things You'll Need

  • Java2python
  • Python 2.5 or newer
  • Antlr 2.7

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.

Step 2

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

Step 3

Open a command prompt and navigate to "C:\java2python\" before typing in "python 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.

Step 4

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

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.

Tips & Warnings

  • For more information, open the README file in the java2python folder with Notepad.
  • Be sure to re-read the generated Python code. Not doing so may result in the program not functioning as intended.