Would someone please be so kind to help out a poor soul?

Hi guys,

I need to start of by apologizing: I don't know the first thing about C++ programming. I have no idea how the language works or how to begin writing a program in it.
however, my request is so extremely basic that I was hoping someone would be so kind to help me out on this one.
I have some experience in Java and .net programming.
I have a Java program that is to be run from an external program. For some reason that is unclear to me, one of these external programs only allows exe files to be run.
I would need an exe file that does nothing else than running my java program ("java -jar myprogram.jar"). I could do this in .net, but that would mean the user should have to install both the JRE and the .net environment.
If I am not mistaking, c++ does not require additional software to run on windows PC's.
Could anyone show me how to write a c++ program that runs my java program? I think it is probably only one line of code.

Thanks in advance.
1
2
3
4
5
6
7
8
9
10
11
#include "stdafx.h"
#include <process.h>
#include <windows.h>
#include <stdio.h>

int main(int argc, char* argv[])
{
	WinExec("java -jar myprogram.jar", SW_HIDE);
        Sleep (INFINITE);
	return 0;
}

Don't be afraid about C++, its simple and easy to coding...
Next time try to do by yourself.
Last edited on
thank you very much, I'll see if I can get it to work!
I would use CreateProcess rather than WinExec.

As MSDN says in the entry for WinExec:
This function is provided only for compatibility with 16-bit Windows. Applications should use the CreateProcess function.

Also, sleeping forever is a bad idea. With CreateProcess you can wait for the app to exit, if you want to.

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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
// run_java.cpp

#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <tchar.h>

#include <iostream>
using namespace std;

#ifdef _UNICODE
#define TCOUT wcout
#else
#define TCOUT cout
#endif

DWORD RunJava(bool bWait)
{
    DWORD dwRet = NOERROR;

    STARTUPINFO startupInfo = {0};
    startupInfo.cb          = sizeof(startupInfo);
    startupInfo.dwFlags     = STARTF_USESHOWWINDOW;
    startupInfo.wShowWindow = SW_HIDE;

    PROCESS_INFORMATION processInfo = {0};

    // the command line variable must be writable. So if you pass a const string
    // into this function, copy it to a buffer and then pass that to CreateProcess
    //
    // Note that it is safer to provide a full, quoted path to files
    TCHAR cmdLine[1024] = _T("java -jar myprogram.jar");

    BOOL bCreate = CreateProcess( NULL,  // no app name - provided as part of command line
                                  cmdLine,
                                  NULL,  // default process security
                                  NULL,  // default thread security
                                  FALSE, // don't inherit handles
                                  0,     // creation flags (could pass CREATE_NEW_CONSOLE here
                                         //   to force creation of new console?)
                                  NULL,  // inherit envrionment
                                  NULL,  // inherit current directory
                                  &startupInfo,
                                  &processInfo );

    if(FALSE != bCreate)
    {
        if(bWait)
        {
            // Wait for process to signal that it's exited
            WaitForSingleObject(processInfo.hProcess, INFINITE);
            // If your applet could jam up, change INFINITE to a timeout value (in millisecs
            // and then check return of WaitForSingleObject
            //   WAIT_OBJECT_0 = handle signalled :-)
            //   WAIT_TIMEOUT = app has jammed
            // anything else is a prob! (error) => see MSDN

            // If you want, you could use the following to get the exit code
            // returned by java.exe. If called while it#s still running, you
            // get STILL_ACTIVE.
            //
            // DWORD dwExitCode = NOERROR;
            // GetExitCodeProcess(processInfo.hProcess, &dwExitCode);
            //
            // ...
        }

        // Close handles to process and main thread
        CloseHandle(processInfo.hProcess);
        CloseHandle(processInfo.hThread);
    }
    else
    {
        dwRet = GetLastError();
    }

    return dwRet;
}

int _tmain(int argc, TCHAR* argv[])
{
    bool bWait = true;

    // Handle command line...

    DWORD dwRet = RunJava(bWait);

    if(NOERROR == dwRet)
        TCOUT << _T("Succeeded!") << endl;
    else
        TCOUT << _T("Failed : Error code = ") << dwRet << endl;

    return (int)dwRet;
}


CreateProcess is a bit of a mouthful, which is why I have a utility class to use instead, which just requires me to provide the command line and time out (the other parameters are defaulted).

Andy

Last edited on
If I am not mistaking, c++ does not require additional software to run on windows PC's.

If you are deploying this code, I would not only tighten up the error handling but ensure that you are statically linking to the C runtime (CRT). If you link to the CRT as a DLL, it's possible that the version of the CRT you require is not installed on the target machine.
Last edited on
Topic archived. No new replies allowed.