May 23, 2012 at 2:10am UTC
So I am trying to run a Java application by running a program I am writing in C++. Everything seems like it should be working fine, however the commands for compiling and running the java do not seem to be registering at all. Here is the complete code for the program.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
#include <cstdlib>
#include <iostream>
#include <windows.h>
using namespace std;
string ExePath()
{
char buffer[MAX_PATH];
GetModuleFileName( NULL, buffer, MAX_PATH );
string::size_type pos = string( buffer ).find_last_of( "\\/" );
return string( buffer ).substr( 0, pos);
}
int main(int argc, char *argv[])
{
string compilerStuff = ExePath() + "\\Main.java" ; //this line is not needed
cout << compilerStuff; //neither is this one
system("pause" ); //or this one
string compileFile = "javac " + ExePath() + "\\Main.java" ;
string runFile = "java " + ExePath() + "\\Main" ;
char compileStuff[compileFile.size()];
char runStuff[runFile.size()];
for (int i=0; i < compileFile.size();i++)
{
compileStuff[i] = compileFile[i];
}
for (int n=0; n < runFile.size();n++)
{
runStuff[n] = runFile[n];
}
cout << compileStuff;
system("pause" );
cout << runStuff;
system("pause" );
system("cd C:\\Program Files\\Java\\jdk1.7.0_01\\bin" );
system("pause" );
system(compileStuff);
system("pause" );
system(runStuff);
system("pause" );
return EXIT_SUCCESS;
}
Last edited on May 23, 2012 at 2:13am UTC
May 23, 2012 at 2:21am UTC
string::c_str() returns a const char *
of the content of the string.
¿Why don't just make a batch script?
May 23, 2012 at 2:45am UTC
Just a project to screw around with. Also, I've only been writing C++ for about 2 days, so could I get an explanation on why "string::c_str() returns a const char* of the content of the string" is important here? Just wondering because everywhere I have a "cout" it displays properly, it just doesn't seem to work when I put it in "system()"
Last edited on May 23, 2012 at 2:45am UTC
May 23, 2012 at 3:40am UTC
That, sir, is very clever of you. Is that what i need to do? Or will that simply shorten my code and have the same result?
Also, here is my new code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
#include <cstdlib>
#include <iostream>
#include <windows.h>
using namespace std;
string ExePath()
{
char buffer[MAX_PATH];
GetModuleFileName( NULL, buffer, MAX_PATH );
string::size_type pos = string( buffer ).find_last_of( "\\/" );
return string( buffer ).substr( 0, pos);
}
int main(int argc, char *argv[])
{
string compilerStuff = ExePath() + "\\Main.java" ;
cout << compilerStuff;
system("pause" );
string compileFile = "javac " + ExePath() + "\\Main.java" ;
string runFile = "java " + ExePath() + "\\Main" ;
system("cd C:\\Program Files\\Java\\jre6" );
system("pause" );
system(compileFile.c_str());
system("pause" );
system(runFile.c_str());
system("pause" );
return EXIT_SUCCESS;
}
Last edited on May 23, 2012 at 6:05pm UTC