All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.javonet.core.handler.LoadLibraryHandler Maven / Gradle / Ivy

Go to download

Javonet allows you to reference and use modules or packages written in (Java/Kotlin/Groovy/Clojure, C#/VB.NET, Ruby, Perl, Python, JavaScript/TypeScript) like they were created in your technology. It works on Linux/Windows and MacOS for applications created in JVM, CLR/Netcore, Perl, Python, Ruby, NodeJS, C++ or GoLang and gives you unparalleled freedom and flexibility with native performance in building your mixed-technologies products. Let it be accessing best AI or cryptography libraries, devices SDKs, legacy client modules, internal custom packages or anything from public repositories available on NPM, Nuget, PyPI, Maven/Gradle, RubyGems or GitHub. Get free from programming languages barriers today! For more information check out our guides at https://www.javonet.com/guides/v2/

There is a newer version: 2.4.5
Show newest version
package com.javonet.core.handler;

import com.javonet.core.handler.loadlibrary.JarAdder;
import com.javonet.core.handler.loadlibrary.JvmClassLoader;
import com.javonet.utils.Command;
import com.javonet.utils.exceptions.JavonetArgumentsMismatchException;

import java.io.File;
import java.io.FileNotFoundException;
import java.lang.reflect.Method;
import java.net.URL;
import java.net.URLClassLoader;

public class LoadLibraryHandler extends AbstractHandler {

  private final int requiredArgumentsCount = 1;
  private final JvmClassLoader jvmClassLoader;

  LoadLibraryHandler() {
    jvmClassLoader = new JvmClassLoader(new URL[] {}, this.getClass().getClassLoader());
  }

  @Override
  public Object process(Command command) throws Exception {
    return loadLibrary(command);
  }

  private Object loadLibrary(Command command) throws Exception {
    if (command.getPayload().length < requiredArgumentsCount)
      throw new JavonetArgumentsMismatchException(
          this.getClass().getName(), requiredArgumentsCount);

    String javaVersion = System.getProperty("java.version");
    String assemblyName = command.getPayload()[0].toString();
    if (javaVersion.startsWith("1.8")) {
      File f = new File(assemblyName);
      if (!f.exists()) {
        throw new FileNotFoundException(assemblyName);
      } else {
          jvmClassLoader.addUrl(f.toURI().toURL());
          JarAdder.addJarToClasspath(f);
          return 0;
      }
    } else {
      File jarFile = new File(assemblyName);
      if (!jarFile.exists()) {
        throw new FileNotFoundException(assemblyName);
      }

        String jarFileName = jarFile.getName();
        if (System.getenv("JAVONET_CLASSPATH") != null) {
            String[] classPath = System.getenv("JAVONET_CLASSPATH").split(String.valueOf(File.pathSeparatorChar));
            for (String path : classPath) {
                File pathFile = new File(path);
                String pathFileName = pathFile.getName();
                if (jarFileName.equals(pathFileName)) {
                    return 0;
                }
            }
          }
            throw new Exception(
              "Java version "
                      + javaVersion
                      + " does not support dynamic class loading.\n"
                      + "Library "
                      + assemblyName
                      + " cannot be loaded. \n"
                      + "Please use Java 1.8 or\n"
                      + "set environment variable JAVONET_CLASSPATH=\"full/path/sample.jar\"\n"
                      + "and restart the application.\n.");
         
    }
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy