Hello everyone,
I am having some trouble with a program I'm writing and I can't figure out for the life of me what is going wrong. I'll post the code below but first I will point out the problems I'm having. (Both problems are highlighted in my source code with comments.)

Problem #1: string variable and substring
I have a variable called "application_name" that tries to substring out the name of the executable file without the path or extension. For some reason, the code is substringing out the path but not the extension. I have no idea why this is as it is clearly supposed to substring out the text between the last '\' or '/' and the last '.'.

Problem #2: system() with path that has spaces
I am using system() to run a program who's absolute path contains spacing. To counter this I have enclosed the paths in double quotes '\"' but it seems to have no effect. I cout'd the pathing information and all the paths are wrapped in quotes but I'm still getting the 'c:\program' error where it should be reading 'c:\program files\etc..'.

I'm compiling using Visual Studio 2010 Professional, could this be the source of my problem? Any help is much appreciated.

Now for the complete source:

#include <iostream>
#include <string>
#include <sys/stat.h>
using namespace std;

//-------------------------------------------------------------------
// Function Declarations
//-------------------------------------------------------------------
namespace JavaLaunch
{
	bool file_exists(string filef)
	{
		struct stat fileStats; // file information gathered from stat()
		return (stat(filef.c_str(), &fileStats) == 0) ? true : false;
	}

	void pause(void)
	{
		cout << "\n\n";
		system("pause");
	}
}


//-------------------------------------------------------------------
// Main Program
//-------------------------------------------------------------------
int main(int argc, const char *argv[])
{
	// initialize variables
	string application_name = ""; // the name of the application without any extensions
	string application_path = ""; // the absolute path of the directory containing this application
	string application_type = ""; // the type of Java application (either .jar or .class)
	string java_runtime_path = ""; // the absolute path of java.exe

	// get aboslute path for this application
	application_path = argv[0];

	// set application name and application path
	if (application_path.find_last_of("/\\") != application_path.npos)
	{
		//-----------------------------------------------------------
		// Problem #1: string and substring
		application_name = application_path.substr(application_path.find_last_of("/\\") + 1, 
			application_path.find_last_of('.'));

		// DEBUG: application name should not include the file extension !?!?
		cout << application_name << endl;
		// END Problem #1
		//-----------------------------------------------------------

		application_path = application_path.substr(0, application_path.find_last_of("/\\") + 1);
	}

	// make sure a Java application with the same name as this one exists as a .jar or .class
	// file within the application directory
	if (JavaLaunch::file_exists(application_path + application_name + ".class"))
		application_type = ".class";
	else if (JavaLaunch::file_exists(application_path + application_name + ".jar"))
		application_type = ".jar";
	else
	{
		cout << "JAVALAUNCH ERROR: Java application does not exist within this directory!\n"
			<< "Valid application names are: " << application_name << ".jar or " << application_name 
			<< ".class\n" << "Java application must be located at: " << application_path << endl;
		JavaLaunch::pause();
		return 0;
	}

	// check program files and and program files (x86) for JRE [only jre5 and jre6 supported]
	if (JavaLaunch::file_exists("c:\\program files\\java\\jre6\\bin\\java.exe"))
		java_runtime_path = "c:\\program files\\java\\jre6\\bin\\java.exe";
	else if (JavaLaunch::file_exists("c:\\program files (x86)\\java\\jre6\\bin\\java.exe"))
		java_runtime_path = "c:\\program files (x86)\\java\\jre6\\bin\\java.exe";
	else if (JavaLaunch::file_exists("c:\\program files\\java\\jre5\\bin\\java.exe"))
		java_runtime_path = "c:\\program files\\java\\jre5\\bin\\java.exe";
	else if (JavaLaunch::file_exists("c:\\program files (x86)\\java\\jre5\\bin\\java.exe"))
		java_runtime_path = "c:\\program files (x86)\\java\\jre5\\bin\\java.exe";
	else
	{
		cout << "JAVALAUNCH ERROR: The Java Runtime Environment doesn't appear to be installed!" << endl;
		JavaLaunch::pause();
		return 0;
	}

	// launch the Java application
	if (application_type == ".jar")
	{
		string cmd = "\"" + java_runtime_path + "\" -jar \"" + application_path + application_name
			+ ".jar\"";
		cout << cmd << endl;
		system(cmd.c_str());
	}
	else
	{
		//-----------------------------------------------------------
		// Problem #2: system and path spacing
		string cmd = "\"" + java_runtime_path + "\" \"" + application_name + "\"";

		// DEBUG: both paths are wrapped in double quotes, why am i still getting spacing errors!!! grrrr
		cout << cmd << endl;
		system(cmd.c_str());
		// END Problem #2
		//-----------------------------------------------------------
	}

	// exit
	JavaLaunch::pause();
	return 0;
}

I'm sure its something simple I'm missing it usually always is >.<;

Thanks in advance for the help,
- Kyle

I've had similar issues.

I've found the easiest way to overcome all this was to write the command to a .bat file then execute the bat file.

Okay, so I just woke up and took a good look at my first problem. I can't believe I missed this last night probably because I was so tired. Anyhow, I was trying to use the second argument as the end index when it is actually the count. The below code change fixes this problem:

application_name = application_path.substr(application_path.find_last_of("/\\") + 1, 
			application_path.find_last_of('.') - application_path.find_last_of("/\\") - 1);

I'll try using your idea to fix my second issue. Thanks a bunch for the fast reply, I'll mark this as solved once I get it working.

Thanks,
- Kyle

Still haven't gotten it to work using the batch idea, because I still am using system to execute the batch file. Here is my change:

// launch the Java application
	ofstream ofs(string(application_name + ".bat").c_str());
	if (application_type == ".jar")
	{
		ofs << string("\"" + java_runtime_path + "\" -jar \"" + application_path + application_name
			+ ".jar\"").c_str();
		system(string("\"" + application_path + application_name + ".bat\"").c_str());
		ofs.close();
	}
	else
	{
		ofs << string("\"" + java_runtime_path + "\" \"" + application_name + "\"").c_str();
		system(string("\"" + application_path + application_name + ".bat\"").c_str());
		ofs.close();
	}

Edited 6 Years Ago by Vexx: n/a

Well, looks like this is a lost cause, I've been working on it all day and can't find a fix for the quotes problem. I'm just going to throw in the white flag and rewrite this in C#. Marking thread as solved.

This question has already been answered. Start a new discussion instead.