Jump to content
  • Advertisement
Sign in to follow this  
Cacks

[java] Launching a C++ App from my Java App

This topic is 4866 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi guys, I'm wondering does any1 know how I can open a C++ application from my running Java application? I'm wondering is it possible to pass parameters to my C++ app from my Java app. Maybe JNI is best? but i'm just looking for a quick solution at the moment till I get upto speed on JNI. Eg // GameOptions.java launch("game.exe" /800 /600 /Level1); //Proper syntax? // Game.cpp int void main(int argc, char *argv[]) { resolution.x = *argv[1]; resolution.y = *argv[2]; Level *level = new Level(*argv[3]); ... Thanks!

Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
Runtime.getRuntime().exec();

Share this post


Link to post
Share on other sites
Oh, i'm having trouble lol.

I got notepad to load using the method Runtime.getRuntime().exec(). But I have written a hello world C++ program but it will not load.

Any1 no why?

Thanks

Share this post


Link to post
Share on other sites
Quote:
I got notepad to load using the method Runtime.getRuntime().exec(). But I have written a hello world C++ program but it will not load.

Yes, and it has to do with how Runtime.exec() works. Runtime.exec() returns a Process that signifies the command's execution. Each Process contains three streams - input, output, and an error stream. As such, your C++ application's output stream is rerouted to the Process output stream, which means you won't see anything until you read it. A quick example :

The C++ application

#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
cout << "Hello World from a C++ application." << endl;
return EXIT_SUCCESS;
}




The Java application - adapted from here

// java.io classes
import java.io.*;

public class ProcessTest {
public static void main(String args[]) {
try {
String ls_str;
Process ls_proc = Runtime.getRuntime().exec("H:\\Dev-Cpp\\Programs\\CConsDemo.exe");
// get its output (your input) stream
DataInputStream ls_in = new DataInputStream(ls_proc.getInputStream());
try {
while ((ls_str = ls_in.readLine()) != null) {
System.out.println(ls_str);
}
} catch (IOException e) {
System.exit(0);
}
} catch (IOException e1) {
System.err.println(e1);
System.exit(1);
}
}
}




The output
Quote:
Hello World from a C++ application

Share this post


Link to post
Share on other sites
Yeah, thats working great.

The output of the Hello world C++ program is shown in the Java IDE output box.

Created an empty .NET form application an called it using

Runtime.getRuntime().exec("location");

It runs ok.

Why does the form application run? and my cout program not?

Thanks!

Share this post


Link to post
Share on other sites
"As such, your C++ application's output stream is rerouted to the Process output stream, which means you won't see anything until you read it."

Ah, I see, cout - being the output stream is rerouted to the Process output stream.

Thanks!

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!