0

I am pretty new to Kotlin, i am trying to make a project in Kotlin that executes code taken by the user. I have used exec() in python to execute code but I don't know how to use that either, please help me if you know python and Kotlin please help Any help will be very much appreciated!! Thanks

EDIT:here is some more info.. i am a newbie in Kotlin i need help with some commands which are like exec in python i am making a program to run python commands in a Kotlin program this is experimental.. forgive me as i am a newbie to Stackoverflow.. thanks..

CodeMaster
  • 35
  • 1
  • 8
  • 3
    Please provide an example of the code that you tried. – Leo Arad Jun 01 '20 at 08:13
  • If you are writing anything other then a development tool running user code might raise serious questions about seurity. I hope you have considered that. – Klaus D. Jun 01 '20 at 08:16
  • Klaus D. I have prepared a few checking barriers using if statements which will raise a error if it is not the function i expected.. i have included a guide to ... P.S i am creating a python interpreter/compiler type thing in a python interpreter – CodeMaster Jun 01 '20 at 08:32
  • Leo, i have not yet started my code i have made the flow chart logic for my program till now... I needed a code for executing code from the user in Kotlin, python, or any other language i am ok with c languages too – CodeMaster Jun 01 '20 at 08:35
  • You may want to execute kotlin scripts rather than kotlin files https://kotlinexpertise.com/run-kotlin-scripts-from-kotlin-programs/ or https://kotlinlang.org/docs/tutorials/quick-run.html or https://stackoverflow.com/questions/34974039/how-can-i-run-kotlin-script-kts-files-from-within-kotlin-java these resources may help you. – Animesh Sahu Jun 01 '20 at 08:39
  • I can do my project in most languages, if you know then help – CodeMaster Jun 01 '20 at 08:39
  • Here is the exact thing with standard library might be faster https://kotlinlang.org/docs/reference/whatsnew11.html#javaxscript-support here is examples of using jsr223 from standard library https://github.com/energister/kotlin-jsr223-example – Animesh Sahu Jun 01 '20 at 08:41
  • is there any direct example, i am complete newbie to Kotlin, but if you can then tell me the answer in python or any c language as i am better with those – CodeMaster Jun 01 '20 at 08:45
  • You can see the first link of my last comment pointing to the kotlin docs, that is straight forward. – Animesh Sahu Jun 01 '20 at 08:46

1 Answers1

0

I have an idea You can get user inputs and save them on a file. Then execute the saved file on the shell by importing 'os' library

import os

print("Enter any python commands!")
print("Enter 'run' to execute.")

user_inputs = ''

# Get user input until user enter 'run' phrase
while True:
    user_input = input('> ')
    if user_input == 'run': break
    user_inputs = user_inputs + "\r\n" + user_input

# save user input on a file
file = open("run.py", 'w')
file.writelines(user_inputs)
file.close()

# execute the code on terminal and get output, then display to the user
stream = os.popen('python run.py')
output = stream.read()

print("---------------------------------")
print(output)

Happy coding

Peyman Majidi
  • 1,777
  • 2
  • 18
  • 31