Is there a way to compile/check for errors in python code(stored in a string) in Java program. A lot of people give Jython as the solution. If Jython, what's the procedure
-
What's the context, i.e., what do you want this for?no comment– no comment2021-09-21 19:32:57 +00:00Commented Sep 21, 2021 at 19:32
-
I have an IDE embedded in my application, whenever I type code in the UI, the typed code is sent to the back end in the form of string. I was making use of org.eclipse.jdt.core.dom.CompilationUnit, org.eclipse.jdt.core.compiler.IProblem in order to compile and check for errors in java code(stored in a string) without executing it. Now I want to allow users to enter python code in the IDE ,which will be sent to the backend and I want to check for errors in python code(stored in a string). Please let me know if there is any api to check for errors in python code without executing itYogi– Yogi2021-09-21 19:39:36 +00:00Commented Sep 21, 2021 at 19:39
Add a comment
|
2 Answers
You could do something similar to this:
Python code:
import os
from stat import*
import csv
data = []
with open('Example.csv') as f:
reader = csv.reader(f, delimiter = ',')
for row in reader:
data.append(row)
f = open("Clearprint.csv","w")
f.truncate()
f.close()
with open('Clearprint.csv','w',newline='') as fp:
a = csv.writer(fp,delimiter = ',')
a.writerows(data)
Java code:
class test1{
public static void main(String[] args) throws IOException {
String pythonScriptPath = "your .py file path";
String[] cmd = new String[2];
cmd[0] = "your python.exe path";
cmd[1] = pythonScriptPath;
Runtime rt = Runtime.getRuntime();
Process pr = rt.exec(cmd);
BufferedReader err = new BufferedReader(new InputStreamReader(pr.getErrorStream()));
String line = "";
while((line = err.readLine()) != null) {
System.err.println(line);
}
}
}
2 Comments
Yogi
Is there any api in java to check for errors in python code without actually executing the python code
Alvaro Lopez
As far as im concerned, you can't catch errors without actually executing the code.
I don't have enough rep to comment this, but this question may be what you're looking for Compiling python code using java. A user mentions "Jython" which may be what fits your description.
Just a warning: If you have an internal IDE for people to practice python (I presume) within your Java app, be wary of returned error messages confusing users since python and java aren't identical in their feedback.