Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Java in C++?

Status
Not open for further replies.

XerxesTheMighty

Programmer
Joined
Jun 15, 2003
Messages
72
Location
US
Is there any way to input a java code into a c++ program?
I once saw ASM put into c++ (ASM code surrounded by '__asm'), so I was just wondered if c++ could also do java.
 
You can call a Java program from a C++ program, and you can combine C++ and Java in other ways, but you can't embed Java code directly into C++ code.
 
>> You can call a Java program from a C++ program

How do you do that?

-pete
 
The same way you'd call any other program:

Code:
system( "java javaprog.class" );

Or exec* in *nix.

Or the Windows equivalent (CreateProcess?) in Windows.

All assuming you have PATH and CLASSPATH and whatever else set correctly for java to work.


i.e. There's no special, specific support in C++ to call Java programs, just general process creation support that can be used to create a process that happens to be a Java program (or, more precisely, the Java interpreter interpreting a Java program).
 
Also, I suspect you could probably hijack the Java plugin that's provided for browsers and use the Java interpreter from your program. That way you could interpret .class files directly from a C++ program.

Interesting...
 
If you really wanted to you could have your c++ code write out a java.class and then execute it as suggested above. Writing out the code from within c++ will save you from distributing another file.
 
Oh, the term "call" confused me. I normally use that in the context of a "function call". Startig process i ususally refer to with "launch" or "spawn".

-pete
 
Oh, sorry. I'm glad you made me think, though; I'm wondering about using that plugin now...


Anyway, there's no Standard way to call a Java function (er, method) from C++, but on some compiler, if you had a JIT-compiled java class to link in, you might be able to do something like:

Code:
extern "Java" void SomeClass___SomeFunc();

Or maybe something cleaner, like declaring a C++ class to be extern "Java" or having a predefined Java "Globals" class.

The Java method would probably have to be static for this usage to make any sense.


In other words, a compiler could let you call a Java function directly, but no one says it has to.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top