Java.cs
2.08 KB
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
using System;
using System.IO;
using UnityEditor.Android;
using UnityEngine;
namespace UnityEditor.XR.ARCore
{
static class Java
{
public static bool TryGetFullPathToJava(out string fullPathToJava, out string diagnosticMessage)
{
#if UNITY_ANDROID
var jdkRootPath = AndroidExternalToolsSettings.jdkRootPath;
if (string.IsNullOrEmpty(jdkRootPath))
{
(fullPathToJava, diagnosticMessage) = (null, "No JDK root path set in Preferences > External Tools > Android > JDK.");
return false;
}
var javaPath = Path.Combine(jdkRootPath, "bin", "java");
#if UNITY_EDITOR_WIN
javaPath += ".exe";
#endif
if (!File.Exists(javaPath))
{
(fullPathToJava, diagnosticMessage) = (null, $"Could not find Java executable at expected path: {javaPath}");
return false;
}
(fullPathToJava, diagnosticMessage) = (javaPath, null);
return true;
#else
(fullPathToJava, diagnosticMessage) = (null, "Cannot get Java path unless the active build platform is Android.");
return false;
#endif
}
public static bool canExecute => TryGetFullPathToJava(out _, out _);
public static (string stdout, string stderr, int exitCode) Execute(string jarFile, string arguments = null)
{
if (string.IsNullOrEmpty(jarFile))
throw new ArgumentException($"{jarFile} must not be null or empty.", nameof(jarFile));
if (!TryGetFullPathToJava(out var fullPathToJava, out var diagnosticMessage))
throw new InvalidOperationException(diagnosticMessage);
var invocation = $"-jar \"{jarFile}\"";
return Cli.Execute(fullPathToJava, string.IsNullOrEmpty(arguments) ? invocation : $"{invocation} {arguments}");
}
public static (string stdout, string stderr, int exitCode) Execute(string jarFile, string[] arguments) =>
Execute(jarFile, arguments == null ? null : string.Join(" ", arguments));
}
}