-4

I need to run some Python and Java codes from my C++ program. I found out how I can run an EXE file from my program. But I don't know how to run those codes.

I got Microsoft Visual C++, Codeblocks, Dev C++ and Turbo C++. If anyone knows how to do that in any of these compilers then please tell me or tell me the name of compiler in which this can be done.

2
  • You could use the command line to compile the java code and then execute it, and the same for python Commented Jan 21, 2012 at 19:15
  • can u plz explain it how can my c++ program run a java or python program in between its execution.... Commented Jan 22, 2012 at 6:10

3 Answers 3

2

You say you know how to run an exe, so run java.exe or python.exe with the name of your .class or .py file as a parameter.

Sign up to request clarification or add additional context in comments.

4 Comments

do java and python can also make exe files as a c or c++ program makes when it is executed....???? coz i heard from someone that they don;t make exe files coz java and python have their own environment.....
Java and Python normally don't turn into exes, although I know that (at least for Python), there are ways to do so. Although really, it's often more simpler to not do so.
can a c++ program simply run the .class file for java and .py file for python as it runs exe files???.....
@AnkitGupta as Deflect mentions, normally you do not generate .exe files for either of those languages, though there are ways to do this. In my answer, java.exe and python.exe refer to the standard runtimes for those languages. You "run" MyClass.class/my or program.py by running "java MyClass" or "python my_program.py", respectively. (note that you may need to include other parameters to get the paths right.)
1

You can use the system() function.

1 Comment

Well, you should read this article. Use system() to execute interpretator of your code, for example: system("java my_java_prog.jar");
1

You can use Boost Python to embed Python in your C++ program. In this case, I think you need to include the Python code in the C++ code as a string. You can read a Python file into the C++ code as a string, though.

If you are not passing information between C++ and Python, this is overkill, and you could use system as suggested by GamePad64, where you can just call out to the shell/os like system("foo.py param1 param2"), or system("python foo.py param1 param2"). You can find similar questions on SO by searching e.g. "C++ python system", for example Adding python script to c++ project.

2 Comments

if i use this method ....then i will have to write my python code accordingly???? i mean i think i saw on that page that i will have to make some changes in my code...wil i have to do it or i can simply run any code of python.......
@AnkitGupta: See expanded answer.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.