---
title: "【Java App Convert to EXE File】"
author: "piperzero"
date: "2009-12-19T10:44:00Z"
tags: []
slug: "java-app-convert-to-exe-file"
draft: false
lastmod: "2013-01-07T09:59:59Z"
id: 1764649
url: "https://piperzero.iteye.com/blog/1764649"
---

\* ------------------------------------------------------------------------------------  
Title: TestJava.cpp  
author: gloomy fish on 2009-09-16  
\------------------------------------------------------------------------------------ \*/  
#include &lt;windows.h&gt;  
#include &lt;stdio.h&gt;  
#include &lt;stdlib.h&gt;  
/* for spawn \*/  
#include &lt;process.h&gt;  
/* for str* functions \*/  
#include &lt;string.h&gt;  
#include &lt;TCHAR.H&gt;

//-------------------------------------------------------------------------------  
int APIENTRY \_tWinMain(HINSTANCE hInstance,  
HINSTANCE hPrevInstance,  
LPTSTR lpCmdLine,  
int nCmdShow)  
{

// -- check wether application is running now....  
HANDLE hMutex = CreateMutex (NULL, // No security descriptor  
FALSE, // Mutex object not owned  
TEXT("TotallyBogusMutexObjectPlateUtility")); // Object name

if (NULL == hMutex)  
{  
// Didn't expect this to happen!  
printf("Mutex is null");  
return 1;  
}  
else  
{  
// Not an error -- deal with success  
if ( ERROR\_ALREADY\_EXISTS == GetLastError() )  
{  
return 0;  
}  
}

// -- set up classpath & env path to launch  
int ret;  
TCHAR envPath\[3072] = "PATH=.;D://JDK6//bin;%path%";  
int anErr = \_tputenv(envPath);  
if (anErr != 0) {  
ret = anErr;  
return ret;  
}

char jvmArg\[2048];

// append all necessary jars to set up classpath  
\_tcscpy(jvmArg, \_T("-cp .//test.jar"));  
// \_tcscat(jvmArg, \_T(";D://JDK6//lib"));

// -- your application  
\_tcscat(jvmArg, \_T(" -Xms256m -Xmx1024m com.swing.ui.Test") );

// -- can't launch like this......  
#ifdef \_DEBUG  
ret = \_spawnlp(\_P\_NOWAIT, "..//..//JDK6//bin//java.exe", "java", jvmArg, NULL);  
#else  
ret = \_spawnlp(\_P\_NOWAIT, "..//..//JDK6//bin//javaw.exe", "java", jvmArg, NULL);  
#endif

return ret;  
}

// winXP, vc++6.0 compile successfully....