8

I would be greatfull if you could tell me how one can translate a code from Java to python.
Should one do it manually ? is there any tool to convert it automatically?

nik
  • 2,500
  • 5
  • 21
  • 48
  • 1
    Found a useful tool to achive the conversion of java code to python - https://www.codeusingjava.com/tools/java2py – Batman Rises Dec 21 '22 at 10:12

1 Answers1

16

If you want to translate java code to python you have to translate it manually. Automatic conversion generally does not have the appropriate quality. It looks like there are some tools out e.g. java2python but the author states

The generated Python code is not guaranteed to run, nor is guaranteed to be syntactically valid Python.

Converting a library to another programming language is never an easy task.

If you simply want to use a java library in a application that you want to write in python you could give jython a try.

0 _
  • 10,524
  • 11
  • 77
  • 109
syntonym
  • 7,134
  • 2
  • 32
  • 45
  • is it possible to invoke Java inside python ? my mean I use the Java code by I run it under python ? – nik Jul 05 '16 at 12:37
  • @nik It looks like there are some tools out there that do automatic translation of java to python code like [java2python](https://roundrockriver.wordpress.com/2007/02/15/automated-translation-of-java-to-python/). But (currently) no automatic tool will easily produce good, maintainable, idiomatic code. What exactly is your usecase? – syntonym Jul 05 '16 at 12:41
  • 1
    Why can't you manipulate and understand it in java? – syntonym Jul 05 '16 at 12:44
  • I get your point, so probably the best is to just write the code myself from the begining and not try to translate it because it is very time consuming and sometimes very complicated. however, I liked your answer and I accept it bro. THANKS – nik Jul 05 '16 at 12:45
  • because it is long time i did not use Java which even I don't know how to run that code :-p – nik Jul 05 '16 at 12:46
  • If you understand the algorithms (I certainly do not!) writing python code yourself might be viable. If you just want to play with the code you could try jython, which executes python in a java context (so you can import the library). If you actually need to do some work maybe you will be able to find python libraries (e.g. [here](http://stackoverflow.com/questions/31694167/clustering-algorithm-for-unweighted-graphs)). But in the end sadly there is no easy answer. Java isn't that complex (syntaxwise) so if you get it to build it shouldn't be too hard. Good luck! – syntonym Jul 05 '16 at 12:52
  • would you mind to like my question? – nik Jul 06 '16 at 09:09