use of dev.jbang.cli.ExitException in project jbang by jbangdev.
the class BaseBuilder method runNativeBuilder.
protected void runNativeBuilder(List<String> optionList) throws IOException {
File nilog = File.createTempFile("jbang", "native-image");
Util.verboseMsg("native-image: " + String.join(" ", optionList));
Util.infoMsg("log: " + nilog.toString());
Process process = new ProcessBuilder(optionList).inheritIO().redirectOutput(nilog).start();
try {
process.waitFor();
} catch (InterruptedException e) {
throw new ExitException(1, e);
}
if (process.exitValue() != 0) {
throw new ExitException(1, "Error during native-image");
}
}
use of dev.jbang.cli.ExitException in project jbang by jbangdev.
the class BaseBuilder method searchForMain.
protected void searchForMain(File tmpJarDir) {
try {
// using Files.walk method with try-with-resources
try (Stream<Path> paths = Files.walk(tmpJarDir.toPath())) {
List<Path> items = paths.filter(Files::isRegularFile).filter(f -> !f.toFile().getName().contains("$")).filter(f -> f.toFile().getName().endsWith(".class")).collect(Collectors.toList());
Indexer indexer = new Indexer();
Index index;
for (Path item : items) {
try (InputStream stream = new FileInputStream(item.toFile())) {
indexer.index(stream);
}
}
index = indexer.complete();
Collection<ClassInfo> classes = index.getKnownClasses();
List<ClassInfo> mains = classes.stream().filter(getMainFinder()).collect(Collectors.toList());
String mainName = getSuggestedMain();
if (mains.size() > 1 && mainName != null) {
List<ClassInfo> suggestedmain = mains.stream().filter(ci -> ci.simpleName().equals(mainName)).collect(Collectors.toList());
if (!suggestedmain.isEmpty()) {
mains = suggestedmain;
}
}
if (!mains.isEmpty()) {
ctx.setMainClass(mains.get(0).name().toString());
if (mains.size() > 1) {
Util.warnMsg("Could not locate unique main() method. Use -m to specify explicit main method. Falling back to use first found: " + mains.stream().map(x -> x.name().toString()).collect(Collectors.joining(",")));
}
}
if (ss.getMainSource().isAgent()) {
Optional<ClassInfo> agentmain = classes.stream().filter(pubClass -> pubClass.method("agentmain", STRINGTYPE, INSTRUMENTATIONTYPE) != null || pubClass.method("agentmain", STRINGTYPE) != null).findFirst();
if (agentmain.isPresent()) {
ctx.setAgentMainClass(agentmain.get().name().toString());
}
Optional<ClassInfo> premain = classes.stream().filter(pubClass -> pubClass.method("premain", STRINGTYPE, INSTRUMENTATIONTYPE) != null || pubClass.method("premain", STRINGTYPE) != null).findFirst();
if (premain.isPresent()) {
ctx.setPreMainClass(premain.get().name().toString());
}
}
}
} catch (IOException e) {
throw new ExitException(1, e);
}
}
use of dev.jbang.cli.ExitException in project jbang by jbangdev.
the class GroovyManager method downloadAndInstallGroovy.
public static Path downloadAndInstallGroovy(String version) {
Util.infoMsg("Downloading Groovy " + version + ". Be patient, this can take several minutes...");
String url = getGroovyDownloadUrl(version);
Util.verboseMsg("Downloading " + url);
Path groovyDir = getGroovyPath(version);
Path groovyTmpDir = groovyDir.getParent().resolve(groovyDir.getFileName().toString() + ".tmp");
Path groovyOldDir = groovyDir.getParent().resolve(groovyDir.getFileName().toString() + ".old");
Util.deletePath(groovyTmpDir, false);
Util.deletePath(groovyOldDir, false);
try {
Path groovyPkg = Util.downloadAndCacheFile(url);
Util.infoMsg("Installing Groovy " + version + "...");
Util.verboseMsg("Unpacking to " + groovyDir);
UnpackUtil.unpack(groovyPkg, groovyTmpDir);
if (Files.isDirectory(groovyDir)) {
Files.move(groovyDir, groovyOldDir);
}
Files.move(groovyTmpDir, groovyDir);
Util.deletePath(groovyOldDir, false);
return groovyDir;
} catch (Exception e) {
Util.deletePath(groovyTmpDir, true);
if (!Files.isDirectory(groovyDir) && Files.isDirectory(groovyOldDir)) {
try {
Files.move(groovyOldDir, groovyDir);
} catch (IOException ex) {
// Ignore
}
}
Util.errorMsg("Required Groovy version not possible to download or install.");
throw new ExitException(EXIT_UNEXPECTED_STATE, "Unable to download or install Groovy version " + version, e);
}
}
use of dev.jbang.cli.ExitException in project jbang by jbangdev.
the class JdkManager method downloadAndInstallJdk.
public static Path downloadAndInstallJdk(int version) {
Util.infoMsg("Downloading JDK " + version + ". Be patient, this can take several minutes...");
String url = getJDKUrl(version, Util.getOS().name(), Util.getArch().name(), Util.getVendor(), Util.getRelease());
Util.verboseMsg("Downloading " + url);
Path jdkDir = getJdkPath(version);
Path jdkTmpDir = jdkDir.getParent().resolve(jdkDir.getFileName().toString() + ".tmp");
Path jdkOldDir = jdkDir.getParent().resolve(jdkDir.getFileName().toString() + ".old");
Util.deletePath(jdkTmpDir, false);
Util.deletePath(jdkOldDir, false);
try {
Path jdkPkg = Util.downloadAndCacheFile(url);
Util.infoMsg("Installing JDK " + version + "...");
Util.verboseMsg("Unpacking to " + jdkDir.toString());
UnpackUtil.unpackJdk(jdkPkg, jdkTmpDir);
if (Files.isDirectory(jdkDir)) {
Files.move(jdkDir, jdkOldDir);
}
Files.move(jdkTmpDir, jdkDir);
Util.deletePath(jdkOldDir, false);
if (getDefaultJdk() < 0) {
setDefaultJdk(version);
}
return jdkDir;
} catch (Exception e) {
Util.deletePath(jdkTmpDir, true);
if (!Files.isDirectory(jdkDir) && Files.isDirectory(jdkOldDir)) {
try {
Files.move(jdkOldDir, jdkDir);
} catch (IOException ex) {
// Ignore
}
}
Util.errorMsg("Required Java version not possible to download or install. You can run with '--java " + JavaUtil.determineJavaVersion() + "' to force using the default installed Java.");
throw new ExitException(EXIT_UNEXPECTED_STATE, "Unable to download or install JDK version " + version, e);
}
}
use of dev.jbang.cli.ExitException in project jbang by jbangdev.
the class JdkManager method linkToExistingJdk.
/**
* Links JBang JDK folder to an already existing JDK path with a link. It checks
* if the incoming version number is the same that the linked JDK has, if not an
* exception will be raised.
*
* @param path path to the pre-installed JDK.
* @param version requested version to link.
*/
public static void linkToExistingJdk(String path, int version) {
Path jdkPath = getJdkPath(version);
Util.verboseMsg("Trying to link " + path + " to " + jdkPath);
if (Files.exists(jdkPath)) {
Util.verboseMsg("JBang managed JDK already exists, must be deleted to make sure linking works");
Util.deletePath(jdkPath, false);
}
Path linkedJdkPath = Paths.get(path);
if (!Files.isDirectory(linkedJdkPath)) {
throw new ExitException(EXIT_INVALID_INPUT, "Unable to resolve path as directory: " + path);
}
Optional<Integer> ver = resolveJavaVersionFromPath(linkedJdkPath);
if (ver.isPresent()) {
Integer linkedJdkVersion = ver.get();
if (linkedJdkVersion == version) {
Util.createLink(jdkPath, linkedJdkPath);
Util.infoMsg("JDK " + version + " has been linked to: " + linkedJdkPath);
} else {
throw new ExitException(EXIT_INVALID_INPUT, "Java version in given path: " + path + " is " + linkedJdkVersion + " which does not match the requested version " + version + "");
}
} else {
throw new ExitException(EXIT_INVALID_INPUT, "Unable to determine Java version in given path: " + path);
}
}
Aggregations