1 // -*- Mode: Java; indent-tabs-mode: t; tab-width: 4 -*-
2 // ---------------------------------------------------------------------------
4 // Copyright (C) Stephanie Gawroriski <xer@multiphasicapps.net>
5 // ---------------------------------------------------------------------------
6 // SquirrelJME is under the GNU General Public License v3+, or later.
7 // See license.mkd for licensing and copyright information.
8 // ---------------------------------------------------------------------------
10 package cc
.squirreljme
.plugin
.util
;
12 import cc
.squirreljme
.plugin
.multivm
.VMHelpers
;
14 import java
.nio
.file
.Files
;
15 import java
.nio
.file
.Path
;
16 import java
.nio
.file
.Paths
;
17 import java
.util
.ArrayList
;
18 import java
.util
.Collection
;
19 import java
.util
.LinkedHashMap
;
20 import java
.util
.List
;
22 import org
.gradle
.internal
.os
.OperatingSystem
;
25 * This is a simple Java execution specification filler which provides the
26 * minimal functionality.
30 public class SimpleJavaExecSpecFiller
31 implements JavaExecSpecFiller
33 /** Program arguments. */
34 private final List
<String
> _args
=
37 /** Java virtual machine arguments. */
38 private final List
<String
> _jvmArgs
=
41 /** System properties. */
42 private final Map
<String
, String
> _sysProps
=
43 new LinkedHashMap
<>();
45 /** Potential classPath objects. */
46 private final List
<Object
> _classPath
=
49 /** The main class to use. */
50 private String _mainClass
;
57 public void classpath(Collection
<Object
> __classPath
)
59 List
<Object
> classPath
= this._classPath
;
62 classPath
.addAll(__classPath
);
70 public Iterable
<String
> getCommandLine()
72 // The final arguments used
73 List
<String
> result
= new ArrayList
<>();
75 // Find the Java executable
76 result
.add(SimpleJavaExecSpecFiller
.__findJavaExe().toString());
78 // Define the classpath
79 StringBuilder classPath
= new StringBuilder();
80 String pathSep
= File
.pathSeparator
;
81 for (Path path
: VMHelpers
.resolvePath(this._classPath
))
83 // We need the path separator between libraries
84 if (classPath
.length() > 0)
85 classPath
.append(pathSep
);
88 classPath
.append(path
);
91 // Was a classpath defined?
92 if (classPath
.length() > 0)
94 result
.add("-classpath");
95 result
.add(classPath
.toString());
98 // Define any system properties
99 for (Map
.Entry
<String
, String
> sysProp
: this._sysProps
.entrySet())
100 result
.add(String
.format("-D%s=%s",
101 sysProp
.getKey(), sysProp
.getValue()));
104 result
.addAll(this._jvmArgs
);
107 result
.add(this._mainClass
);
109 // Any arguments to pass to the VM
110 result
.addAll(this._args
);
120 public void setMain(String __mainClass
)
122 this._mainClass
= __mainClass
;
130 public void setArgs(Collection
<String
> __args
)
132 List
<String
> args
= this._args
;
143 public void setJvmArgs(Collection
<String
> __args
)
145 List
<String
> jvmArgs
= this._jvmArgs
;
148 jvmArgs
.addAll(__args
);
156 public void systemProperties(Map
<String
, String
> __sysProps
)
158 Map
<String
, String
> sysProps
= this._sysProps
;
161 sysProps
.putAll(__sysProps
);
165 * Finds the Java executable.
167 * @return The Java executable.
170 private static Path
__findJavaExe()
172 String javaHomeRaw
= System
.getProperty("java.home");
173 Path javaHome
= (javaHomeRaw
!= null ? Paths
.get(javaHomeRaw
) : null);
175 // Name differs per operating system
177 if (OperatingSystem
.current() == OperatingSystem
.WINDOWS
)
178 javaExeName
= Paths
.get("java.exe");
180 javaExeName
= Paths
.get("java");
182 // Check to see if the Java executable exists here
183 Path binPath
= (javaHome
!= null ? javaHome
.resolve("bin")
184 .resolve(javaExeName
) : null);
185 if (binPath
!= null && Files
.exists(binPath
) &&
186 Files
.isExecutable(binPath
))
189 // Fallback to the system Java, assuming it exists
190 return Paths
.get("java");