blob: 8b22aef0d83c5c8954ce12f19cb465c51e90bade [file] [log] [blame]
James Kuszmaulb13e13f2023-11-22 20:44:04 -08001// Copyright (c) FIRST and other WPILib contributors.
2// Open Source Software; you can modify and/or share it under the terms of
3// the WPILib BSD license file in the root directory of this project.
4
5#include <filesystem>
6
7#include "Windows.h"
8
9int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
10 LPTSTR pCmdLine, int nCmdShow) {
11 DWORD Status;
12 WCHAR ExePathRaw[1024];
13 Status = GetModuleFileNameW(NULL, ExePathRaw, 1024);
14 if (Status == 0) {
15 DWORD LastError = GetLastError();
16 return LastError;
17 }
18
19 std::filesystem::path ExePath{ExePathRaw};
20 if (ExePath.empty()) {
21 return 1;
22 }
23
24 if (!ExePath.has_stem()) {
25 return 1;
26 }
27
28 if (!ExePath.has_parent_path()) {
29 return 1;
30 }
31
32 std::filesystem::path JarPath{ExePath};
33 JarPath.replace_extension(L"jar");
34 std::filesystem::path ParentPath{ExePath.parent_path()};
35
36 if (!ParentPath.has_parent_path()) {
37 return 1;
38 }
39 std::filesystem::path ToolsFolder{ParentPath.parent_path()};
40
41 std::filesystem::path Javaw = ToolsFolder / L"jdk" / L"bin" / L"javaw.exe";
42
43 std::wstring ToRun = L" -jar \"";
44 ToRun += JarPath;
45 ToRun += L"\"";
46
47 STARTUPINFOW StartupInfo;
48 PROCESS_INFORMATION ProcessInfo;
49
50 ZeroMemory(&StartupInfo, sizeof(StartupInfo));
51 StartupInfo.cb = sizeof(StartupInfo);
52 ZeroMemory(&ProcessInfo, sizeof(ProcessInfo));
53
54 if (!CreateProcessW(Javaw.c_str(), ToRun.data(), NULL, NULL, FALSE, 0, NULL,
55 NULL, &StartupInfo, &ProcessInfo)) {
56 ZeroMemory(&StartupInfo, sizeof(StartupInfo));
57 StartupInfo.cb = sizeof(StartupInfo);
58 ZeroMemory(&ProcessInfo, sizeof(ProcessInfo));
59
60 ToRun = L" -jar \"";
61 ToRun += JarPath;
62 ToRun += L"\"";
63
64 Status = GetEnvironmentVariableW(L"JAVA_HOME", ExePathRaw, 1024);
65 std::wstring JavawLocal = L"javaw";
66 if (Status != 0 && Status < 1024) {
67 std::filesystem::path JavaHomePath{ExePathRaw};
68 JavaHomePath /= "bin";
69 JavaHomePath /= "javaw.exe";
70 JavawLocal = JavaHomePath;
71 }
72
73 if (!CreateProcessW(JavawLocal.c_str(), ToRun.data(), NULL, NULL, FALSE, 0,
74 NULL, NULL, &StartupInfo, &ProcessInfo)) {
75 return 1;
76 }
77 }
78
79 Status =
80 WaitForSingleObject(ProcessInfo.hProcess, 3000); // Wait for 3 seconds
81 CloseHandle(ProcessInfo.hProcess);
82 CloseHandle(ProcessInfo.hThread);
83
84 return Status == WAIT_TIMEOUT ? 0 : 1;
85}