-2

I want to create a java program which will compile and execute program written in c++. Ijust wanted to know the links or get any idea how will I do this, I want to learn by-self, but not sure from where i should start. I got a link for executing a ".exe" file, which is the part of my program, but how can I compile a C++ program through Java.

I tried to search the related stuff, but was unable to find...suggestions are appreciated...

4
  • Presumably you don't want to build the compiler itself, but call an existing one? Commented Nov 13, 2015 at 12:08
  • So ... you wand to write a C++ compiler in Java ? Good luck with that! Start with the documentation of clang maybe. Commented Nov 13, 2015 at 12:09
  • ... or you could invoke a C++ compiler from your java program. Ok I get it. Commented Nov 13, 2015 at 12:10
  • #YSC want to invoke a C++ compiler. #Bathsheba, call an existing one... Commented Nov 13, 2015 at 12:14

2 Answers 2

0

I think you want to run a .exe file through Java.

You should try Runtime.getRuntime().exec(String command, String[] envparam, File dir) with:

  • command is the location of the .exe
  • envparam can be null
  • dir is the directory of your .exe

Example:

Runtime.getRuntime().exec("c:\\program files\\test\\test.exe", null, new File("c:\\program files\\test\\"));
Sign up to request clarification or add additional context in comments.

2 Comments

before running a .exe file, i wont to compile a code, with java program, written in C++ to generate a .exe file.
so you should call command to build before run .exe. Example g++ main.cpp -o main
0

Here's how you should use Runtime.exec() to execute a C++ compiler. You might also try its more modern cousin, ProcessBuilder:

Java Runtime.getRuntime().exec() alternatives

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.