void CallCommandLineProcessor(const std::wstring& curDir, const std::wstring& args)
{
    JNIEnv *env;
    JavaVMAttachArgs attachArgs;
    attachArgs.version = JNI_VERSION_1_2;
    attachArgs.name = "WinLauncher external command processing thread";
    attachArgs.group = NULL;
    jvm->AttachCurrentThread((void**)&env, &attachArgs);

    std::string processorClassName = LoadStdString(IDS_COMMAND_LINE_PROCESSOR_CLASS);
    jclass processorClass = env->FindClass(processorClassName.c_str());
    if (processorClass)
    {
        jmethodID processMethodID = env->GetStaticMethodID(processorClass, "processWindowsLauncherCommandLine", "(Ljava/lang/String;Ljava/lang/String;)V");
        if (processMethodID)
        {
            jstring jCurDir = env->NewString((const jchar *)curDir.c_str(), curDir.size());
            jstring jArgs = env->NewString((const jchar *)args.c_str(), args.size());
            env->CallStaticVoidMethod(processorClass, processMethodID, jCurDir, jArgs);
            jthrowable exc = env->ExceptionOccurred();
            if (exc)
            {
                MessageBox(NULL, _T("Error sending command line to existing instance"), _T("Error"), MB_OK);
            }
        }
    }

    jvm->DetachCurrentThread();
}
bool RunMainClass()
{
    std::string mainClassName = LoadStdString(IDS_MAIN_CLASS);
    jclass mainClass = env->FindClass(mainClassName.c_str());
    if (!mainClass)
    {
        char buf[_MAX_PATH + 256];
        sprintf_s(buf, "Could not find main class %s", mainClassName.c_str());
        std::string error = LoadStdString(IDS_ERROR_LAUNCHING_APP);
        MessageBoxA(NULL, buf, error.c_str(), MB_OK);
        return false;
    }

    jmethodID mainMethod = env->GetStaticMethodID(mainClass, "main", "([Ljava/lang/String;)V");
    if (!mainMethod)
    {
        std::string error = LoadStdString(IDS_ERROR_LAUNCHING_APP);
        MessageBoxA(NULL, "Could not find main method", error.c_str(), MB_OK);
        return false;
    }

    jobjectArray args = PrepareCommandLine();
    env->CallStaticVoidMethod(mainClass, mainMethod, args);
    jthrowable exc = env->ExceptionOccurred();
    if (exc)
    {
        std::string error = LoadStdString(IDS_ERROR_LAUNCHING_APP);
        MessageBoxA(NULL, "Error invoking main method", error.c_str(), MB_OK);
    }

    return true;
}
bool LocateJVM()
{
    bool result;
    if (FindJVMInEnvVar(LoadStdString(IDS_JDK_ENV_VAR).c_str(), result))
    {
        return result;
    }

    if (FindJVMInSettings()) return true;

    std::vector<std::string> jrePaths;
    if(need64BitJRE) jrePaths.push_back(GetAdjacentDir("jre64"));
    jrePaths.push_back(GetAdjacentDir("jre"));
    for(std::vector<std::string>::iterator it = jrePaths.begin(); it != jrePaths.end(); ++it) {
        if (FindValidJVM((*it).c_str()) && Is64BitJRE(jvmPath) == need64BitJRE)
        {
            return true;
        }
    }

    if (FindJVMInEnvVar("JAVA_HOME", result))
    {
        return result;
    }

    if (FindJVMInRegistry())
    {
        return true;
    }

    std::string jvmError;
    jvmError = "No JVM installation found. Please install a " BITS_STR " JDK.\n"
               "If you already have a JDK installed, define a JAVA_HOME variable in\n"
               "Computer > System Properties > System Settings > Environment Variables.";

    if (IsWow64())
    {
        // If WoW64, this means we are running a 32-bit program on 64-bit Windows. This may explain
        // why we couldn't locate the JVM.
        jvmError += "\n\nNOTE: We have detected that you are running a 64-bit version of the "
                    "Windows operating system but are running the 32-bit executable. This "
                    "can prevent you from finding a 64-bit installation of Java. Consider running "
                    "the 64-bit version instead, if this is the problem you're encountering.";
    }

    std::string error = LoadStdString(IDS_ERROR_LAUNCHING_APP);
    MessageBoxA(NULL, jvmError.c_str(),  error.c_str(), MB_OK);
    return false;
}
bool LoadJVMLibrary()
{
    std::string dllName(jvmPath);
    std::string binDir = dllName + "\\bin";
    std::string serverDllName = binDir + "\\server\\jvm.dll";
    std::string clientDllName = binDir + "\\client\\jvm.dll";
    if ((bServerJVM && FileExists(serverDllName)) || !FileExists(clientDllName))
    {
        dllName = serverDllName;
    }
    else
    {
        dllName = clientDllName;
    }

    // ensure we can find msvcr100.dll which is located in jre/bin directory; jvm.dll depends on it.
    SetCurrentDirectoryA(binDir.c_str());
    hJVM = LoadLibraryA(dllName.c_str());
    if (hJVM)
    {
        pCreateJavaVM = (JNI_createJavaVM) GetProcAddress(hJVM, "JNI_CreateJavaVM");
    }
    if (!pCreateJavaVM)
    {
        std::string jvmError = "Failed to load JVM DLL ";
        jvmError += dllName.c_str();
        jvmError += "\n"
                    "If you already have a " BITS_STR " JDK installed, define a JAVA_HOME variable in "
                    "Computer > System Properties > System Settings > Environment Variables.";
        std::string error = LoadStdString(IDS_ERROR_LAUNCHING_APP);
        MessageBoxA(NULL, jvmError.c_str(), error.c_str(), MB_OK);
        return false;
    }
    return true;
}
bool CreateJVM()
{
    JavaVMInitArgs initArgs;
    initArgs.version = JNI_VERSION_1_2;
    initArgs.options = vmOptions;
    initArgs.nOptions = vmOptionCount;
    initArgs.ignoreUnrecognized = JNI_FALSE;

    int result = pCreateJavaVM(&jvm, &env, &initArgs);

    for (int i = 0; i < vmOptionCount; i++)
    {
        free(vmOptions[i].optionString);
    }
    free(vmOptions);
    vmOptions = NULL;

    if (result != JNI_OK)
    {
        std::stringstream buf;

        buf << "Failed to create JVM: error code " << result << ".\n";
        buf << "JVM Path: " << jvmPath << "\n";
        buf << "If you already have a " BITS_STR " JDK installed, define a JAVA_HOME variable in \n";
        buf << "Computer > System Properties > System Settings > Environment Variables.";
        std::string error = LoadStdString(IDS_ERROR_LAUNCHING_APP);
        MessageBoxA(NULL, buf.str().c_str(), error.c_str(), MB_OK);
    }

    return result == JNI_OK;
}
Exemplo n.º 6
0
bool LocateJVM()
{
	bool result;
	if (FindJVMInEnvVar(LoadStdString(IDS_JDK_ENV_VAR).c_str(), result))
	{
		return result;
	}

	std::string jreDir = GetAdjacentDir("jre");
	if (FindValidJVM(jreDir.c_str()) && Is64BitJRE(jvmPath) == need64BitJRE)
	{
		return true;
	}

	if (FindJVMInRegistry())
	{
		return true;
	}

	if (FindJVMInEnvVar("JAVA_HOME", result))
	{
		return result;
	}

	MessageBoxA(NULL, "No JVM installation found. Please reinstall the product or install a JDK.", "Error Launching IntelliJ Platform", MB_OK);
	return false;
}
Exemplo n.º 7
0
bool LocateJVM()
{
	bool result;
	if (FindJVMInEnvVar(LoadStdString(IDS_JDK_ENV_VAR).c_str(), result))
	{
		return result;
	}


        std::vector<std::string> jrePaths;
        if(need64BitJRE) jrePaths.push_back(GetAdjacentDir("jre64"));
        jrePaths.push_back(GetAdjacentDir("jre"));
        for(std::vector<std::string>::iterator it = jrePaths.begin(); it != jrePaths.end(); ++it) {
          if (FindValidJVM((*it).c_str()) && Is64BitJRE(jvmPath) == need64BitJRE)
          {
                  return true;
          }
        }

	if (FindJVMInRegistry())
	{
		return true;
	}

	if (FindJVMInEnvVar("JAVA_HOME", result))
	{
		return result;
	}

	MessageBoxA(NULL, "No JVM installation found. Please reinstall the product or install a JDK.", "Error Launching IntelliJ Platform", MB_OK);
	return false;
}
Exemplo n.º 8
0
void AddPredefinedVMOptions(std::vector<std::string>& vmOptionLines)
{
  char propertiesFile[_MAX_PATH];
  if (GetEnvironmentVariableA(LoadStdString(IDS_PROPS_ENV_VAR).c_str(), propertiesFile, _MAX_PATH))
  {
    vmOptionLines.push_back(std::string("-Didea.properties.file=") + propertiesFile);
  }
}
bool FindJVMInRegistryWithVersion(const char* version, bool wow64_32)
{
    const char* keyName = LoadStdString(IDS_JDK_ONLY) == std::string("true")
                          ? "Java Development Kit"
                          : "Java Runtime Environment";

    char buf[_MAX_PATH];
    sprintf_s(buf, "Software\\JavaSoft\\%s\\%s", keyName, version);
    return FindJVMInRegistryKey(buf, wow64_32);
}
Exemplo n.º 10
0
void AddPredefinedVMOptions(std::vector<std::string>& vmOptionLines)
{
	std::string vmOptions = LoadStdString(IDS_VM_OPTIONS);
	while(vmOptions.size() > 0)
	{
		int pos = vmOptions.find(' ');
		if (pos == std::string::npos) pos = vmOptions.size();
		vmOptionLines.push_back(vmOptions.substr(0, pos));
		while(pos < vmOptions.size() && vmOptions[pos] == ' ') pos++;
		vmOptions = vmOptions.substr(pos);
	}
}
Exemplo n.º 11
0
bool LoadVMOptions()
{
    TCHAR buffer[_MAX_PATH];
    TCHAR copy[_MAX_PATH];

    std::vector<std::wstring> files;

    GetModuleFileName(NULL, buffer, _MAX_PATH);
    std::wstring module(buffer);

    if (LoadString(hInst, IDS_VM_OPTIONS_ENV_VAR, buffer, _MAX_PATH))
    {
        if (GetEnvironmentVariableW(buffer, copy, _MAX_PATH)) {
            ExpandEnvironmentStrings(copy, buffer, _MAX_PATH);
            files.push_back(std::wstring(buffer));
        }
    }

    if (LoadString(hInst, IDS_VM_OPTIONS_PATH, buffer, _MAX_PATH))
    {
        ExpandEnvironmentStrings(buffer, copy, _MAX_PATH - 1);
        std::wstring selector(copy);
        files.push_back(selector + module.substr(module.find_last_of('\\')) + L".vmoptions");
    }

    files.push_back(module + L".vmoptions");
    std::wstring used;
    std::vector<std::string> vmOptionLines;

    if (!FindValidVMOptions(files, used, vmOptionLines))
    {
        std::string error = LoadStdString(IDS_ERROR_LAUNCHING_APP);
        MessageBoxA(NULL, "Cannot find VM options file", error.c_str(), MB_OK);
        return false;
    }

    vmOptionLines.push_back(std::string("-Djb.vmOptionsFile=") + EncodeWideACP(used));

    if (!AddClassPathOptions(vmOptionLines)) return false;
    AddPredefinedVMOptions(vmOptionLines);

    vmOptionCount = vmOptionLines.size();
    vmOptions = (JavaVMOption*)malloc(vmOptionCount * sizeof(JavaVMOption));
    for (int i = 0; i < vmOptionLines.size(); i++)
    {
        vmOptions[i].optionString = _strdup(vmOptionLines[i].c_str());
        vmOptions[i].extraInfo = 0;
    }

    return true;
}
Exemplo n.º 12
0
void AddPredefinedVMOptions(std::vector<std::string>& vmOptionLines)
{
    std::string vmOptions = LoadStdString(IDS_VM_OPTIONS);
    while (vmOptions.size() > 0)
    {
        int pos = vmOptions.find(' ');
        if (pos == std::string::npos) pos = vmOptions.size();
        vmOptionLines.push_back(vmOptions.substr(0, pos));
        while (pos < vmOptions.size() && vmOptions[pos] == ' ') pos++;
        vmOptions = vmOptions.substr(pos);
    }

    std::string errorFile = getVMOption(IDS_VM_OPTION_ERRORFILE);
    std::string heapDumpPath = getVMOption(IDS_VM_OPTION_HEAPDUMPPATH);
    if (errorFile != "") vmOptionLines.push_back(errorFile);
    if (heapDumpPath != "") vmOptionLines.push_back(heapDumpPath);

    char propertiesFile[_MAX_PATH];
    if (GetEnvironmentVariableA(LoadStdString(IDS_PROPS_ENV_VAR).c_str(), propertiesFile, _MAX_PATH))
    {
        vmOptionLines.push_back(std::string("-Didea.properties.file=") + propertiesFile);
    }
}
Exemplo n.º 13
0
std::string BuildClassPath()
{
    std::string classpathLibs = LoadStdString(IDS_CLASSPATH_LIBS);
    std::string result = CollectLibJars(classpathLibs);

    std::string toolsJar = FindToolsJar();
    if (toolsJar.size() > 0)
    {
        result += ";";
        result += toolsJar;
    }

    return result;
}
Exemplo n.º 14
0
bool AddClassPathOptions(std::vector<std::string>& vmOptionLines)
{
    std::string classPath = BuildClassPath();
    if (classPath.size() == 0) return false;
    vmOptionLines.push_back(std::string("-Djava.class.path=") + classPath);

    std::string bootClassPathLibs = LoadStdString(IDS_BOOTCLASSPATH_LIBS);
    std::string bootClassPath = CollectLibJars(bootClassPathLibs);
    if (bootClassPath.size() > 0)
    {
        vmOptionLines.push_back(std::string("-Xbootclasspath/a:") + bootClassPath);
    }

    return true;
}
Exemplo n.º 15
0
void AddPredefinedVMOptions(std::vector<std::string>& vmOptionLines)
{
	std::string vmOptions = LoadStdString(IDS_VM_OPTIONS);
	while(vmOptions.size() > 0)
	{
		int pos = vmOptions.find(' ');
		if (pos == std::string::npos) pos = vmOptions.size();
		vmOptionLines.push_back(vmOptions.substr(0, pos));
		while(pos < vmOptions.size() && vmOptions[pos] == ' ') pos++;
		vmOptions = vmOptions.substr(pos);
	}

	char ideaProperties[_MAX_PATH];
	if (GetEnvironmentVariableA("IDEA_PROPERTIES", ideaProperties, _MAX_PATH-1))
	{
		vmOptionLines.push_back(std::string("-Didea.properties.file=") + ideaProperties);
	}
}
Exemplo n.º 16
0
bool FindJVMInRegistryWithVersion(const char* version, bool wow64_32)
{
  char* keyName = "Java Runtime Environment";
  bool foundJava = false;
  char buf[_MAX_PATH];
  //search jre in registry if the product doesn't require tools.jar
  if (LoadStdString(IDS_JDK_ONLY) != std::string("true")) {
    sprintf_s(buf, "Software\\JavaSoft\\%s\\%s", keyName, version);
    foundJava = FindJVMInRegistryKey(buf, wow64_32);
  }

  //search jdk in registry if the product requires tools.jar or jre isn't installed.
  if (!foundJava) {
    keyName = "Java Development Kit";
    sprintf_s(buf, "Software\\JavaSoft\\%s\\%s", keyName, version);
    foundJava = FindJVMInRegistryKey(buf, wow64_32);
  }
  return foundJava;
}
Exemplo n.º 17
0
bool FindJVMInEnvVar(const char* envVarName, bool& result)
{
    char envVarValue[_MAX_PATH];
    if (GetEnvironmentVariableA(envVarName, envVarValue, _MAX_PATH - 1))
    {
        if (FindValidJVM(envVarValue))
        {
            if (Is64BitJRE(jvmPath) != need64BitJRE) return false;
            result = true;
        }
        else
        {
            char buf[_MAX_PATH];
            sprintf_s(buf, "The environment variable %s (with the value of %s) does not point to a valid JVM installation.",
                      envVarName, envVarValue);
            std::string error = LoadStdString(IDS_ERROR_LAUNCHING_APP);
            MessageBoxA(NULL, buf, error.c_str(), MB_OK);
            result = false;
        }
        return true;
    }
    return false;
}
Exemplo n.º 18
0
std::string BuildClassPath()
{
  std::string classpathLibs = LoadStdString(IDS_CLASSPATH_LIBS);
  std::string result = CollectLibJars(classpathLibs);
  return result;
}