Is it possible to compile a .java file in a c++ program (assuming the .java file is given to me)? If so, how?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
It is possible to compile a Java module using C++. In UNIX / Linux, you can have C++ use the
fork()/exec()C functions to launch thejavaccompiler within a separate process. In Windows, you can use the CREATEPROCESS facilities.Other techniques include launching a shell which then calls
javac. This is used at times when you don’t want to do more work to integrate input and output with the “launching” program.You can also leverage the existing JNI (Java Native Interface) to start a JVM in your C / C++ process. Under such a solution, you could then use the new Java 1.6 facilities to grab the tool chain compiler. Once you have the compiler, you can invoke it via JNI calls to compile your source code.
The
javacprogram is open source. Under the right conditions (if you are developing something that is GPL compatible) you can integrate the HotSpot code directly within your program.Finally, if you are only compiling a small subset of Java, you can also write your own compiler. JVM bytecode is quite easy to understand, and the class file format is publicly accessible.