Hullo,

I'm trying to make a write a program to automatically create a jar file in java. I've got this far and this works.

#include <windows.h>
#include <direct.h>
#include <fstream>

using namespace std;

int main()
{
      
    chdir("C:/j2sdk1.4.2_04/bin");   
    system("javac Saluton.java");
    system("java Saluton");
   
    
    

    remove ("Saluton.class");

   

return 0;

}

But I was wondering is there a better way, using windows API to execute the system("javac Saluton.java") command?

Search for CreateProcess

BOOL CreateProcess(
LPCTSTR lpApplicationName,
LPTSTR lpCommandLine,
LPSECURITY_ATTRIBUTES lpProcessAttributes,
LPSECURITY_ATTRIBUTES lpThreadAttributes,
BOOL bInheritHandles,
DWORD dwCreationFlags,
LPVOID lpEnvironment,
LPCTSTR lpCurrentDirectory,
LPSTARTUPINFO lpStartupInfo,
LPPROCESS_INFORMATION lpProcessInformation
);

in the Platform SDK Documentation or MSDN site.

Edit:
Okay here it is.

I have no idea what that means. Seeing as you have taken the time to learn the msdn manual by heart do you think you could code up a sample snippet. :cheesy:

Or is using system() ok here?

I have no idea what that means. Seeing as you have taken the time to learn the msdn manual by heart do you think you could code up a sample snippet. :cheesy:

There is an example at the bottom of the page I linked.

Or is using system() ok here?

Personally for something like this I would use system . The overhead for the system call is nothing compared to the effort required to understand and use the CreateProcess function. I would use CreateProcess only if there are some intricate attributes that need to be set in the child processes. But there will be people here who would complain vehemently on the use of system.

Ah, just noticed your edit...

This works although I need to wrap it up into a function:

#include <windows.h>
#include <stdio.h>
#include <tchar.h>
#include <direct.h>

int main(  )
{
    chdir("C:/j2sdk1.4.2_04/bin");
    
    STARTUPINFO si;
    PROCESS_INFORMATION pi;
    LPTSTR szCmdline=_tcsdup(TEXT("javac Saluton.java"));

    ZeroMemory( &si, sizeof(si) );
    si.cb = sizeof(si);
    ZeroMemory( &pi, sizeof(pi) );

    // Start the child process. 
    if( !CreateProcess( NULL,   // No module name (use command line)
        szCmdline,      // Command line
        NULL,           // Process handle not inheritable
        NULL,           // Thread handle not inheritable
        FALSE,          // Set handle inheritance to FALSE
        0,              // No creation flags
        NULL,           // Use parent's environment block
        NULL,           // Use parent's starting directory 
        &si,            // Pointer to STARTUPINFO structure
        &pi )           // Pointer to PROCESS_INFORMATION structure
    ) 
    {
        printf( "CreateProcess failed (%d).\n", GetLastError() );
        getchar();
        //return 0;
    }

    // Wait until child process exits.
    WaitForSingleObject( pi.hProcess, INFINITE );

    // Close process and thread handles. 
    CloseHandle( pi.hProcess );
    CloseHandle( pi.hThread );
    
    getchar();
}

>But there will be people here who would complain vehemently on the use of system.

Yeah I'm kind of in two minds as to whether or not to just use system here. Mmkay.

This article has been dead for over six months. Start a new discussion instead.